EC2 Roles and Instance Profiles
Using EC2 Roles and Instance Profiles in AWS
Overview
AWS Identity and Access Management (IAM) roles for Amazon Elastic Compute Cloud (EC2) provide the ability to grant instances temporary credentials. These temporary credentials can then be used by hosted applications to access permissions configured within the role. IAM roles eliminate the need for managing credentials, help mitigate long-term security risks, and simplify permissions management. Prerequisites for this lab include understanding how to log in to and use the AWS Management Console, EC2 basics (including how to launch an instance), IAM basics (including users, policies, and roles), and how to use the AWS CLI.

Create a Trust Policy and Role Using the AWS CLI
Log in to Bastion Host and Set the AWS CLI Region and Output Type
Navigate to EC2 > Instances.
Copy the public IP of the Bastion Host instance.
Open a terminal, and log in to the bastion host via SSH:
ssh cloud_user@<BASTION_HOST_PUBLIC_IP>Enter the password provided for it on the lab page.
Run the following command:
[cloud_user@bastion]$ aws configurePress Enter twice to leave the AWS Access Key ID and AWS Secret Access Key blank.
Enter us-east-1 as the default region name.
Enter json as the default output format.
Create IAM Trust Policy for an EC2 Role
Create a file called
trust_policy_ec2.json:[cloud_user@bastion]$ vim trust_policy_ec2.jsonPaste in the following content:
Save the file
Create the DEV_ROLE IAM Role
DEV_ROLE IAM RoleRun the following AWS CLI command:
Create an IAM Policy Defining Read-Only Access Permissions to an S3 Bucket
Create a file called
dev_s3_read_access.json:[cloud_user@bastion]$ vim dev_s3_read_access.jsonEnter the following content, replacing
<DEV_S3_BUCKET_NAME>with the bucket name:Create the managed policy called
DevS3ReadAccess:Copy the policy ARN from the output
Create Instance Profile and Attach Role to an EC2 Instance
Attach Managed Policy to Role
Attach the managed policy to the role, replacing
<DevS3ReadAccess_POLICY_ARN>with the ARN you just copied:Verify the managed policy was attached:
Create the Instance Profile and Add the DEV_ROLE via the AWS CLI
DEV_ROLE via the AWS CLICreate instance profile named
DEV_PROFILE:Add role to the
DEV_PROFILEcalled DEV_ROLE:Verify the configuration:
Attach the DEV_PROFILE Role to an Instance
DEV_PROFILE Role to an InstanceIn the AWS console, navigate to EC2 > Instances.
Copy the instance ID of the instance named Web Server instance — we'll need it in a second.
In the terminal, attach the
DEV_PROFILEto an EC2 instance, replacing<LAB_WEB_SERVER_INSTANCE_ID>with the Web Server instance ID you just copied:Verify the configuration (be sure to replace
<LAB_WEB_SERVER_INSTANCE_ID>with the Web Server instance ID again):This command's output should show this instance is using
DEV_PROFILEas anIamInstanceProfile. Verify this by locating theIamInstanceProfilesection in the output, and look below to make sure the"Arn"ends in/DEV_PROFILE.
Test S3 Permissions via the AWS CLI
In the AWS console, copy the public IP of the Web Server instance.
Open a new terminal.
Log in to the web server instance via SSH:
Use the same password for the bastion host provided on the lab page.
Verify the instance is assuming the
DEV_ROLErole:We should see
DEV_ROLEin theArn.List the buckets in the account:
Copy the entire name (starting with
cfst) of the bucket withs3bucketdevin its name.Attempt to view the files in the
s3bucketdev-bucket, replacing<s3bucketdev-123>with the bucket name you just copied:We should see a list of files.
Create an IAM Policy and Role Using the AWS Management Console
Create Policy
In the AWS console, navigate to IAM > Policies.
Click Create policy.
Click the JSON tab.
Paste the following text as the policy, replacing
<PROD_S3_BUCKET_NAME>with the bucket name:Click Next: Tags.
Click Next: Review.
Enter ProdS3ReadAccess as the policy name.
Click Create policy.
Create Role
Click Roles in the left-hand menu.
Click Create role.
Under Choose a use case, select EC2.
Click Next: Permissions.
In the Filter policies search box, enter ProdS3ReadAccess.
Click the checkbox to select ProdS3ReadAccess.
Click Next: Tags.
Click Next: Review.
Give it a Role name of PROD_ROLE.
Click Create role.
Attach IAM Role to an EC2 Instance Using the AWS Management Console
Navigate to EC2 > Instances.
Select the Web Server instance.
Click Actions > Security > Modify IAM role.
In the IAM role dropdown, select PROD_ROLE.
Click Save.
Test the Configuration
Open the existing terminal connected to the Web Server instance. (You may need to reconnect if you've been disconnected.)
Determine the identity currently being used:
This time, we should see
PROD_ROLEin theArn.List the buckets:
Copy the entire name (starting with
cfst) of the bucket withs3bucketprodin its name.Attempt to view the files in the
s3bucketprod-bucket, replacing<s3bucketprod-123>with the bucket name you just copied:It should list the files.
In the
aws s3 lscommand output, copy the entire name (starting withcfst) of the bucket withs3bucketsecretin its name.Attempt to view the files in the
<s3bucketsecret-123>bucket, replacing<s3bucketsecret-123>with the bucket name you just copied:This time, our access will be denied — which means our configuration is properly set up.
Last updated