Lab 39: Role Variable Setup Using Terraform
The Nautilus DevOps team is automating IAM role creation using Terraform to streamline permissions management. As part of this task, they need to create an IAM role with specific requirements.
For this task, create an AWS IAM role using Terraform with the following requirements:
- The IAM role name
iamrole_mariyamshould be stored in a variable namedKKE_iamrole.
Note:
-
- The configuration values should be stored in a variables.tf file.
-
- The Terraform script should be structured with a main.tf file referencing variables.tf.
-
The Terraform working directory is
/home/bob/terraform. -
Right-click under the
EXPLORERsection inVS Codeand selectOpen in Integrated Terminalto launch the terminal.
# /home/bob/terraform/variables.tf
# Variable to store the IAM role name as required by the task
variable "KKE_iamrole" {
description = "The name for the IAM role"
type = string
# The default value is set to the required role name: iamrole_mariyam
default = "iamrole_mariyam"
}
# /home/bob/terraform/main.tf
# main.tf
resource "aws_iam_role" "mariyam_role" {
# Role name is retrieved from the variable KKE_iamrole
name = var.KKE_iamrole
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "ec2.amazonaws.com"
},
},
],
})
# Optional: Define tags for the role
tags = {
Automation = "Terraform-DevOps"
Project = "Nautilus"
}
}
# Optional: Output the ARN of the created role for verification
output "iam_role_arn" {
description = "The ARN of the newly created IAM role"
value = aws_iam_role.mariyam_role.arn
}
terraform init
# or apply forcefully without creating plan and applying it
terraform apply -auto-approve