Using temporary credentials with Amazon resources
You can use temporary security credentials to make programmatic requests for Amazon resources
using the Amazon CLI or Amazon API (using the Amazon SDKs
-
When you make a call using temporary security credentials, the call must include a session token, which is returned along with those temporary credentials. Amazon uses the session token to validate the temporary security credentials.
-
Temporary credentials expire after a specified interval. After temporary credentials expire, any calls that you make with those credentials will fail, so you must generate a new set of temporary credentials. Temporary credentials cannot be extended or refreshed beyond the original specified interval.
-
When you use temporary credentials to make a request, your principal might include a set of tags. These tags come from session tags and tags that are attached to the role that you assume. For more information about session tags, see Passing session tags in Amazon STS.
If you are using the Amazon SDKs
Note
You can use Amazon Security Token Service (Amazon STS) to create and provide trusted users with temporary security
credentials that can control access to your Amazon resources. For more information about Amazon STS,
see Temporary security credentials in IAM. Amazon STS is a
global service that has a default endpoint at https://sts.amazonaws.com.cn
.
This endpoint is in the US East (N. Virginia) Region, although credentials that you get from this and other
endpoints are valid globally. These credentials work with services and resources in any
Region. You can also choose to make Amazon STS API calls to endpoints in any of the supported
Regions. This can reduce latency by making the requests from servers in a Region that is
geographically closer to you. No matter which Region your credentials come from, they work
globally. For more information, see Managing Amazon STS in an Amazon Web Services Region.
Contents
Using temporary credentials in Amazon EC2 instances
If you want to run Amazon CLI commands or code inside an EC2 instance, the recommended way to get credentials is to use roles for Amazon EC2. You create an IAM role that specifies the permissions that you want to grant to applications that run on the EC2 instances. When you launch the instance, you associate the role with the instance.
Applications, Amazon CLI, and Tools for Windows PowerShell commands that run on the instance can then get automatic temporary security credentials from the instance metadata. You do not have to explicitly get the temporary security credentials. The Amazon SDKs, Amazon CLI, and Tools for Windows PowerShell automatically get the credentials from the EC2 Instance Metadata Service (IMDS) and use them. The temporary credentials have the permissions that you define for the role that is associated with the instance.
For more information and for examples, see the following:
-
Using IAM Roles to Grant Access to Amazon Resources on Amazon Elastic Compute Cloud — Amazon SDK for Java
-
Granting Access Using an IAM Role — Amazon SDK for .NET
-
Creating a Role — Amazon SDK for Ruby
Using temporary security credentials with the Amazon SDKs
To use temporary security credentials in code, you programmatically call an Amazon STS API
like AssumeRole
and extract the resulting credentials and session token. You then
use those values as credentials for subsequent calls to Amazon. The following example shows
pseudocode for how to use temporary security credentials if you're using an Amazon SDK:
assumeRoleResult = AssumeRole(
role-arn
); tempCredentials = new SessionAWSCredentials( assumeRoleResult.AccessKeyId, assumeRoleResult.SecretAccessKey, assumeRoleResult.SessionToken); s3Request = CreateAmazonS3Client(tempCredentials);
For an example written in Python (using the Amazon SDK for Python (Boto)AssumeRole
to get temporary security credentials and then use those credentials
to make a call to Amazon S3.
For details about how to call AssumeRole
, GetFederationToken
,
and other API operations, see the Amazon Security Token Service API Reference. For
information on getting the temporary security credentials and session token from the result,
see the documentation for the SDK that you're working with. You can find the documentation for
all the Amazon SDKs on the main Amazon
documentation page
You must make sure that you get a new set of credentials before the old ones expire. In some SDKs, you can use a provider that manages the process of refreshing credentials for you; check the documentation for the SDK you're using.
Using temporary security credentials with the Amazon CLI
You can use temporary security credentials with the Amazon CLI. This can be useful for testing policies.
Using the Amazon CLI, you can call an Amazon STS API like AssumeRole
or
GetFederationToken
and then capture the resulting output. The following example
shows a call to AssumeRole
that sends the output to a file. In the example, the
profile
parameter is assumed to be a profile in the Amazon CLI configuration file.
It is also assumed to reference credentials for an IAM user who has permissions to assume
the role.
aws sts assume-role --role-arn arn:aws-cn:iam::123456789012:role/
role-name
--role-session-name "RoleSession1" --profileIAM-user-name
> assume-role-output.txt
When the command is finished, you can extract the access key ID, secret access key, and session token from wherever you've routed it. You can do this either manually or by using a script. You can then assign these values to environment variables.
When you run Amazon CLI commands, the Amazon CLI looks for credentials in a specific
order—first in environment variables and then in the configuration file. Therefore,
after you've put the temporary credentials into environment variables, the Amazon CLI uses those
credentials by default. (If you specify a profile
parameter in the command, the
Amazon CLI skips the environment variables. Instead, the Amazon CLI looks in the configuration file,
which lets you override the credentials in the environment variables if you need to.)
The following example shows how you might set the environment variables for temporary
security credentials and then call an Amazon CLI command. Because no profile
parameter
is included in the Amazon CLI command, the Amazon CLI looks for credentials first in environment
variables and therefore uses the temporary credentials.
Linux
$
export AWS_ACCESS_KEY_ID=ASIAIOSFODNN7EXAMPLE
$
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
$
export AWS_SESSION_TOKEN=AQoDYXdzEJr...<remainder of session token>
$
aws ec2 describe-instances --region us-west-1
Windows
C:\>
SET AWS_ACCESS_KEY_ID=ASIAIOSFODNN7EXAMPLE
C:\>
SET AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
C:\>
SET AWS_SESSION_TOKEN=AQoDYXdzEJr...<remainder of token>
C:\>
aws ec2 describe-instances --region us-west-1
Using temporary security credentials with API operations
If you're making direct HTTPS API requests to Amazon, you can sign those requests with the
temporary security credentials that you get from the Amazon Security Token Service (Amazon STS). To do this, you use
the access key ID and secret access key that you receive from Amazon STS. You use the access key ID
and secret access key the same way you would use long-term credentials to sign a request. You
also add to your API request the session token that you receive from Amazon STS. You add the
session token to an HTTP header or to a query string parameter named
X-Amz-Security-Token
. You add the session token to the HTTP header
or the query string parameter, but not both. For more information about
signing HTTPS API requests, see Signing Amazon API Requests in the Amazon Web Services General Reference.
More information
For more information about using Amazon STS with other Amazon services, see the following links:
-
Amazon S3. See Making requests using IAM user temporary credentials or Making requests using federated user temporary credentials in the Amazon Simple Storage Service User Guide .
-
Amazon SNS. See Using Temporary Security Credentials in the Amazon Simple Notification Service Developer Guide.
-
Amazon SQS. See Using identity-based policies with Amazon SNS in the Amazon Simple Queue Service Developer Guide.
-
Amazon SimpleDB. See Identity and access management in Amazon SQS in the Amazon SimpleDB Developer Guide.