Skip to content

Instantly share code, notes, and snippets.

@nghnam
Last active February 14, 2020 06:42
Show Gist options
  • Select an option

  • Save nghnam/6b49f18c01c5d0468c7dff7ccfe607f1 to your computer and use it in GitHub Desktop.

Select an option

Save nghnam/6b49f18c01c5d0468c7dff7ccfe607f1 to your computer and use it in GitHub Desktop.
Use terraform to manage Kong
provider "kong" {
kong_admin_uri = "http://192.168.1.100:8001"
}
variable "kong_admin_url" {
description = "Kong admin api"
default = "http://192.168.1.100:8001"
}
variable "example-user" {
default = "admin"
}
variable "example-password" {
default = "admin"
}
resource "kong_service" "example" {
name = "example"
protocol = "http"
host = "example"
port = 6555
retries = 5
}
resource "kong_route" "example-route" {
name = "example-route"
protocols = ["https"]
hosts = ["api.example.com"]
paths = ["/"]
https_redirect_status_code = 301
strip_path = false
preserve_host = true
service_id = kong_service.example.id
}
resource "kong_upstream" "example" {
name = "example"
slots = 1000
}
resource "kong_target" "example-target1" {
target = "192.168.1.150:6555"
weight = 10
upstream_id = kong_upstream.example.id
}
resource "kong_target" "example-target2" {
target = "192.168.1.151:6555"
weight = 10
upstream_id = kong_upstream.example.id
}
resource "kong_consumer" "example" {
username = "example"
custom_id = "123"
provisioner "local-exec" {
command = <<EOT
curl -X POST ${var.kong_admin_url}/consumers/${kong_consumer.example.id}/basic-auth \
--data username=${var.example-user} \
--data password=${var.example-password}
EOT
}
}
resource "kong_plugin" "example-basic-auth" {
name = "basic-auth"
route_id = kong_route.example-route.id
enabled = true
config_json = <<EOT
{
"hide_credentials": true
}
EOT
}
resource "kong_certificate" "example" {
certificate = file("certificates/example.com/fullchain.pem")
private_key = file("certificates/example.com/privkey.pem")
}
resource "kong_sni" "example" {
name = "api.example.com"
certificate_id = kong_certificate.example.id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment