Search code examples
yamlterraformterraform-provider-kubernetes

Multiline string annotations for terraform kubernetes provider


I would like to set up Ambassador as an API Gateway for kubernetes using terraform. There are several ways how to configure Ambassador. The recommended way, according to documentation, is by using kubernetes annotations for each service that is routed and exposed outside the cluster. This is done easily using kubernetes yaml configuration:

kind: Service
apiVersion: v1
metadata:
  name: my-service
  annotations:
    getambassador.io/config: |
      ---
        apiVersion: ambassador/v0
        kind:  Mapping
        name:  my_service_mapping
        prefix: /my-service/
        service: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376

The getambassador.io/config field's value starting with | suggest it is a multiline string value. How to achieve the same thing using terraform HCL?


Solution

  • Terraform documentation contains a section about multiline strings using <<EOF your multiline string EOF:

    resource "kubernetes_service" "my-service" {
      "metadata" {
        name = "my-service"
        annotations {
          "getambassador.io/config" = <<EOF
            apiVersion: ambassador/v0
            kind:  Mapping
            name:  my_service_mapping
            prefix: /my-service/
            service: my-service
            EOF
        }
      }
      "spec" {
        selector {
          app = "MyApp"
        }
        port {
          protocol = "TCP"
          port = 80
          target_port = "9376"
        }
      }
    }
    

    Make sure there is no triple dash (---) from yaml configuration. Terraform parses it incorrectly.