
Head of software development @beSharpsrl// Leapp Creator //
#AWSCertified // 🔥 about #cloudcomputing and #AWS // ❤️ Cinema, Game programmer, comic artist //
Search for a command to run...

Head of software development @beSharpsrl// Leapp Creator //
#AWSCertified // 🔥 about #cloudcomputing and #AWS // ❤️ Cinema, Game programmer, comic artist //
No comments yet. Be the first to comment.
We have decided to close down Noovolari. This decision, marks the end of an amazing journey.

0.2 | 09.2.24

0.1 | 25.1.24

The PiedPiper practical example of managing a large number of developers with an IAM Role Federated strategy with cross-account access

The PiedPiper practical example of managing a large number of developers with an IAM Users strategy with cross-account access

AWS CLI stands for Amazon Web Services Command Line Interface.
It is an open-source tool, and knowing how to use it to interact with AWS Services is crucial, especially for Developers.
It allows to centralize control of all existing services from a single tool, and moreover, to make automated scripts.
AWS Identity & Access Management, IAM in short, provides fine-grained access control across AWS services.
This article will show how to use the AWS CLI to perform all the most common IAM operations.
$ aws --version.
$ which aws.
$ aws --cli-auto-prompt: enable Auto Completion mode for the CLI, giving you suggestions as you write down your commands. Just remember to exit this mode when you need to run scripts!Users can create commands in single or multiple lines. The \ character splits a command into multiple lines for better readability.
In general, a command is structured in this way:

