Skip to content

Guide

LAB - Automated Deployment of IAM Groups and Roles

                     CloudFormation
                          │
          ┌───────────────┴────────────────┐
          │                                │
          ▼                                ▼
 ┌───────────────────┐            ┌─────────────────────┐
 │ CloudSecurityGroup│            │ CloudSecurity       │
 │                   │            │ AssumableRole       │
 └─────────┬─────────┘            └──────────┬──────────┘
           │                                 │
           │ attached                        │ Trust Policy
           ▼                                 │
 ┌───────────────────────────┐               │
 │ CloudSecurityManagedPolicy│               │
 │                           │               │
 │ EC2 Describe              │               │
 │ S3 Get/List               │               │
 │ IAM Get/List              │               │
 │ CloudWatch Get/List       │               │
 │                           │               │
 │ sts:AssumeRole ───────────┼──────────────►│
 └─────────────┬─────────────┘               │
               │                             │
               ▼                             ▼
        ┌─────────────┐              ┌────────────────┐
        │ IAM User    │              │ ReadOnlyAccess │
        │             │              │                │
        │ Member of   │──Assume─────►│ Role Session   │
        │ Group       │    Role      │                │
        └─────────────┘              └────────────────┘

Introduction

This lab introduces beginners to different IAM services in AWS.

  • IAM User: It represents a human user, workload that is been used to interact with AWS resources.
  • IAM Groups: An IAM group is a collection of IAM Users. It also allows you to specify permissions for a collection of users in that particular group.
  • IAM Role: Just like an IAM user, Roles do have specific permissions but it is intended to be assumed by anyone who needs it.
  • IAM policies: They are set of rules that define permissions for users, groups, or roles within an AWS account.

Walk through

In this scenerio, after we have deployment the attached Cloudformation in our AWS account. It creates a number of resources which are ABSOLUTELY FREE (No charge). Deployed resources includes:

  • An IAM Group
  • A Custom IAM policy and
  • A Custom AssumeRole

Wait till the Cloudformation deployment completes before proceeding.

This part from here on out would require you have the aws cli installed and access keys imported.

Let's use the AWS CLI to list out the just deployed IAM group. You would also notice that the Users section is blank, so we don't have any IAM user that belongs to this group.

List group

aws iam get-group \
  --group-name CloudSecurityUsers | jq
{
  "Users": [],
  "Group": {
    "Path": "/",
    "GroupName": "CloudSecurityUsers",
    "GroupId": "AGPAZQ3DSY5BXAUFJKSKT",
    "Arn": "arn:aws:iam::276389523671:group/CloudSecurityUsers",
    "CreateDate": "2026-09-11T22:46:42+00:00"
  }
}
The IAM group also has an attached IAM policies (Inline policy) that was configured when we deployed the CloudFormation templates

List group permissions

aws iam list-attached-group-policies \
--group-name CloudSecurityUsers | jq
{
  "AttachedPolicies": [
    {
      "PolicyName": "CloudSecurityManagedPolicy",
      "PolicyArn": "arn:aws:iam::276389523671:policy/CloudSecurityManagedPolicy"
    }
  ]
}
To see what permissions are specified on the group attached policy, we would have to get the Policy version before we can view the listed permissions. Policy version is v2.

Get Policy Version

aws iam get-policy \
> --policy-arn arn:aws:iam::276389523671:policy/CloudSecurityManagedPolicy | jq
{
  "Policy": {
    "PolicyName": "CloudSecurityManagedPolicy",
    "PolicyId": "ANPAZQ3DSY5BYM3IY6IRQ",
    "Arn": "arn:aws:iam::276389523671:policy/CloudSecurityManagedPolicy",
    "Path": "/",
    "DefaultVersionId": "v2",
    "AttachmentCount": 1,
    "PermissionsBoundaryUsageCount": 0,
    "IsAttachable": true,
    "CreateDate": "2026-09-11T22:46:39+00:00",
    "UpdateDate": "2026-09-12T01:50:14+00:00",
    "Tags": []
  }
}
With the Policy version already obtained from our previous commands, we can now get the listed permssions using the iam get-policy-version command. Under the first Allow Statement, there is a few Actions that anyone that belongs to the CloudsecurityUsers can do like:

  • List s3 bucket
  • List all entities in IAM and basically
  • List all entitites in CloudWatch

There is also another Allow Statement that give us the ability to Assume a specific role.

Get Group Policy Metadata

aws iam get-policy-version \
--policy-arn arn:aws:iam::276389523671:policy/CloudSecurityManagedPolicy \
--version-id v2 | jq
{
  "PolicyVersion": {
    "Document": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": [
            "ec2:Describe*",
            "s3:Get*",
            "s3:List*",
            "iam:Get*",
            "iam:List*",
            "cloudwatch:Describe*",
            "cloudwatch:Get*",
            "cloudwatch:List*"
          ],
          "Resource": "*",
          "Effect": "Allow",
          "Sid": "AllowReadOnlyAccess"
        },
        {
          "Action": [
            "sts:AssumeRole"
          ],
          "Resource": [
            "arn:aws:iam::276389523671:role/CloudSecurityAssumableRole"
          ],
          "Effect": "Allow",
          "Sid": "AllowAssumeCloudSecurityRole"
        }
      ]
    },
    "VersionId": "v2",
    "IsDefaultVersion": true,
    "CreateDate": "2026-09-12T01:50:14+00:00"
  }
}
Let's look more closely to that Assume Role. Under the Principal, I can see the aws arn that ends with :root. This is not the root user of the AWS account but it signifies every user in that particular account can assume this role.

