To use Terraform to establish an AWS user, use the aws_iam_user resource given by the AWS provider. Here’s a step-by-step tutorial for creating an AWS user with Terraform.
1 Configure AWS Credentials:
Make sure you have your AWS credentials set up before you begin. You may either specify them as environment variables or use the AWS CLI aws configure command.
2 Create a Terraform configuration by following these steps:
To define your Terraform setup, create a.tf file (for example, main.tf).
3 Create an AWS User Resource:
To define the AWS user resource, add the following code to your main.tf file:
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = "ap-south-1" access_key = "your_Access_Key" secret_key = "Your_Secret_Key" } resource "aws_iam_user" "example_user" { name = "nitin_user" } resource "aws_iam_access_key" "kye1" { user = aws_iam_user.example_user.id } output "secret_key" { value = aws_iam_access_key.kye1.secret sensitive = true } output "access_key" { value = aws_iam_access_key.kye1.id } resource "aws_iam_policy_attachment" "test-attach" { name = "test-attachment" users = [aws_iam_user.example_user.name] # roles = [aws_iam_role.role.name] # groups = [aws_iam_group.group.name] policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess" }
4 Initialize Terraform:
To start Terraform, navigate to the directory containing your Terraform configuration file and run the following command:
terraform init
5 Plan the Configuration:
In HashiCorp Terraform, the terraform plan command generates an execution plan outlining the modifications Terraform will make to your infrastructure based on your existing configuration. Without actually making the changes, it demonstrates to you what steps Terraform will take to create, update, or remove resources, for example. By doing so, you may examine and confirm the modifications before implementing them in your infrastructure.
terraform plan
6 Apply the Configuration:
Run the following command to create the AWS user:
terraform apply
7 Review and Confirm:
Terraform will display a plan of what it aims to build. After reviewing the plan, type yes to confirm and create the AWS user.
Pingback:AWS terraform init terraform plan terraform apply