Manage NGINX configurations using Terraform

Overview

Use the f5ads_configuration resource to manage versioned NGINX configurations with Terraform. For TLS, create a certificate first and reference its resource ID in the configuration payload. The deployment uses the configuration ID and its latest version ID.

Prerequisites

Create a certificate with Terraform before you add the configuration below. See Add certificates using Terraform.

Create an NGINX configuration

The following example assumes that your certificate resource is named f5ads_certificate.tls. It creates an HTTPS server configuration and links the certificate and key to the paths used by NGINX.

hcl
resource "f5ads_configuration" "tls" {
  name        = "example-tls-config"
  description = "NGINX configuration with a managed TLS certificate."

  payloads = [
    {
      type      = "managed_certificate"
      object_id = f5ads_certificate.tls.id
      paths     = ["/etc/nginx/tls/server.crt"]
    },
    {
      type      = "managed_key"
      object_id = f5ads_certificate.tls.id
      paths     = ["/etc/nginx/tls/server.key"]
    },
  ]

  configs = [
    {
      name = "/etc/nginx"
      files = [
        {
          name = "nginx.conf"
          contents = base64encode(<<-EOT
            worker_processes auto;

            events {
              worker_connections 1024;
            }

            http {
              server {
                listen 443 ssl;
                server_name example.com;

                ssl_certificate     /etc/nginx/tls/server.crt;
                ssl_certificate_key /etc/nginx/tls/server.key;

                location / {
                  return 200 "OK";
                }
              }
            }
          EOT
          )
        },
      ]
    },
  ]
}

The managed_certificate and managed_key payloads link the certificate to the configuration. Their paths values must match the paths in the ssl_certificate and ssl_certificate_key directives. The configuration resource creates a new version when you change its files.

Initialize Terraform, review the plan, and apply the configuration:

shell
terraform init
terraform plan
terraform apply

Apply the configuration to a deployment

Set nginx_config_id to the configuration resource’s id and nginx_config_version_id to its latest_version_id in your f5ads_deployment resource.

Add the cloud-specific properties required by your deployment. For examples, see Deploy using the Terraform provider for AWS or Deploy using the Terraform provider for Google Cloud.

References