Terraform Level 3 Task 10 Managing Terraform Workspaces

Hi Support Team,

Can someone please review this task? I get the following error message:

“Dev workspace API Gateway names are incorrect or missing”

However, I think I’ve created them correctly see the following screenshots and code snippets:

locals {
env = terraform.workspace
api_names = var.KKE_API_NAMES
api_gw = local.env == “dev” ? [local.api_names[0]] : local.env == “prod” ? [local.api_names[1]] : []
}

resource “aws_api_gateway_rest_api” “devops_api_gws” {
count = length(local.api_gw)
name = “${local.env}-${local.api_gw[count.index]}”

provisioner “local-exec” {
when = create
command = “echo "Created API Gateway ${self.name} in workspace ${terraform.workspace}" >> apigateway.log”
}
}

resource “aws_cloudwatch_log_group” “devops_log_groups” {
count = length(local.api_gw)
name = “/aws/apigateway/${aws_api_gateway_rest_api.devops_api_gws[count.index].name}”

provisioner “local-exec” {
when = create
command = “echo "Created Log Group ${self.name} in workspace ${terraform.workspace}" >> loggroups.log”
}
}

variable “KKE_API_NAMES” {
description = “Workspace API names”
type = list(string)

validation {
condition = length(var.KKE_API_NAMES) >= 2
error_message = “Provide at least two API names, e.g., ["devops-api-1","devops-api-2"].”
}
}

output “kke_api_gateway_names” {
description = “API Gateway names created in the current workspace”
value = [for api in aws_api_gateway_rest_api.devops_api_gws : api.name]
}

output “kke_log_group_names” {
description = “CloudWatch Log Group names created in the current workspace”
value = [for log in aws_cloudwatch_log_group.devops_log_groups : log.name]
}

terraform.tfvars

KKE_API_NAMES = [“devops-api-1”, “devops-api-2”]

Hi @amaruxia

I’ve just checked it and it’s worked properly from my end. Please refer to my main.tf file

# Get the current workspace
locals {
  workspace = terraform.workspace
}

# Create API Gateway REST APIs using count
resource "aws_api_gateway_rest_api" "this" {
  count = length(var.KKE_API_NAMES)
  name  = "${local.workspace}-${var.KKE_API_NAMES[count.index]}"

  lifecycle {
    create_before_destroy = true
  }

  provisioner "local-exec" {
    command = "echo 'Created API Gateway ${self.name} in workspace ${local.workspace}' >> /home/bob/terraform/apigateway.log"
  }
}

# Create matching CloudWatch Log Groups
resource "aws_cloudwatch_log_group" "this" {
  count = length(var.KKE_API_NAMES)
  name  = "/aws/apigateway/${local.workspace}-${var.KKE_API_NAMES[count.index]}"
  retention_in_days = 30

  provisioner "local-exec" {
    command = "echo 'Created Log Group ${self.name} in workspace ${local.workspace}' >> /home/bob/terraform/loggroups.log"
  }
}

Raymond,

Thanks for the quick response. I understand the error I made. I misinterpreted the lab question and was thinking it needed one API per workspace instead of creating all APIs in the list. DOH! :slight_smile:

Thank you for your assistance, you guys are awesome!

Regards,

Amaru

1 Like

Two suggestions in this lab, one: make the logged message clear because the lab validation expected a specific message while the lab itself doesn’t request that


two: make it clear that you want two resource for each workspace because the example in the task is confusing
image

I concur this. Instructions are not very clear on several tasks. The verbiage used should be updated correctly to what exactly the task validations are expecting.