IAM tutorial: Use an Amazon CloudFormation template to create a SAML federated IAM role
When you have an existing SAML Identity Provider (IdP) configured in your Amazon account, you can create federated IAM roles that trust that IdP. This tutorial shows you how to use an Amazon CloudFormation template to create a SAML federated IAM role that can be assumed by users authenticated through your external IdP.
The template creates a federated IAM role with a trust policy that allows your SAML IdP to assume the role. Users authenticated by your external IdP can assume this role to access Amazon resources based on the permissions attached to the role.
The deployed resource consists of the following:
-
A federated IAM role that trusts your existing SAML IdP.
-
Configurable managed policies that can be attached to the role to grant specific permissions.
-
Optional permissions boundary and session duration settings.
Prerequisites
This tutorial assumes that you have the following already in place:
-
An existing SAML IdP configured in your Amazon account. If you don't have one, you can create it using the IAM tutorial: Use an Amazon CloudFormation template to create a SAML Identity Provider (IdP) tutorial.
-
The ARN of your SAML IdP, which you'll need to specify as a parameter when creating the stack.
-
Python 3.6 or later installed on your local machine to run the Python command used in this tutorial for formatting your IdP's SAML metadata XML file.
Create a SAML federated role using Amazon CloudFormation
To create the SAML federated role, you'll create an CloudFormation template and use it to create a stack containing the role.
Create the template
First, create the CloudFormation template.
-
In the Template section, click the copy icon on the JSON or YAML tab to copy the template contents.
-
Paste the template contents into a new file.
-
Save the file locally.
Create the stack
Next, use the template you've saved to provision a CloudFormation stack.
-
Open the Amazon CloudFormation console at https://console.amazonaws.cn/cloudformation
. -
On the Stacks page, from the Create stack menu, choose with new resources (standard).
-
Specify the template:
-
Under Prerequisite, choose Choose an existing template.
-
Under Specify template, choose Upload a template file.
-
Choose Choose file, navigate to the template file, and choose it.
-
Choose Next.
-
-
Specify the following stack details:
-
Enter a stack name.
-
For SAMLProviderARN, enter the ARN of your existing SAML IdP. This should be in the format
arn:aws:iam::123456789012:saml-provider/YourProviderName
.Example:
arn:aws:iam::123456789012:saml-provider/CompanyIdP
Note
If you created your SAML IdP using the IAM tutorial: Use an Amazon CloudFormation template to create a SAML Identity Provider (IdP) tutorial, you can find the provider ARN in the Outputs tab of that CloudFormation stack.
-
For RoleName, you can leave this empty to auto-generate a name based on the stack name, or enter a custom name for the IAM role.
Example:
SAML-Developer-Access
orSAML-ReadOnly-Role
-
For other parameters, accept the default values or enter your own based on your requirements:
-
RoleSessionDuration - Maximum session duration in seconds (3600-43200, default 7200)
Example:
14400
(4 hours) -
RolePermissionsBoundary - Optional ARN of a permissions boundary policy
Example:
arn:aws:iam::123456789012:policy/DeveloperBoundary
-
RolePath - Path for the IAM role (default is /)
Example:
/saml-roles/
-
ManagedPolicy1-5 - Optional ARNs of up to 5 managed policies to attach
Example for ManagedPolicy1:
arn:aws:iam::aws:policy/ReadOnlyAccess
Example for ManagedPolicy2:
arn:aws:iam::123456789012:policy/CustomPolicy
-
-
Choose Next.
-
-
Configure the stack options:
-
Under Stack failure options, choose Delete all newly created resources.
Note
Choosing this option prevents you from possibly being billed for resources whose deletion policy specifies they be retained even if the stack creation fails.
-
Accept all other default values.
-
Under Capabilities, check the box to acknowledge that CloudFormation might create IAM resources in your account.
-
Choose Next.
-
-
Review the stack details and choose Submit.
Amazon CloudFormation creates the stack. Once the stack creation is complete, the stack resources are ready to use. You can use the Resources tab on the stack detail page to view the resources that were provisioned in your account.
The stack will output the following value, which you can view on the Outputs tab:
-
RoleARN: The ARN of the created IAM role (for example,
arn:aws:iam::123456789012:role/SAML-Developer-Access
orarn:aws:iam::123456789012:role/stack-name-a1b2c3d4
if using auto-generated name).
You'll need this role ARN when configuring your IdP to send the appropriate SAML attributes for role assumption.
Test the SAML federated role
Once the SAML federated role has been created, you can verify its configuration and test the federation setup.
-
Open the IAM console at https://console.amazonaws.cn/iam/
. -
In the navigation pane, choose Roles.
-
Find and choose your newly created federated role.
If you provided a custom role name, look for that name. If you left the RoleName parameter empty, the role will have an auto-generated name based on the stack name and a unique identifier.
-
Choose the Trust relationships tab to review the trust policy.
The trust policy should show that your SAML IdP is trusted to assume this role with the condition that the SAML audience (
SAML:aud
) matcheshttps://signin.aws.amazon.com/saml
. -
Choose the Permissions tab to review the attached policies.
You can see any managed policies that were attached to the role during creation.
-
Note the Role ARN displayed on the role summary page.
You will need this ARN to configure your external IdP to allow users to assume this role.
Your SAML federated role is now ready to be used. Configure your external IdP to include this role's ARN in SAML assertions, and authenticated users will be able to assume this role to access Amazon resources.
Clean up: delete resources
As a final step, you'll delete the stack and the resources it contains.
-
Open the Amazon CloudFormation console.
-
On the Stacks page, choose the stack created from the template, and choose Delete, then confirm Delete.
CloudFormation initiates deletion of the stack and all resources it includes.
CloudFormation template details
Resources
The Amazon CloudFormation template for this tutorial will create the following resource in your account:
-
AWS::IAM::Role
: A federated IAM role that can be assumed by users authenticated through your SAML IdP.
Configuration
The template includes the following configurable parameters:
-
RoleName - Name of the IAM Role (leave empty for auto-generated name)
-
SAMLProviderARN - ARN of the SAML IdP (required)
-
RoleSessionDuration - Maximum session duration in seconds (3600-43200, default 7200)
-
RolePermissionsBoundary - Optional ARN of permissions boundary policy
-
RolePath - Path for the IAM role (default /)
-
ManagedPolicy1-5 - Optional ARNs of up to 5 managed policies to attach
CloudFormation template
Save the following JSON or YAML code as a separate file to use as the CloudFormation template for this tutorial.