38.User Variable Setup Using Terraform
Source: Dev.to
Lab Information
The Nautilus DevOps team is automating IAM user creation using Terraform for better identity management.
Create an AWS IAM user with the following requirements:
- The IAM user name
iamuser_markshould be stored in a variable namedKKE_user. - Configuration values should be stored in a
variables.tffile. - The Terraform script should be structured with a
main.tffile referencingvariables.tf. - Working directory:
/home/bob/terraform.
variables.tf
# Define variable for IAM user name
variable "KKE_user" {
description = "The name of the IAM user to create"
type = string
default = "iamuser_mark"
}
main.tf
# Create AWS IAM User with variable reference
resource "aws_iam_user" "this" {
name = var.KKE_user
tags = {
Name = var.KKE_user
}
}
Terraform Commands
cd /home/bob/terraform
terraform init
Output (excerpt):
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "5.91.0"...
- Installing hashicorp/aws v5.91.0...
- Installed hashicorp/aws v5.91.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider selections.
Terraform has been successfully initialized!
terraform plan
Output (excerpt):
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
+ create
# aws_iam_user.this will be created
+ resource "aws_iam_user" "this" {
+ arn = (known after apply)
+ force_destroy = false
+ id = (known after apply)
+ name = "iamuser_mark"
+ path = "/"
+ tags = {
+ "Name" = "iamuser_mark"
}
+ tags_all = {
+ "Name" = "iamuser_mark"
}
+ unique_id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
terraform apply
When prompted, type yes to confirm.
Output (excerpt):
aws_iam_user.this: Creating...
aws_iam_user.this: Creation complete after 0s [id=iamuser_mark]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.