Terraform Level-3 question-3 Enforcing IAM Naming Standards and Permissions Using Terraform

Hi All,

I am stuck in this task. Getting error as KKE_PROJECT not set or incorrect. Kindly review my code and help me on this.

#main.tf
locals {
sanitized_project = lower(replace(var.KKE_PROJECT, “[^a-zA-Z0-9-]”, “-”))
sanitized_team = lower(replace(var.KKE_TEAM, “[^a-zA-Z0-9-]”, “-”))

resource_prefix = “${local.sanitized_project}-${local.sanitized_team}”

common_tags = {
Project = “datacenter”
Team = “dev-team”
ManagedBy = “Terraform”
Env = var.KKE_ENVIRONMENT
}
}

resource “aws_iam_user” “kke_user” {
name = “${local.resource_prefix}-user”
tags = local.common_tags
}

data “aws_iam_policy_document” “assume_ec2” {
statement {
actions = [“sts:AssumeRole”]

principals {
  type        = "Service"
  identifiers = ["ec2.amazonaws.com"]
}

}
}

resource “aws_iam_role” “kke_role” {
name = “${local.resource_prefix}-role”
assume_role_policy = data.aws_iam_policy_document.assume_ec2.json

tags = merge(
local.common_tags,
{
RoleType = “EC2”
}
)
}

#outputs.tf
output “kke_user_name” {
value = aws_iam_user.kke_user.name
}

output “kke_role_name” {
value = aws_iam_role.kke_role.name
}

output “kke_tags_applied” {
value = aws_iam_user.kke_user.tags
}

#terraform.tfvars

KKE_PROJECT = “DataCenter”
KKE_TEAM = “Dev_Team”
KKE_ENVIRONMENT = “dev”

#variables.tf
variable “KKE_PROJECT” {
type = string
validation {
condition = length(var.KKE_PROJECT) > 0
error_message = “KKE_PROJECT cannot be empty. Please provide a valid project name.”
}

}

variable “KKE_TEAM” {
type = string
validation {
condition = can(regex(“[1]+$”, var.KKE_TEAM))
error_message = “KKE_TEAM may only contain letters, digits, dashes, or underscores.”
}
}

variable “KKE_ENVIRONMENT” {
type = string
}

Thank You!


  1. a-zA-Z0-9-_ ↩︎

Hi @Venkata_Pavan

If you share the code, please use a code block. It will make the code easier to read.

Hi @Venkata_Pavan

I’ve checked it and I believe this is a valid issue. I’ll inform the team and we will look into it. I’ll keep you updated.

1 Like

Hi @Venkata_Pavan

Please try again and make sure the terraform.tfvars file declares KKE_PROJECT, KKE_TEAM, and KKE_ENVIRONMENT, and that their values match the ones in the task.