You have the CLI invocation, and then you apply a command to a specific service. You can also add many different optional parameters.
There are many different commands that you can exploit using the AWS CLI, but this article will focus only on those related to IAM and STS (AWS Security Token Service).
Because commands can have many optional parameters, we recommend opening this link in your browser for further reference information.
Note: you can use the TOC feature to jump to your desired command.
Create a new IAM user.
aws iam create-user --user-name AlessandroArticle
iam: Servicecreate-user: Command--user-name: Name of the userOutput:
{
"User": {
"Path": "/",
"UserName": "AlessandroArticle",
"UserId": "<user_id>",
"Arn": "arn:aws:iam::<account_number>:user/AlessandroArticle",
"CreateDate": "<creation_date>"
}
}
Lists all users in the credentials’ set account.
aws iam list-users
iam: Servicelist-users: CommandOutput:
{
"Users": [
{
"Path": "/",
"UserName": "AlessandroArticle",
"UserId": "<user_id>",
"Arn": "arn:aws:iam::<account_number>:user/AlessandroArticle",
"CreateDate": "<creation_date>"
}
]
}
Updates an IAM user. We can update the name of a user using the update-user command.
aws iam update-user --user-name AlessandroArticle --new-user-name AlessandroArticleNew
iam: Serviceupdate-user: Command—-user-name: The old name—-new-user-name: The new nameDeletes the specified IAM user.
aws iam delete-user —user-name AlessandroArticle
iam: Serviceupdate-user: Command—-user-name: The name of the user to removeNote: you must delete the items attached to the user before attempting to delete a user, otherwise the command will fail (as per AWS documentation):
Pro tips:
List userId and UserName
aws iam list-users | jq -r ‘.Users[ ]|.UserId+” “+.UserName’
Get single user
aws iam get-user --user-name (username)
Add user
aws iam create-user --user-name (username)
Delete user
aws iam delete-user --user-name (username)
List access keys for user
aws iam list-access-keys --user-name (username) | jq -r .AccessKeyMetadata[ ].AccessKeyId
Delete access key for user
aws iam delete-access-key --user-name (username) --access-key-id (accessKeyID)
Activate/deactivate access key for user
aws iam update-access-key --status Active --user-name (username) --access-key-id (access key)
aws iam update-access-key --status Inactive --user-name (username) --access-key-id (access key)
Generate new access key for user
aws iam create-access-key --user-name (username) | jq -r ‘.AccessKey | .AccessKeyId+” “+.SecretAccessKey’
Creates a new IAM policy.
aws iam create-policy --policy-name example-policy --policy-document file://example-policy.json
iam: Servicecreate-policy: Command--policy-name: Name of the IAM policy--policy-document: Policy document in JSON format (useful because the policies are structured files)An example policy document:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:ListBucketMultipartUploads"
],
"Resource": [
"arn:aws:s3:::<my_bucket>"
]
}
]
}
Output:
{
"Policy": {
"PolicyName":"example-policy",
"PolicyId":"<policy_id>",
"Arn":"arn:aws:iam::<account_number>:policy/example-policy",
"Path":"/",
"DefaultVersionId":"v1",
"AttachmentCount":0,
"PermissionsBoundaryUsageCount":0,
"IsAttachable":true,
"CreateDate":"<creation_date>",
"UpdateDate":"<update_date>"
}
}
Lists IAM policies in the account.
aws iam list-policies --scopes All
iam: Servicelist-policies: Command--scopes: Policies scope. Possible values: All, AWS, Local. AWS is for managed policies, while Local for custom policies.Output
{
"Policies": [
{
"Policy": {
"PolicyName":"example-policy",
"PolicyId":"<policy_id>",
"Arn":"arn:aws:iam::<account_number>:policy/example-policy",
"Path":"/",
"DefaultVersionId":"v1",
"AttachmentCount":0,
"PermissionsBoundaryUsageCount":0,
"IsAttachable":true,
"CreateDate":"<creation_date>",
"UpdateDate":"<update_date>"
}
}
]
}
Edit an IAM policy and set it as default.
aws iam create-policy-version \
--policy-arn arn:aws:iam::123456789012:policy/my-policy \
--policy-document file://NewPolicyVersion.json --set-as-default
iam: Servicecreate-policy-version: Command--policy-arn: ARN of the policy--policy-document: Updated policy fileDelete a policy given the ARN.
aws iam delete-policy --policy-arn arn**:**aws**:**iam**::**123456789012**:**policy/my-policy
iam: Servicedelete-policy: Command--policy-arn: ARN of the policyCreates a new IAM role. The arguments for this command are:
aws iam create-role --role-name example-role --assume-role-policy-document file://assume-policy.json
iam: Servicecreate-role: Command--role-name: Name of the IAM role--assume-role-policy-document: Trust relationship policy document that grants an entity permission to assume the roleIn this example, we will create an IAM role that grants AWS Glue permission to assume the role (as principal).
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "glue.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Output:
{
"Role": {
"Path": "/",
"RoleName": "example-role",
"RoleId": "<role_id>",
"Arn": "arn:aws:iam::<account_number>:role/example-role",
"CreateDate": "<creation_date>",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "glue.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
}
}
Deletes an IAM Role.
aws iam delete-role --role-name Test-Role
iam: Servicedelete-role: Command--role-name: Name of the IAM Role to removeTo allow a User to do some actions, apply a policy to it.
aws iam attach-user-policy --user-name AlessandroArticle --policy-arn arn:aws:iam::<policy_id>:policy/my-policy
iam: Serviceattach-user-policy: Command--user-name: Name of the IAM user--policy-arn: ARN of the IAM policy to attachIn this example, we will attach the IAM policy we created earlier to an example IAM.
We can also attach a policy to a IAM role.
aws iam attach-role-policy --role-name example-role --policy-arn arn:aws:iam::<policy_id>:policy/my-policy
iam: Serviceattach-role-policy: Command--role-name: Name of the IAM role--policy-arn: ARN of the IAM policy you want to attachWe can list all policies attached to an IAM User.
aws iam list-attached-user-policies --user-name AlessandroArticle
iam: Servicelist-attached-user-policies: Command--user-name: The User to whom the policies are attached toOutput
{
"AttachedPolicies": [
{
"PolicyName": "my-policy",
"PolicyArn": "arn:aws:iam::<account_number>:policy/learnaws-dynamo-policy"
}
]
}
List all policies attached to an IAM Role.
aws iam list-attached-role-policies --role-name example-role
iam: Servicelist-attached-role-policies: Command--role-name: The Role to whom the policies are attached toOutput
{
"AttachedPolicies": [
{
"PolicyName": "my-policy",
"PolicyArn": "arn:aws:iam::<account_number>:policy/example-policy"
}
]
}
Output:
{
"UserId": "AROAJQ3ISEWFFR6GXAW:<user_name>",
"Account": "637004329899",
"Arn": "arn:aws:sts::637004329899:assumed-role/<role-name>/<user_name>"
}
Finally, thanks to the excellent tutorial from BlueMatador, here we present some fast snippets that integrate the jq tool to extrapolate useful info for different use-cases. Kudos to them 🙂.
List groups
aws iam list-groups | jq -r .Groups[ ].GroupName
Add/Delete groups
aws iam create-group --group-name (groupName)
List policies and ARNs
aws iam list-policies | jq -r ‘.Policies[ ]|.PolicyName+” “+.Arn’
aws iam list-policies --scope AWS | jq -r ‘.Policies[ ]|.PolicyName+” “+.Arn’
aws iam list-policies --scope Local | jq -r ‘.Policies[ ]|.PolicyName+” “+.Arn’
List user/group/roles for a policy
aws iam list-entities-for-policy --policy-arn arn:aws:iam:2308345:policy/example-ReadOnly
List policies for a group
aws iam list-attached-group-policies --group-name (groupname)
Add policy to a group
aws iam attach-group-policy --group-name (groupname) --policy-arn arn:aws:iam::aws:policy/exampleReadOnlyAccess
Add user to a group
aws iam add-user-to-group --group-name (groupname) --user-name (username)
Remove user from a group
aws iam remove-user-from-group --group-name (groupname) --user-name (username)
List users in a group
aws iam get-group --group-name (groupname)
List groups for a user
aws iam list-groups-for-user --user-name (username)
Attach/detach policy to a group
aws iam attach-group-policy --group-name (groupname) --policy-arn arn:aws:iam::aws:policy/DynamoDBFullAccess
aws iam detach-group-policy --group-name (groupname) --policy-arn arn:aws:iam::aws:policy/DynamoDBFullAccess
This article shows that AWS CLI is a powerful tool for automatic operations on AWS services.
In particular, we have used IAM and STS services to explore all the different commands that we can leverage for Access Management and Identity governance.
We have demonstrated that AWS CLI commands can be chained with other terminal tools to push even further your automation scripts.
Finally, we have seen how jq can be a perfect companion for the CLI to obtain properties out of JSON-formatted files or command results.
If this article interested you, next week we will continue with a new cheatsheet correlated to STS and how it is tied closely to our open-source tool Leapp. Don’t miss it out!
Thank you everyone, for coming this far. We hope that you enjoyed this little “cheatsheet”.
As always, if you have questions, clarifications, or just want to share your opinions, feel free to join our Top of the Ops community.
Until next time, stay safe 🙂!