AWS IAM Tutorial


AWS Identity and Access Management (IAM) is a crucial service that allows you to securely control access to AWS resources. IAM helps you manage users, groups, roles, and permissions for your AWS environment, ensuring that only authorized entities can perform specific actions on your resources.

Whether you’re an administrator setting up new users or a developer managing application permissions, understanding IAM is essential for managing AWS securely and efficiently. In this tutorial, we will explore IAM’s key features, components, and how you can use it to control access in your AWS environment.


What is AWS IAM?

AWS Identity and Access Management (IAM) is a web service that helps you securely manage access to AWS resources. IAM enables you to define who can access your AWS resources (users and groups) and what actions they can perform (permissions).

Key functions of AWS IAM include:

  • Managing user identities.
  • Assigning permissions to users and resources.
  • Creating roles for temporary access and applications.
  • Auditing access activity using CloudTrail.

IAM is free to use; however, you may incur costs when using other AWS resources in conjunction with IAM, like AWS Lambda, Amazon EC2, and Amazon S3.


How Does AWS IAM Work?

At its core, IAM works by associating specific permissions with an identity (a user or a role). It defines who can access which AWS services and resources and what actions they can perform. Here's how IAM manages access:

  1. Users: IAM users are individual accounts with their own credentials (username and password). Each user is associated with specific permissions.

  2. Groups: A group is a collection of IAM users. Permissions are granted to groups, and users inherit those permissions by being members of the group.

  3. Roles: IAM roles are designed for applications or services that need to access AWS resources. A role can be assumed by users or services, allowing temporary access to resources with specific permissions.

  4. Policies: Policies are JSON documents that define the permissions granted to IAM users, groups, and roles. Policies specify what actions can be performed on what resources and under what conditions.


IAM Components

Understanding IAM components is essential to managing access in AWS. Below are the key IAM components:

1. IAM Users

IAM users represent individuals or applications that need access to AWS resources. Each user is assigned specific permissions through policies attached to the user or through group membership.

  • Example: You could have an IAM user for an employee who needs access to specific AWS services like Amazon S3 and EC2.

Sample Command to Create a User (using AWS CLI):

aws iam create-user --user-name newUser

2. IAM Groups

IAM groups help you manage multiple users at once. Rather than assigning permissions to each user individually, you can assign permissions to a group and then add users to the group.

  • Example: You might have a group called "Admins" for users who need full access to all AWS resources, and another group called "Developers" for users who only need access to Amazon EC2 and Lambda.

Sample Command to Create a Group:

aws iam create-group --group-name Developers

3. IAM Roles

IAM roles are used to delegate access to AWS services and resources. Unlike users, roles are not assigned to individuals but are assumed by authorized users, groups, or AWS services.

  • Example: An EC2 instance might need access to an S3 bucket. You can assign an IAM role to the instance that grants permission to access the S3 bucket.

Sample Command to Create a Role:

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

4. IAM Policies

Policies are JSON documents that define what actions are allowed or denied on specific resources. Policies are attached to users, groups, or roles to grant permissions.

  • Example: A policy can grant a user read access to a specific S3 bucket but deny them write access.

Sample Policy (JSON format):

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

Features of AWS IAM

1. Fine-Grained Access Control

IAM allows you to specify who can access specific resources and what actions they can perform on those resources. You can define permissions for services like EC2, S3, DynamoDB, Lambda, and more, down to individual API actions.

2. Multi-Factor Authentication (MFA)

To enhance security, IAM supports multi-factor authentication (MFA). When enabled, users must provide an additional authentication factor (such as a mobile device or hardware token) to access AWS resources.

3. Temporary Security Credentials

IAM roles can be used to grant temporary access to AWS resources. This is useful for cross-account access, federated users, and applications that require temporary permissions.

Example: You could grant a third-party service temporary access to an S3 bucket by assigning a role with a predefined expiration time.

4. Identity Federation

AWS IAM supports federating identities from external identity providers (such as Google or Active Directory). This allows users to access AWS resources using their existing credentials.

5. Integrated with AWS CloudTrail

IAM works seamlessly with AWS CloudTrail, which logs all AWS API requests. This helps you monitor and track actions performed by users, making it easier to audit and maintain security compliance.


How to Manage IAM Users, Groups, and Roles

Creating a New IAM User

To create a new IAM user, you can use the AWS Management Console or the AWS CLI. Below is an example using the CLI:

  1. Create the user:
    aws iam create-user --user-name JohnDoe
    
  2. Create a policy to assign permissions to the user:
    {
       "Version": "2012-10-17",
       "Statement": [
          {
             "Effect": "Allow",
             "Action": "s3:*",
             "Resource": "arn:aws:s3:::my-bucket/*"
          }
       ]
    }
    
  3. Attach the policy to the user:
    aws iam put-user-policy --user-name JohnDoe --policy-name S3AccessPolicy --policy-document file://s3-policy.json
    

Creating an IAM Group and Adding Users

To create a group and add users to it, use the following steps:

  1. Create a group:
    aws iam create-group --group-name Developers
    
  2. Add a user to the group:
    aws iam add-user-to-group --user-name JohnDoe --group-name Developers
    

Creating an IAM Role and Attaching Policies

To create an IAM role and assign a policy to it, follow these steps:

  1. Create the role with a trust policy:
    aws iam create-role --role-name EC2Role --assume-role-policy-document file://trust-policy.json
    
  2. Attach a policy to the role:
    aws iam attach-role-policy --role-name EC2Role --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
    

Best Practices for Using AWS IAM

  • Use Least Privilege Principle: Only grant the permissions necessary for users to perform their job functions.
  • Enable MFA for All Users: Multi-factor authentication adds an extra layer of security.
  • Use IAM Roles for Services: Instead of hardcoding credentials, assign IAM roles to services like EC2 or Lambda for temporary, secure access.
  • Monitor with CloudTrail: Always track IAM activities using CloudTrail to maintain security and compliance.
  • Regularly Review and Rotate Keys: Periodically review your IAM policies and rotate access keys to ensure that your environment remains secure.