---
title: "AWS resources using AssumeRole"
url: "https://www.pulumi.com/dev/examples/aws-py-assume-role/"
image: "https://www.pulumi.com/assets/og/dev/examples/aws-py-assume-role.png"
---

# AWS resources using AssumeRole

- Source on GitHub: https://github.com/pulumi/examples/tree/master/aws-py-assume-role

## Get started with this example

This example lives in the [pulumi/examples](https://github.com/pulumi/examples/tree/master/aws-py-assume-role) repo. Pull down just this directory to follow along:

```bash
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set aws-py-assume-role
cd pulumi-examples/aws-py-assume-role
```

This example shows how to use the AssumeRole functionality of the AWS provider to create resources in the security context of an IAM Role assumed by the IAM User running the Pulumi programs.

This directory contains two Pulumi projects that are deployed in sequence:

- [create-role/](https://github.com/pulumi/examples/tree/master/aws-py-assume-role/create-role) — creates an unprivileged IAM user and a role the user is allowed to assume.
- [assume-role/](https://github.com/pulumi/examples/tree/master/aws-py-assume-role/assume-role) — assumes that role and creates an S3 bucket in its security context.

## Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
2. [Configure AWS credentials](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/)
3. [Install Python](https://www.pulumi.com/docs/intro/languages/python/)

## Deploying the example

### Part 1: Privileged components

The Pulumi program in `create-role` requires credentials with permissions to create an IAM User, an IAM Role, and assign an AWS Access Key to the user. The program creates a new, unprivileged user with no policies attached, and a role which specifies a trust policy allowing assumption by the unprivileged user. The role allows the `s3:*` actions on all resources.

Set the `create-role:unprivilegedUsername` configuration variable to the name of the unprivileged user, as well as the AWS region in which to operate, then install dependencies and deploy the stack:

```bash
cd create-role
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pulumi stack init assume-role-create
pulumi config set create-role:unprivilegedUsername somebody@pulumi.com
pulumi config set aws:region us-east-1
pulumi up
```

The outputs of the program tell you the ARN of the Role, and the Access Key ID and Secret associated with the User:

```bash
pulumi stack output --json
```

```
{
  "accessKeyId": "AKIAYJ7EUPHL3DSDH4CX",
  "roleArn": "arn:aws:iam::571173272023:role/allow-s3-management-fcc71c0",
  "secretAccessKey": [secret]
}
```

The above command does not show the `secretAccessKey`. In order to show the secret value, use this:

```bash
pulumi stack output --json --show-secrets
```

```
{
  "accessKeyId": "AKIAYJ7EUPHL3DSDH4CX",
  "roleArn": "arn:aws:iam::571173272023:role/allow-s3-management-fcc71c0",
  "secretAccessKey": "[plain text value]"
}
```

### Part 2: Assuming the role

The Pulumi program in `assume-role` creates an S3 bucket after assuming the Role created in Part 1. It should be run with the unprivileged user credentials created in Part 1. Configure it as follows, from the `assume-role` directory, replacing `{YOUR_STACK_PATH/assume-role-create}` with the full name of your stack from Part 1. The full name of your stack is available at [`app.pulumi.com`][app]:

```bash
cd ../assume-role
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
export AWS_ACCESS_KEY_ID="$(pulumi stack output --stack {YOUR_STACK_PATH/assume-role-create} accessKeyId)"
export AWS_SECRET_ACCESS_KEY="$(pulumi stack output --stack {YOUR_STACK_PATH/assume-role-create} --show-secrets secretAccessKey)"
```

The configuration variable `roleToAssumeARN` must be set to the ARN of the role allowing S3 access, and the AWS region must be set to the region in which you wish to operate:

```bash
pulumi stack init assume-role-assume
pulumi config set roleToAssumeARN "$(pulumi stack output --stack {YOUR_STACK_PATH/assume-role-create} roleArn)"
pulumi config set aws:region us-east-1
```

Unset the `AWS_SESSION_TOKEN` or any additional credential setting if you have set it for previous access:

```bash
unset AWS_SESSION_TOKEN
```

Deploy the stack:

```bash
pulumi up
```

You can verify that the role is indeed assumed by looking at the CloudTrail logs of the bucket creation operation, or by commenting out the `assumeRole` configuration in the provider and ensuring creation is not successful.

## Cleaning up

To clean up your resources, run `pulumi destroy` and respond yes to the confirmation prompt in each project directory.

[app]: https://app.pulumi.com/
