IAM Roles for Amazon EC2
Applications must sign their API requests with AWS credentials. Therefore, if you are an application developer, you need a strategy for managing credentials for your applications that run on EC2 instances. For example, you can securely distribute your AWS credentials to the instances, enabling the applications on those instances to use your credentials to sign requests, while protecting them from other users. However, it's challenging to securely distribute credentials to each instance, especially those that AWS creates on your behalf, such as Spot instances or instances in Auto Scaling groups. You must also be able to update the credentials on each instance when you rotate your AWS credentials.
We designed IAM roles so that your applications can securely make API requests from your instances, without requiring you to manage the security credentials that the applications use. Instead of creating and distributing your AWS credentials, you can delegate permission to make API requests using IAM roles as follows:
Create an IAM role.
Define which accounts or AWS services can assume the role.
Define which API actions and resources the application can use after assuming the role.
Specify the role when you launch your instances.
Have the application retrieve a set of temporary credentials and use them.
For example, you can use IAM roles to grant permissions to applications running on your instances that needs to use a bucket in Amazon S3.
Note
Amazon EC2 uses an instance profile as a container for an IAM role. When you create an IAM role using the console, the console creates an instance profile automatically and gives it the same name as the role it corresponds to. If you use the AWS CLI, API, or an AWS SDK to create a role, you create the role and instance profile as separate actions, and you might give them different names. To launch an instance with an IAM role, you specify the name of its instance profile. When you launch an instance using the Amazon EC2 console, you can select a role to associate with the instance; however, the list that's displayed is actually a list of instance profile names. For more information, see Instance Profiles in the IAM User Guide.
You can specify permissions for IAM roles by creating a policy in JSON format. These are similar to the policies that you create for IAM users. If you make a change to a role, the change is propagated to all instances, simplifying credential management.
Note
You can't assign a role to an existing instance; you can only specify a role when you launch a new instance.
For more information about creating and using IAM roles, see Roles in the IAM User Guide.
Topics
- Retrieving Security Credentials from Instance Metadata
- Granting an IAM User Permission to Launch an Instance with an IAM Role
- Creating an IAM Role Using the Console
- Launching an Instance with an IAM Role Using the Console
- Creating an IAM Role Using the AWS CLI
- Launching an Instance with an IAM Role Using the AWS CLI
Retrieving Security Credentials from Instance Metadata
An application on the instance retrieves the security credentials provided by the role
from the instance metadata item
iam/security-credentials/
role-name. The application is
granted the permissions for the actions and resources that you've defined for the role
through the security credentials associated with the role. These security credentials are
temporary and we rotate them automatically. We make new credentials available at least five
minutes prior to the expiration of the old credentials.
Warning
If you use services that use instance metadata with IAM roles, ensure that you don't expose your credentials when the services make HTTP calls on your behalf. The types of services that could expose your credentials include HTTP proxies, HTML/CSS validator services, and XML processors that support XML inclusion.
The following command retrieves the security credentials for an IAM role named
s3access
.
$
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/s3access
The following is example output.
{
"Code" : "Success",
"LastUpdated" : "2012-04-26T16:39:16Z",
"Type" : "AWS-HMAC",
"AccessKeyId" : "AKIAIOSFODNN7EXAMPLE",
"SecretAccessKey" : "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"Token" : "token",
"Expiration" : "2012-04-27T22:39:16Z"
}
For applications, AWS CLI, and Tools for Windows PowerShell commands that run on the instance, you do not have to explicitly get the temporary security credentials — the AWS SDKs, AWS CLI, and Tools for Windows PowerShell automatically get the credentials from the EC2 instance metadata service and use them. To make a call outside of the instance using temporary security credentials (for example, to test IAM policies), you must provide the access key, secret key, and the session token. For more information, see Using Temporary Security Credentials to Request Access to AWS Resources in the IAM User Guide.
For more information about instance metadata, see Instance Metadata and User Data.
Granting an IAM User Permission to Launch an Instance with an IAM Role
To enable an IAM user to launch an instance with an IAM role, you must grant the user permission to pass the role to the instance.
For example, the following IAM policy grants users permission to launch an instance
with the IAM role named s3access
.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::123456789012:role/s3access"
}]
}
Alternatively, you could grant IAM users access to all your roles by specifying the resource as "*" in this policy. However, consider whether users who launch instances with your roles (ones that exist or that you'll create later on) might be granted permissions that they don't need or shouldn't have.
For more information, see Permissions Required for Using Roles with Amazon EC2 in the IAM User Guide.
Creating an IAM Role Using the Console
You must create an IAM role before you can launch an instance with that role.
To create an IAM role using the IAM console
Sign in to the Identity and Access Management (IAM) console at https://console.aws.amazon.com/iam/.
In the navigation pane, choose Roles, and then choose Create New Role.
On the Set Role Name page, enter a name for the role and choose Next Step.
On the Select Role Type page, choose Select next to Amazon EC2.
On the Attach Policy page, select an AWS managed policy. For example, for Amazon EC2, one of the following AWS managed policies might meet your needs:
PowerUserAccess
ReadOnlyAccess
AmazonEC2FullAccess
AmazonEC2ReadOnlyAccess
Review the role information, edit the role as needed, and then choose Create Role.
Launching an Instance with an IAM Role Using the Console
After you've created an IAM role, you can launch an instance, and associate that role with the instance during launch.
Important
After you create an IAM role, it may take several seconds for the permissions to propagate. If your first attempt to launch an instance with a role fails, wait a few seconds before trying again. For more information, see Troubleshooting Working with Roles in the IAM User Guide.
To launch an instance with an IAM role
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
On the dashboard, choose Launch Instance.
Select an AMI, then select an instance type and choose Next: Configure Instance Details.
On the Configure Instance Details page, select the IAM role you created from the IAM role list.
Note
The IAM role list displays the name of the instance profile that you created when you created your IAM role. If you created your IAM role using the console, the instance profile was created for you and given the same name as the role. If you created your IAM role using the AWS CLI, API, or an AWS SDK, you may have named your instance profile differently.
Configure any other details, then follow the instructions through the rest of the wizard, or choose Review and Launch to accept default settings and go directly to the Review Instance Launch page.
Review your settings, then choose Launch to choose a key pair and launch your instance.
If you are using the Amazon EC2 API actions in your application, retrieve the AWS security credentials made available on the instance and use them to sign the requests. Note that the AWS SDK does this for you.
$
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/
role_name
Creating an IAM Role Using the AWS CLI
You must create an IAM role before you can launch an instance with that role.
To create an IAM role using the AWS CLI
Create an IAM role with a policy that allows the role to use an Amazon S3 bucket.
Create the following trust policy and save it in a text file named
ec2-role-trust-policy.json
.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com"}, "Action": "sts:AssumeRole" } ] }
Create the
s3access
role and specify the trust policy that you created.$
aws iam create-role --role-name s3access --assume-role-policy-document file://ec2-role-trust-policy.json
{ "Role": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" } } ] }, "RoleId": "AROAIIZKPBKS2LEXAMPLE", "CreateDate": "2013-12-12T23:46:37.247Z", "RoleName": "s3access", "Path": "/", "Arn": "arn:aws:iam::123456789012:role/s3access" } }Create an access policy and save it in a text file named
ec2-role-access-policy.json
. For example, this policy grants administrative permissions for Amazon S3 to applications running on the instance.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": ["s3:*"], "Resource": ["*"] } ] }
Attach the access policy to the role.
$
aws iam put-role-policy --role-name s3access --policy-name S3-Permissions --policy-document file://ec2-role-access-policy.json
Create an instance profile named
s3access-profile
.$
aws iam create-instance-profile --instance-profile-name s3access-profile
{ "InstanceProfile": { "InstanceProfileId": "AIPAJTLBPJLEGREXAMPLE", "Roles": [], "CreateDate": "2013-12-12T23:53:34.093Z", "InstanceProfileName": "s3access-profile", "Path": "/", "Arn": "arn:aws:iam::123456789012:instance-profile/s3access-profile" } }Add the
s3access
role to thes3access-profile
instance profile.$
aws iam add-role-to-instance-profile --instance-profile-name s3access-profile --role-name s3access
For more information about these commands, see create-role, put-role-policy, and create-instance-profile in the AWS Command Line Interface Reference.
Launching an Instance with an IAM Role Using the AWS CLI
After you've created an IAM role, you can launch an instance, and associate that role with the instance during launch.
Important
After you create an IAM role, it may take several seconds for the permissions to propagate. If your first attempt to launch an instance with a role fails, wait a few seconds before trying again. For more information, see Troubleshooting Working with Roles in the IAM User Guide.
To launch an instance with an IAM role using the AWS CLI
Launch an instance using the instance profile. The following example shows how to launch an instance with the instance profile.
$
aws ec2 run-instances --image-id ami-11aa22bb --iam-instance-profile Name="s3access-profile" --key-name my-key-pair --security-groups my-security-group --subnet-id subnet-1a2b3c4d
For more information, see run-instances in the AWS Command Line Interface Reference.
If you are using the Amazon EC2 API actions in your application, retrieve the AWS security credentials made available on the instance and use them to sign the requests. Note that the AWS SDK does this for you.
$
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/
role_name