Bootstrap FreeKB - Kong Enterprise Edition (KongEE) - Create Update or Delete a Service using Terraform
Kong Enterprise Edition (KongEE) - Create Update or Delete a Service using Terraform


Kong is a service that sits between a client and a server, such as a web server, an application server, an FTP server. For example, let's say you want to route requests to www.example.com through Kong. In this scenario, you could create a route and a service that would be used to route requests onto www.example.com.

 

Let's say you have the following files on your Terraform server.

├── locals.tf
├── modules.tf
├── outputs.tf
├── provider.tf
├── terraform.tfstate
├── variables.tf
├── services (directory, child module)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf

 

Let's say provider.tf has the following. This assumes you have setup Terraform as described in Kong - Getting Started with Terraform.

terraform {
  required_providers {
    kong = {
      source  = "philips-labs/kong"
    }
  }
}

provider "kong" {
  kong_admin_uri = "http://kong.example.com:8001"
  kong_admin_username = "admin"
  kong_admin_password = "itsasecret"
}

 

And modules.tf contains something like this.

module "services" {
  source = "./services"
}

 

Add resources.tf in your services module contains the following.

resource "kong_service" "my-service" {
    name        = "foo"
    protocol    = "http"
    host        = "foo.example.com"
    port        = 8080
    path        = "/foo"
    retries     = 5
    connect_timeout = 1000
    write_timeout   = 2000
    read_timeout    = 3000
}

 

The terraform plan command can be used to see what Terraform will try to do.

terraform plan

 

By default, the terraform.tfstate file should be found in your root module directory (/usr/local/terraform/kong in this example).

  • If the service does not exist and the terraform.tfstate file does not contain the service, Terraform will create the service.
  • If the service exists and the terraform.tfstate file contains the service and a difference is found between the services.tf file and the terraform.tfstate file, Terraform will update the service.
  • If the service exists and the terraform.tfstate file contains the service and the service is removed from the services.tf file, Terraform will destroy (delete) the service.

The terraform apply command from your root module directory (/usr/local/terraform/kong in this example) can be used to create or update the service. 

terraform apply

 

Something like this should be displayed, and you will be prompted to enter yes.

Terraform will perform the following actions:

  # module.services.kong_service.my-service will be created
  + resource "kong_service" "my-service" {
      + connect_timeout = 1000
      + host            = "foo.example.com"
      + id              = (known after apply)
      + name            = "foo"
      + path            = "/foo"
      + port            = 8080
      + protocol        = "http"
      + read_timeout    = 3000
      + retries         = 5
      + write_timeout   = 2000
    }

Plan: 1 to add, 0 to change, 0 to destroy.

 

After entering yes, the following should be displayed.

kong_service.my-service: Creation complete after 0s [id=6c7e44b3-35c6-4e85-b3ed-78f96dab2d88]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

 


Postgres

Undeneath the hood, this tells Kong to issue an INSERT, UPDATE or DELETE statement to insert/update/delete the record in the services table in the Kong Postgres database. The Postgres SQL command would look something like this.

psql 
--username postgres
--dbname kong 
--command "INSERT INTO "services" ("id", "created_at", "updated_at", "name", "retries", "protocol", "host", "port", "path", "connect_timeout", "write_timeout", "read_timeout", "tags", "client_certificate_id", "tls_verify", "tls_verify_depth", "ca_certificates", "enabled", "ws_id") VALUES ('945e918a-cdec-4c5e-b266-55be9f3e4a93', TO_TIMESTAMP(1663253243) AT TIME ZONE 'UTC', TO_TIMESTAMP(1663253243) AT TIME ZONE 'UTC', 'foo.example.com', 5, 'https', '/foo', 8080, NULL, 3000, NULL, 2000, NULL, NULL, NULL, NULL, NULL, TRUE, 'f8c5b6dd-5945-424c-9b32-839faa7d80b1')"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter d18131 in the box below so that we can be sure you are a human.