This article explains the use of AssumeRole with Service: ec2 in Terraform, enabling secure AWS resource management within EC2 instances.
Managing AWS resources across different accounts is a common requirement when using Terraform. AWS AssumeRole provides a secure mechanism to grant your Terraform code temporary credentials to operate in a target account without requiring long-term access keys. This approach aligns with the principle of least privilege, enhancing the security of your infrastructure. Here's a step-by-step guide on how to leverage AssumeRole in your Terraform workflows to manage resources in different AWS accounts effectively.
When using Terraform to manage AWS resources, you might need to access and manage resources in an AWS account different from where your Terraform code runs. This is where AWS AssumeRole comes in handy.
AssumeRole allows you to grant your Terraform code temporary credentials to operate within a different AWS account, adhering to the principle of least privilege.
Here's how it works:
Important Considerations:
skip_metadata_api_check flag in your AWS provider configuration. This flag disables the default behavior of the provider attempting to retrieve credentials from the instance metadata service. Use this flag cautiously as it can have security implications.By using AssumeRole, you can enhance the security and flexibility of your Terraform deployments, allowing you to manage resources across different AWS accounts securely.
This code demonstrates how to use Terraform to manage resources in a different AWS account using AssumeRole. It shows how to create an IAM role and policy in the target account, and then configure the AWS provider in Terraform to assume that role. This allows Terraform to create resources in the target account on behalf of the user. The example specifically shows how to create an S3 bucket in a different account, but the same principles can be applied to other AWS resources.
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::<Account A ID>:root"
},
"Action": "sts:AssumeRole"
}
]
}Replace <Account A ID> with the actual AWS account ID where your Terraform code runs.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:CreateBucket",
"s3:DeleteBucket"
],
"Resource": "arn:aws:s3:::my-bucket-in-account-b"
}
]
}Replace my-bucket-in-account-b with your desired bucket name.
Main Account (Account A):
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-west-2"
assume_role {
role_arn = "arn:aws:iam::<Account B ID>:role/terraform-role"
}
}
resource "aws_s3_bucket" "example" {
bucket = "my-bucket-in-account-b"
acl = "private"
force_destroy = true
}Replace <Account B ID> with the actual AWS account ID of the target account and terraform-role with the name of the IAM role you created.
Explanation:
assume_role block within the aws provider tells Terraform to assume the specified IAM role in Account B.terraform apply, Terraform will use the assume_role configuration to request temporary credentials from AWS STS.Important:
This example demonstrates the basic principles of using AssumeRole with Terraform. You can adapt this approach to manage various AWS resources across different accounts securely.
Permissions & Policies:
*:* (all actions, all resources) permission in your target account role. Be as specific as possible about the actions and resources your Terraform code needs.AssumeRole Mechanics:
Best Practices:
Troubleshooting:
Alternatives to AssumeRole:
This table summarizes using AWS AssumeRole to manage resources in a different AWS account than where your Terraform code runs:
| Step | Description | Security & Best Practices |
|---|---|---|
| 1. Define IAM Role in Target Account | Create an IAM role in the target account with the specific permissions your Terraform code needs. | Principle of Least Privilege: Grant only the necessary permissions to this role. |
| 2. Grant AssumeRole Permissions | In the target account's IAM role policy, specify who (user, role, service) can assume this role. | |
| 3. Configure AWS Provider in Terraform | In your Terraform code, configure the AWS provider to use AssumeRole by providing the ARN of the target account's IAM role. | |
| 4. Terraform Assumes Role | When executed, Terraform uses the provided ARN to request temporary credentials from AWS STS. | |
| 5. Terraform Manages Target Account | Using the temporary credentials, Terraform can now provision and manage resources in the target AWS account. |
Additional Considerations:
skip_metadata_api_check flag in the AWS provider configuration cautiously, as it can have security implications.Benefits:
AWS AssumeRole is a powerful feature for managing AWS resources across multiple accounts securely and efficiently with Terraform. By leveraging temporary credentials, you can adhere to the principle of least privilege, enhancing the overall security posture of your infrastructure. By understanding the steps involved in setting up AssumeRole, configuring your Terraform code, and following best practices, you can streamline your infrastructure management workflows while maintaining a high level of security. Remember to carefully consider the permissions granted to your IAM roles and utilize tools like the AWS Policy Simulator to validate your configurations. As you adopt more complex infrastructure setups, exploring advanced AssumeRole features like external IDs and session tags can further enhance your security and governance practices. By embracing these principles and techniques, you can confidently manage your AWS resources across multiple accounts using Terraform, ensuring a secure and well-organized cloud environment.
Use AssumeRole to provision AWS resources across accounts ... | AWS AssumeRole allows you to grant temporary credentials with additional privileges to users as needed, following the principle of least privilege.
Backend Type: s3 | Terraform | HashiCorp Developer | Endpoints set for specific services will override the base endpoint configured in the profile. Assume Role Configuration. Assuming an IAM Role can be configured ...
AWS EC2 Assume Role with Terraform | e-Zest | Nowadays, it is an ideal practice to keep different AWS accounts for IAM users and a different account for running production infrastructure. Also, Terraform is used to manage and provision cloud infrastructure. To create infrastructure in one account while users reside in another, the user needs to assume the role of that account. To provision resources using Terraform, which executes on an EC2 instance in the AWS account where users reside, we need to assume the role by saving credentials of resources account on the EC2 instance.