Kubernetes Manifest Terraform

12/10/2021

I am trying to create a Kubernetes Ingress object with the kubernetes_manifest terraform resource. It is throwing the following error:

│ Error: Failed to morph manifest to OAPI type
│   with module.services.module.portal.module.appmesh.kubernetes_manifest.service_ingress_object,
│   on .terraform/modules/services.portal.appmesh/kubernetes_manifest.tf line 104, in resource "kubernetes_manifest" "service_ingress_object":
│  104: resource "kubernetes_manifest" "service_ingress_object" {
│ AttributeName("spec"): [AttributeName("spec")] failed to morph object element into object element: AttributeName("spec").AttributeName("rules"): [AttributeName("spec").AttributeName("rules")] failed to
│ morph object element into object element: AttributeName("spec").AttributeName("rules"): [AttributeName("spec").AttributeName("rules")] unsupported morph of object value into type:
│ tftypes.List[tftypes.Object["host":tftypes.String, "http":tftypes.Object["paths":tftypes.List[tftypes.Object["backend":tftypes.Object["resource":tftypes.Object["apiGroup":tftypes.String,
"kind":tftypes.String, "name":tftypes.String], "serviceName":tftypes.String, "servicePort":tftypes.DynamicPseudoType], "path":tftypes.String, "pathType":tftypes.String]]]]]

My code is:

resource "kubernetes_manifest" "service_ingress_object" {
  manifest = {
    "apiVersion" = "networking.k8s.io/v1beta1"
    "kind"       = "Ingress"
    "metadata" = {
      "name"      = "${var.service_name}-ingress"
      "namespace" = "${var.kubernetes_namespace}"
      "annotations" = {
        "alb.ingress.kubernetes.io/actions.ssl-redirect" = "{'Type': 'redirect', 'RedirectConfig': { 'Protocol': 'HTTPS', 'Port': '443', 'StatusCode': 'HTTP_301'}}"
        "alb.ingress.kubernetes.io/listen-ports"         = "[{'HTTP': 80}, {'HTTPS':443}]"
        "alb.ingress.kubernetes.io/certificate-arn"      = "${data.aws_acm_certificate.enivronment_default_issued.arn}"
        "alb.ingress.kubernetes.io/scheme"               = "internal"
        "alb.ingress.kubernetes.io/target-type"          = "instance"
        "kubernetes.io/ingress.class"                    = "alb"
      }
    }
    "spec" = {
      "rules" = {
        "host" = "${aws_route53_record.service_dns.fqdn}"
        "http" = {
          "paths" = {
            "backend" = {
              "serviceName" = "${var.service_name}-svc"
              "servicePort" = "${var.service_port}"
            }
            "path" = "/*"
          }
        }
      }
    }
  }
}

I have tried adding brackets to the "spec" field, however when I do that, I just the following error:

│ Error: Missing item separator
│   on .terraform/modules/services.portal.appmesh/kubernetes_manifest.tf line 121, in resource "kubernetes_manifest" "service_ingress_object":
│  120:     "spec" = {[
│  121:       "rules" = {
│ Expected a comma to mark the beginning of the next item.

Once I get that error, I have tried adding commas under "spec". It just continuously throws the same error after this.

-- sd-gallowaystorm
kubernetes
terraform

1 Answer

12/10/2021

I figured it out. You need to add the bracket before the "{". So the code now looks like this:

resource "kubernetes_manifest" "service_ingress_object" {
  manifest = {
    "apiVersion" = "networking.k8s.io/v1beta1"
    "kind"       = "Ingress"
    "metadata" = {
      "name"      = "${var.service_name}-ingress"
      "namespace" = "${var.kubernetes_namespace}"
      "annotations" = {
        "alb.ingress.kubernetes.io/actions.ssl-redirect" = "{'Type': 'redirect', 'RedirectConfig': { 'Protocol': 'HTTPS', 'Port': '443', 'StatusCode': 'HTTP_301'}}"
        "alb.ingress.kubernetes.io/listen-ports"         = "[{'HTTP': 80}, {'HTTPS':443}]"
        "alb.ingress.kubernetes.io/certificate-arn"      = "${data.aws_acm_certificate.enivronment_default_issued.arn}"
        "alb.ingress.kubernetes.io/scheme"               = "internal"
        "alb.ingress.kubernetes.io/target-type"          = "instance"
        "kubernetes.io/ingress.class"                    = "alb"
      }
    }
    "spec" = {
      "rules" = [{
        "host" = "${aws_route53_record.service_dns.fqdn}"
        "http" = {
          "paths" = [{
            "backend" = {
              "serviceName" = "${var.service_name}-svc"
              "servicePort" = "${var.service_port}"
            }
            "path" = "/*"
          }]
        }
      }]
    }
  }
}
-- sd-gallowaystorm
Source: StackOverflow