We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

IAM Roles

Okay, so we can give our users (e.g. developers) access to AWS resources... but what happens when we need our application server to be able to access a resource? For example, an EC2 instance that needs to read from an S3 bucket?

We could create a user for our server, but by using a username and password, but we'll eventually encounter this dreadful message:

Luckily, IAM has roles. A role is a temporary identity that can be used or assumed by trusted parties.

The process is simple:

  1. Create a role with a policy that defines what actions it can perform.
  2. Assign the role to a trusted entity, like an EC2 instance.
  3. The entity can then assume the role to get temporary credentials.
  4. The credentials are automatically rotated and expire after a short time, so there's no need to manage them manually.

A trust policy looks a lot like a "regular" policy, with a couple of tweaks:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
  • The Principal field specifies which service or entity can assume the role.
  • The sts:AssumeRole action allows the specified principal to assume the role.

When you select "EC2" as the use case, AWS automatically creates this trust policy for you. The trust policy tells AWS which services can assume this role (in this case, EC2 instances). You can view it later in the role's "Trust relationships" tab.

Assignment

Create an IAM role patientping-ec2-readonly-role for EC2 with the patientping-ec2-readonly policy, then attach the role to your EC2 instance.

Cost check: Creating IAM roles is free; costs arise from service usage.

You should now see the IAM role listed in the instance details.

Run and submit the CLI tests.

Tip

Here's some CLI boilerplate to get you started with IAM roles:

# Create a role with trust policy
aws iam create-role --role-name ROLE-NAME --assume-role-policy-document file://TRUST-POLICY.json

# Attach a policy to the role
aws iam attach-role-policy --role-name ROLE-NAME --policy-arn POLICY-ARN

# Create an instance profile and add the role
aws iam create-instance-profile --instance-profile-name PROFILE-NAME
aws iam add-role-to-instance-profile --instance-profile-name PROFILE-NAME --role-name ROLE-NAME

# Associate the instance profile with an EC2 instance
aws ec2 associate-iam-instance-profile --instance-id INSTANCE-ID --iam-instance-profile Name=PROFILE-NAME