Bootstrap FreeKB - Kong Enterprise Edition (KongEE) - Create Update or Delete Route using Terraform
Kong Enterprise Edition (KongEE) - Create Update or Delete Route 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
├── routes (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 "routes" {
  source = "./routes"
}

 

Add resources.tf in the routes module contains the following.

resource "kong_route" "my-route" {
    name            = "foo-route"
    protocols       = [ "http", "https" ]
    methods         = [ "GET", "POST" ]
    hosts           = [ "foo.example.com" ]
    paths           = [ "/test" ]
    strip_path      = false
    preserve_host   = true
    regex_priority  = 1
    service_id       = kong_service.service.id
    header {
      name   = "my-header"
      values = ["foo", "bar"]
    }
}

 

You may need to reissue the terraform init command.

~]# terraform init
Initializing the backend...
Initializing modules...
Initializing provider plugins...
Terraform has been successfully initialized!

 

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 route does not exist and the terraform.tfstate file does not contain the route, Terraform will create the route.
  • If the route exists and the terraform.tfstate file contains the route and a difference is found between the routes.tf file and the terraform.tfstate file, Terraform will update the route.
  • If the route exists and the terraform.tfstate file contains the route and the route is removed from the routes.tf file, Terraform will destroy (delete) the route.

 

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

terraform apply

 

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

Terraform will perform the following actions:

  # module.modules.kong_route.my-route will be created

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

 

After entering yes, the following should be displayed.

kong_route.my-route: 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 routes table in the Kong Postgres database. The Postgres SQL command would look something like this.

psql 
--username postgres
--dbname kong 
--command "INSERT INTO "routes" ("id", "created_at", "updated_at", "protocols", "methods", "hosts", "paths", "regex_priority", "strip_path", "preserve_host", "service_id", "name", "snis", "sources", "destinations", "tags", "https_redirect_status_code", "headers", "path_handling", "ws_id", "request_buffering", "request_buffering") VALUES ('8c32dc1b-b96e-41f8-969a-3a38df7e34c4', TO_TIMESTAMP(1663253243) AT TIME ZONE 'UTC', TO_TIMESTAMP(1663253243) AT TIME ZONE 'UTC', '{https}', NULL, '{foo.example.com}', '{/my-endpoint}', 0, f, f, 'f8c5b6dd-5945-424c-9b32-839faa7d80b1', 'my-route', NULL, NULL, NULL, NULL, 426, NULL, 'v0', '945e918a-cdec-4c5e-b266-55be9f3e4a93', 't', 't')"

 




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 ca84ab in the box below so that we can be sure you are a human.