Using this Terraform code, we can provision AWS IAM users and attach policies to them.
This Terraform configuration allows for the creation of IAM users in AWS and the attachment of a predefined policy to those users. IAM (Identity and Access Management) in AWS enables you to securely control access to AWS services and resources.
Before using this Terraform configuration, ensure you have the following:
-
Passing IAM credentials through environment variables ensures that Terraform can securely access them without explicitly storing them in its configuration.
-
Terraform automatically fetches these credentials from the environment variables during its execution.
- Update the variables.tf file to specify the usernames for the IAM users you want to create.
- Initialize the Terraform configuration by running
terraform init
. - Review the execution plan by running
terraform plan
. - If the plan looks good, apply the changes by running
terraform apply
. - After applying the changes, Terraform will output the details of the created IAM users.
The aws_iam_user
resource block is responsible for creating IAM users in AWS. It iterates over the usernames specified in the var.username variable and creates a user for each.
The aws_iam_policy
resource block defines a policy named ec2_policy
, which allows describing EC2 instances. It grants the necessary permissions using a JSON-encoded policy document.
The aws_iam_policy_attachment resource block attaches the ec2_policy
to the IAM users created earlier. It iterates over each IAM user and attaches the policy to them. The lifecycle block ensures that the attachment is created before any existing attachments are destroyed, to prevent downtime.
![Screenshot_1](https://private-user-images.githubusercontent.com/37361340/326870924-0c8390c1-4b2b-42de-b487-37a172a222f7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3MDI4OTcsIm5iZiI6MTcyNDcwMjU5NywicGF0aCI6Ii8zNzM2MTM0MC8zMjY4NzA5MjQtMGM4MzkwYzEtNGIyYi00MmRlLWI0ODctMzdhMTcyYTIyMmY3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI2VDIwMDMxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWRmYWM4MDUzYzU1Nzg1ZjUwZGRhMmY3YjEwOTE3OTEwMWZlZjllNWE5YTZjMzg0NmViMWFjZjUzZWJhNWFiZmImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.wTcOsrQUSciAaqCb0hl6MUvADpmRgqYG7UPAon-OFMI)
![Screenshot_2](https://private-user-images.githubusercontent.com/37361340/326871060-578c33f9-30ac-4dd0-ba71-28782f6118eb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3MDI4OTcsIm5iZiI6MTcyNDcwMjU5NywicGF0aCI6Ii8zNzM2MTM0MC8zMjY4NzEwNjAtNTc4YzMzZjktMzBhYy00ZGQwLWJhNzEtMjg3ODJmNjExOGViLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI2VDIwMDMxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNmM2FjZjA1NmQ1MDJmZjYyNGVjNzk3NzdiYjY5NjdhM2ExNzhmMzRiYmVhY2Y2YmUzYTk4NzQ1NmNhZWJjZjkmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.mYgQs74hIwhAm27YMiJIhYfefS0MqseDXFhNnEN9h7I)
![Screenshot_3](https://private-user-images.githubusercontent.com/37361340/326871234-c69b4689-af46-4940-a19c-47a3ef9eccf0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3MDI4OTcsIm5iZiI6MTcyNDcwMjU5NywicGF0aCI6Ii8zNzM2MTM0MC8zMjY4NzEyMzQtYzY5YjQ2ODktYWY0Ni00OTQwLWExOWMtNDdhM2VmOWVjY2YwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI2VDIwMDMxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTllMmFhOWEzYmI1OWE4YWY4OGQ5MmFjY2RhNWYyNDE3N2QzZWFmMTEzODBjMzg3MWU1NzFmZTdiNjMyYzg1NGQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.8VqDmURXFjiygVYFy8W_T2goJkAB9_v73DlqUOdI3XQ)
![Screenshot_4](https://private-user-images.githubusercontent.com/37361340/326871345-0272d5cb-dea5-4399-a693-123fbf292f96.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3MDI4OTcsIm5iZiI6MTcyNDcwMjU5NywicGF0aCI6Ii8zNzM2MTM0MC8zMjY4NzEzNDUtMDI3MmQ1Y2ItZGVhNS00Mzk5LWE2OTMtMTIzZmJmMjkyZjk2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI2VDIwMDMxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWMzOTk2Yjk3NWIwMTM1ODVjYmE1YTRjZDY4YjI4YmUxMjUzMzMyNjBhYzgxZWRkYzkyMjVlYjMyZjNmMmZmOGImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.2fb6EEcI950hjtAwm39qlZjj15xkn1HD3tO3aC-LkLE)