Example AWS CLI commands for IAM user, permissions, roles, policy

Lets understand AWS CLI for IAM

So you're getting into cloud computing, and AWS (Amazon Web Services) is a big part of that journey! One of the main tools for managing users, permissions, and security in AWS is called IAM (Identity and Access Management). The AWS CLI (Command Line Interface) lets you control IAM with just a few commands. Let’s break it down, nice and easy!

What is IAM?

Imagine you're working on a group project. You might not want everyone to have access to all files, right? Some people just need to read files, while others might need more access. That’s where IAM comes in – it lets you set up users with specific permissions so everyone has the access they need and nothing more.

aws cli iam examples

Setting Up AWS CLI for IAM

First, make sure you have the AWS CLI installed. You can find instructions here.

Once installed, configure it by running:

aws configure

Enter your AWS Access Key, Secret Access Key, Region, and Output format (JSON is good for now). And you're ready to go!

Basic IAM Commands

1. Listing IAM Users

To see all users in your account:

aws iam list-users

This gives you a list of all users. If you only want their usernames, try:

aws iam list-users --query "Users[*].UserName" --output text

2. Creating an IAM User

Want to add a new user? Let’s add a user named Alex:

aws iam create-user --user-name Alex

Now Alex has an account! But they can't do much yet; we’ll add permissions soon.

3. Deleting an IAM User

If Alex no longer needs access, you can delete their user account:

aws iam delete-user --user-name Alex

Working with IAM Roles

Roles are like "job titles" that come with specific permissions. Different people or even services (like an app) can take on these roles to perform tasks.

4. Creating an IAM Role

To create a role, we need a trust policy first. Here’s a simple trust policy that lets AWS Lambda (a service in AWS) assume a role:

Save this JSON in a file called trust-policy.json:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

Now create the role using this command:

aws iam create-role --role-name LambdaRole --assume-role-policy-document file://trust-policy.json

5. Deleting a Role

If you don’t need a role anymore:

aws iam delete-role --role-name LambdaRole

Setting Permissions with Policies

Policies define what a user, group, or role can and cannot do. Let’s explore how to use policies in IAM.

6. Attaching a Policy to a User

To give Alex read-only access to S3 (Amazon's storage service):

aws iam attach-user-policy --user-name Alex --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess

7. Detaching a Policy from a User

If you want to remove this access from Alex:

aws iam detach-user-policy --user-name Alex --policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess

8. Creating a Custom Policy

Let’s create a custom policy that allows listing S3 buckets but not reading or writing:

Save this JSON as list-s3-policy.json:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "*"
    }
  ]
}

Create the policy:

aws iam create-policy --policy-name ListS3Buckets --policy-document file://list-s3-policy.json

Access Keys: For Programmatic Access

If a user needs to log into AWS from code, you can generate access keys.

9. Creating Access Keys for a User

Create access keys for Alex:

aws iam create-access-key --user-name Alex

10. Deleting Access Keys

To delete an access key:

aws iam delete-access-key --user-name Alex --access-key-id 

Advanced Commands

11. Listing Policies Attached to a User, Group, or Role

To see policies attached to a user:

aws iam list-attached-user-policies --user-name Alex

For a group:

aws iam list-attached-group-policies --group-name 

For a role:

aws iam list-attached-role-policies --role-name LambdaRole

12. Listing Inline Policies

Sometimes, policies are directly embedded inside a user, group, or role. These are called inline policies.

To list inline policies for a user:

aws iam list-user-policies --user-name Alex

13. Enabling Multi-Factor Authentication (MFA)

MFA adds security by requiring a one-time code from an app or device.

To list MFA devices linked to a user:

aws iam list-mfa-devices --user-name Alex

Interview questions about IAM

Question 1: What is AWS IAM, and why do we need it?

Answer: Great place to start! AWS IAM is a tool provided by Amazon Web Services that helps us manage access to AWS resources. Think of it like a security guard that decides who can enter a building and what they can do inside.

Example:

Let’s say we have three friends: Alex, Jamie, and Chris. They all want to use your AWS account to work on a project together.

  • Alex only needs to view some files.
  • Jamie can view and edit files.
  • Chris is a manager, so they can view, edit, and delete files.

With IAM, we can set up rules (called policies) for each person, so Alex can only view, Jamie can view and edit, and Chris can do everything. This way, we control who has access to what.

Question 2: What is an IAM Policy, and how does it work?

Answer: An IAM policy is a document (in JSON format) that defines permissions. It basically says what actions are allowed or denied for a specific user or group.

Basic Example:

Here’s a simple IAM policy example that allows a user to only view files in an S3 bucket:

{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": "s3:GetObject",
                "Resource": "arn:aws:s3:::example-bucket/*"
            }
        ]
    }

This policy allows someone to GetObject (view files) in the S3 bucket called “example-bucket.”

Advanced Example:

Let’s say we want to allow the same user to view and upload files but not delete them:

{
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": ["s3:GetObject", "s3:PutObject"],
                "Resource": "arn:aws:s3:::example-bucket/*"
            }
        ]
    }

Question 3: What’s the difference between an IAM User and an IAM Role?

Answer: This is a big one! 🤔

An IAM User is someone who has a login, like a regular user with a username and password. Each user is assigned specific permissions.

An IAM Role is used to give temporary access. Instead of having login credentials, a Role is something a user or an AWS service can assume (borrow) to perform certain tasks.

Example:

Imagine Alex, Jamie, and Chris again. Instead of giving each of them their own login credentials, you give them all a Role that allows them to “borrow” permissions only when they need it. Or, maybe your EC2 (AWS virtual machine) needs access to a database – instead of hardcoding credentials into it, you can assign a Role that lets it access that database temporarily.

Question 4: Can you explain IAM Groups and why we use them?

Answer: IAM Groups are a way to organize users and apply policies to multiple users at once.

Example:

Let’s say you’re managing a team of 50 developers and 10 managers. Instead of setting up permissions for each person individually, you create a Developer Group and a Manager Group.

For the Developer Group, you create a policy that allows viewing and editing files. For the Manager Group, you create a policy that allows viewing, editing, and deleting files. Now, if you hire a new developer, you can just add them to the Developer Group, and they automatically get the correct permissions!

Question 5: What are MFA and its importance in AWS IAM?

Answer: MFA stands for Multi-Factor Authentication. It's a security feature that adds an extra layer of protection by requiring two forms of verification when logging in.

Example:

Let’s say you’re logging into your AWS account. Without MFA, you just enter your password and get access. But with MFA, you enter your password and a code from an app on your phone. Even if someone steals your password, they would also need the code from your phone to log in. This makes it much harder for anyone to hack into your account!

Wrapping Up

And that’s a quick intro to using AWS CLI for IAM! From creating users to setting up roles and policies, this guide should help you with the basics and even some advanced commands. Just practice these commands, and soon managing IAM with AWS CLI will feel like second nature. Happy cloud computing!

Post a Comment

0 Comments