No Conditions on this role? This is bad security practice, because it is basically open for everyone including an attacker in our environment to escalate his privileges.

Get the Assume Role Policy

aws iam get-role \
  --role-name CloudSecurityAssumableRole | jq
{
  "Role": {
    "Path": "/",
    "RoleName": "CloudSecurityAssumableRole",
    "RoleId": "AROAZQ3DSY5BV5WD4RZVN",
    "Arn": "arn:aws:iam::276389523671:role/CloudSecurityAssumableRole",
    "CreateDate": "2026-09-11T22:46:40+00:00",
    "AssumeRolePolicyDocument": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "AllowGroupUsersToAssumeRole",
          "Effect": "Allow",
          "Principal": {
            "AWS": "arn:aws:iam::276389523671:root"
          },
          "Action": "sts:AssumeRole"
        }
      ]
    },
    "Description": "",
    "MaxSessionDuration": 3600,
    "RoleLastUsed": {}
  }
}

Create an IAM User

aws iam create-user --user-name cynthia.morgan | jq
{
  "User": {
    "Path": "/",
    "UserName": "cynthia.morgan",
    "UserId": "AIDAZQ3DSY5B7IGBH37BR",
    "Arn": "arn:aws:iam::276389523671:user/cynthia.morgan",
    "CreateDate": "2026-09-11T23:51:15+00:00"
  }
}

Add New User to the New IAM Group

aws iam add-user-to-group \
--group-name CloudSecurityUsers \
--user-name cynthia.morgan | jq

Create an Access Key for the new user (Cynthia.morgan)

aws iam create-access-key --user-name cynthia.morgan | jq
{
  "AccessKey": {
    "UserName": "cynthia.morgan",
    "AccessKeyId": "<< Redacted >>",
    "Status": "Active",
    "SecretAccessKey": "<< Redacted >>",
    "CreateDate": "2026-09-11T23:54:43+00:00"
  }
}

Create an AWS profile for the new user

aws configure --profile cynthia

Tip: You can deliver temporary credentials to the AWS CLI using your AWS Console session by running the command 'aws login'.

AWS Access Key ID [None]: < Redacted >
AWS Secret Access Key [None]: < Redacted >
Default region name [None]: us-east-1
Default output format [None]: json

# whomai
aws sts get-caller-identity --profile cynthia | jq
{
  "UserId": "AIDAZQ3DSY5B7IGBH37BR",
  "Account": "276389523671",
  "Arn": "arn:aws:iam::276389523671:user/cynthia.morgan"
}

Check whether the user has access to view Cloudtrail trails

aws cloudtrail list-trails --output json --profile cynthia | jq

aws: [ERROR]: An error occurred (AccessDeniedException) when calling the ListTrails operation: User: arn:aws:iam::654654490435:user/cynthia.morgan is not authorized to perform: cloudtrail:ListTrails because no identity-based policy allows the cloudtrail:ListTrails action

Assumed Role by New User

aws sts assume-role \
> --role-arn arn:aws:iam::276389523671:role/CloudSecurityAssumableRole \
> --role-session-name CloudSecurityLab | jq
{
  "Credentials": {
    "AccessKeyId": "<< REDACTED >>",
    "SecretAccessKey": "<< REDACTED >>",
    "SessionToken": "<< REDACTED >>",
    "Expiration": "2026-09-12T00:57:35+00:00"
  },
  "AssumedRoleUser": {
    "AssumedRoleId": "AROAZQ3DSY5BV5WD4RZVN:CloudSecurityLab",
    "Arn": "arn:aws:sts::276389523671:assumed-role/CloudSecurityAssumableRole/CloudSecurityLab"
  },
  "PackedPolicySize": 18
}
export AWS_ACCESS_KEY_ID= << REDACTED >>
export AWS_SECRET_ACCESS_KEY= << REDACTED >>
export AWS_SESSION_TOKEN=<< REDACTED >>
aws cloudtrail list-trails --output json | jq
{
  "Trails": [
    {
      "TrailARN": "arn:aws:cloudtrail:us-east-1:276389523671:trail/aws-controltower-BaselineCloudTrail",
      "Name": "aws-controltower-BaselineCloudTrail",
      "HomeRegion": "us-east-1"
    }
  ]
}

Clean up

  • Remove newly created user from group
    aws iam remove-user-from-group \
        --group-name CloudSecurityUsers \
        --user-name cynthia.morgan | jq
    
  • List access key for user and delete it

    aws iam list-access-keys \
    > --user-name cynthia.morgan | jq
    {
      "AccessKeyMetadata": [
        {
          "UserName": "cynthia.morgan",
          "AccessKeyId": "AKIAZQ3DSY5BS4UJQEWP",
          "Status": "Active",
          "CreateDate": "2026-09-11T23:54:43+00:00"
        }
      ]
    }
    
    # Delete it
    aws iam delete-access-key \
    --access-key-id AKIAZQ3DSY5BS4UJQEWP \
    --user-name cynthia.morgan | jq
    

  • Finally delete user

    aws iam delete-user \
    --user-name cynthia.morgan | jq
    

  • Delete stack from CloudFormation