Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The aws cli to create a IAM policy, create a user, create access key, and attach the user to the policy
# The code for the backend-role-policy.json is available at https://gist.github.com/kunduso/bf94f1aa5e683ed66539458a9a44138d
# create a policy with name "Custom-Terraform-Policy-Backend-April"
# https://docs.aws.amazon.com/cli/latest/reference/iam/create-policy.html
aws iam create-policy --policy-name Custom-Terraform-Policy-Backend-April --policy-document file://backend-role-policy.json
#output
{
"Policy": {
"PolicyName": "Custom-Terraform-Policy-Backend-April",
"PolicyId": "ANPAZIAA3LP6OBWQHE5E6",
"Arn": "arn:aws:iam::$(AWSAccountNumber):policy/Custom-Terraform-Policy-Backend-April",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"CreateDate": "2021-04-01T18:45:25+00:00",
"UpdateDate": "2021-04-01T18:45:25+00:00"
}
}
# Create a user with name Terraform-User
# https://docs.aws.amazon.com/cli/latest/reference/iam/create-user.html
aws iam create-user --user-name Terraform-User
#output
{
"User": {
"Path": "/",
"UserName": "Terraform-User",
"UserId": "AIDAZIAA3LP6FKZZCFBD5",
"Arn": "arn:aws:iam::$(AWSAccountNumber):user/Terraform-User",
"CreateDate": "2021-04-01T18:54:27+00:00"
}
}
# Create access key for the user with name Terraform-User
# https://docs.aws.amazon.com/cli/latest/userguide/cli-services-iam-create-creds.html
aws iam create-access-key --user-name Terraform-User
#output
{
"AccessKey": {
"UserName": "Terraform-User",
"AccessKeyId": "$(AccessKeyId-For-This-User)",
"Status": "Active",
"SecretAccessKey": "$(SecretAccessKey-For-This-User)",
"CreateDate": "2021-04-01T18:56:47+00:00"
}
}
# Attach a policy with policy ARN to a user with name Terraform-User
# https://docs.aws.amazon.com/cli/latest/reference/iam/attach-user-policy.html
aws iam attach-user-policy --policy-arn arn:aws:iam::$(AWSAccountNumber):policy/Custom-Terraform-Policy-Backend-April --user-name Terraform-User
# No output if user successfully attached to a policy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment