Learn how to leverage AWS assume role capabilities for seamless and secure Terraform deployments in your cloud infrastructure.
Managing infrastructure across multiple AWS accounts is a common scenario in cloud environments. Terraform, a popular Infrastructure as Code (IaC) tool, provides a mechanism to interact with and manage resources in AWS accounts you don't directly control. This approach leverages AWS's AssumeRole functionality to enhance security and streamline cross-account resource management.
To use Terraform to manage resources in an AWS account you don't directly control (like a different AWS account), you can leverage AWS's AssumeRole functionality. Here's how:
Understand AssumeRole: AssumeRole lets an IAM user (or another AWS service) temporarily take on the permissions of a different IAM role. This is perfect for cross-account access in Terraform.
Prerequisites:
Terraform Configuration:
provider "aws" {
region = "your-target-region"
assume_role {
role_arn = "arn:aws:iam::TARGET_ACCOUNT_ID:role/YOUR_ROLE_NAME"
session_name = "TerraformSession" # Optional, for session naming
}
}TARGET_ACCOUNT_ID and YOUR_ROLE_NAME with the actual values.Authentication:
terraform apply), the AWS provider will automatically use your local AWS credentials to assume the specified role. This grants Terraform the permissions it needs in the target account.Important Considerations:
By following these steps, you can effectively use Terraform to manage resources across different AWS accounts using AssumeRole, enhancing security and simplifying your infrastructure management.
This code demonstrates how to create an S3 bucket in a different AWS account (Account B) from your main account (Account A) using Terraform's AssumeRole functionality. In Account B, you create an IAM role with permissions to create S3 buckets and allow your IAM user from Account A to assume this role. In Account A, your Terraform code uses the 'assume_role' block within the AWS provider to specify the role ARN from Account B. When you run 'terraform apply', Terraform uses your local AWS credentials to obtain temporary credentials for the role in Account B, allowing it to create the S3 bucket in the target account. This setup ensures secure cross-account resource provisioning in AWS.
This example demonstrates how to use Terraform to create an S3 bucket in a different AWS account using AssumeRole.
Target Account (Account B):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::YOUR_ACCOUNT_ID:user/YOUR_IAM_USER"
},
"Action": "sts:AssumeRole"
}
]
}YOUR_ACCOUNT_ID and YOUR_IAM_USER with the account ID and IAM user from your main account (Account A).AmazonS3FullAccess).Main Account (Account A):
Terraform Configuration (main.tf):
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-west-2" # Replace with your target region
assume_role {
role_arn = "arn:aws:iam::TARGET_ACCOUNT_ID:role/YOUR_ROLE_NAME"
}
}
resource "aws_s3_bucket" "example" {
bucket = "my-bucket-in-another-account"
acl = "private"
tags = {
Name = "Example bucket created with Terraform and AssumeRole"
}
}TARGET_ACCOUNT_ID and YOUR_ROLE_NAME with the values from Account B.Authentication:
Deploy:
terraform init to initialize the project.terraform apply to create the S3 bucket in Account B.Explanation:
assume_role block in the aws provider tells Terraform to assume the specified role in Account B.terraform apply, Terraform uses your local AWS credentials to get temporary credentials for the role in Account B.Important:
Best Practices:
Troubleshooting:
Alternatives to AssumeRole:
Additional Tips:
This table summarizes how to use Terraform to manage resources in a different AWS account using AssumeRole:
| Step | Description | Key Points |
|---|---|---|
| 1. Understand AssumeRole | - Allows temporary assumption of another IAM role's permissions. - Ideal for secure cross-account access in Terraform. |
|
| 2. Prerequisites |
Target Account: - Create an IAM role with necessary permissions for Terraform. - Configure the role's trust policy to allow your IAM user or service to assume it. |
- Define permissions based on the principle of least privilege. |
| 3. Terraform Configuration |
Provider Configuration: - Configure the AWS provider to use assume_role. - Specify the role_arn and optionally a session_name. |
- Replace placeholders with actual account ID and role name. |
| 4. Authentication | - Ensure your local AWS CLI has credentials with permission to assume the defined role. | - Terraform will automatically use these credentials to assume the role during execution. |
| 5. Important Considerations | - Security: Carefully manage the assumed role's permissions. - Session Duration: Handle potential timeouts for long-running operations. - Terraform Cloud/Enterprise: Securely configure environment variables for credentials. |
- Prioritize security and plan for session management. |
By following these steps, you can leverage AssumeRole for secure and streamlined cross-account resource management with Terraform.
In conclusion, managing infrastructure across multiple AWS accounts is made significantly easier and more secure with Terraform's AssumeRole functionality. By configuring an IAM role in the target account and utilizing the assume_role block within your Terraform code, you can seamlessly provision resources without needing to directly manage credentials for the target account. This approach, combined with security best practices like the principle of least privilege and proper credential management, empowers organizations to maintain a secure and efficient multi-account AWS environment. As your infrastructure grows, mastering Terraform's AssumeRole capabilities will prove invaluable for managing complex deployments and ensuring secure cross-account access within your AWS ecosystem.
Use AssumeRole to provision AWS resources across accounts ... | The AWS Terraform provider can use AssumeRole credentials to authenticate against AWS. In this tutorial, you will use Terraform to define an IAM role that ...
Using Credential created by AWS SSO for Terraform - AWS ... | I read a lot of articles related with this issue, including this. I am little confused so I want to ask my understanding. Using credential create by AWS SSO and stored in ~/.aws/cli or ~/.aws/sso to deploy aws resource by terraform is not possible. is this correct? It seems there are possible way if you are trying to use aws-sdk-go, but just declare it in terraform file such as provider “aws” … with using aws_shared_credentials and profile is not working properly. please help me to un...
Terraform Cloud Remote execution mode and AWS assumed role ... | Hi there, I followed this tutorial: Everything is great until I reached the store remote state section at the end. It says in the guide that Terraform Cloud will now run in remote execution mode. You then get told to add secret key and key id to the Terraform Cloud user interface environment variables. All not a problem. However, my organisation requires me to run as an assumed role and MFA. This is setup ok for my AWS Cli when running Terraform local execution like so: .aws\config [pr...