Terraform Challenges: Challenge 1

Stuck on the Kubernetes Service answer check. I wrote each config file separately (provider.tf, resource.tf, main.tf). When I run “terraform init” and “Terraform validate”, it confirms my code is correct. However, when I select “Check” to check my work, I get an error that none of the steps are completed.

Error:
Tasks not completed!

  • Create a terraform resource webapp-service for kubernetes service with following specs:
    Service name: webapp-service
  • Service Type: NodePort
  • Port: 8080
  • NodePort: 30080

My Code:

Resouce for Kubernetes Service

resource “kubernetes_service” “webapp_service” {
metadata {
name = “webapp-service”
}

spec {
    selector = {
        app = "webapp"
    }

    type = "NodePort"

    port {
        port        = 8080
        node_port   = 30080
    }
}

}

Hi @jojofly

That’s because how the grader is evaluating your Service resource. The issue here could be the way you are targeting the Deployment to expose this Service resource. You have hard-coded the label “webapp” in selector block. This would create issues when Terraform tries to create dependency based on explicit resource references.

Instead, you can try:

    selector = {
      name = kubernetes_deployment.frontend.spec.0.template.0.metadata.0.labels.name
    }

In this way, Terraform understands the dependency between the kubernetes_service and kubernetes_deployment resources.

Also, you are missing targetPort in your port that defines a port to access on pods targeted by this service.

Try updating these and check again.