Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Creates a backend S3 bucket and DynamoDB table for managing Terraform state in external accounts

License

NotificationsYou must be signed in to change notification settings

rhythmictech/terraform-aws-backend

Repository files navigation

tflinttfsecyamllintmisspellpre-commit-checkfollow on Twitter

Creates an S3 bucket and DynamoDB table for managing Terraform state. Note that when bootstrapping a new environment, it is typically easier to use a separate method for creating the bucket and lock table, likea CloudFormation Stack. This module is intended to create a backend in an AWS account that is already Terraform-managed. This is useful to store the state for other accounts externally.

This module will create a CloudFormation stack and an optional wrapper script to deploy it. This stack is suitable to run in any account that will store its Terraform state in the bucket created by this module. It creates an IAM role with the AdministratorAccess policy attached and with an External ID which can then be assumed by terraform to create resources in the child account(s).

visualization

Breaking Changes

Previous versions of this module had support for cross-account management in a way that proved awkward for many uses cases and made it more difficult than it should've to fully secure the tfstate between accounts. Version 4.x and later eliminates support for this and refocuses the module on using centralized tfstate buckets with cross-account role assumption for execution of terraform. As a result, many variable names have changed and functionality has been dropped. Upgrade to this version at your own peril.

Multi-Account Usage

These instructions assume two AWS accounts; a "Parent" account which holds the terraform state and IAM users, and a "Child" account.

  1. In the parent account create this module. The below code is a serving suggestion.
module "backend" {  source  = "rhythmictech/backend/aws"  version = "4.1.0"  bucket_name                = "${local.account_id}-${var.region}-terraform-state"  create_assumerole_template = true  logging_target_bucket      = module.s3logging-bucket.s3_bucket_name  logging_target_prefix      = "${local.account_id}-${var.region}-tf-state"  tags                       = module.tags.tags_no_name}

It will create a folder with a shell script and a CloudFormation stack in it.

  1. Log into the child account and run the shell script,assumerole/addrole.sh. This will create a CloudFormation stack in that child account.

  2. In the terraform code for the child account create the provider and backend sections like below, substitutingPARENT_ACCT_ID andPARENT_REGION,CHILD_ACCT_ID, ANDEXTERNAL_ID.

terraform backend config:

bucket         = "PARENT_ACCT_ID-PARENT_REGION-terraform-state"dynamodb_table = "tf-locktable"key            = "account.tfstate"region         = "PARENT_REGION"

provider config:

provider "aws" {  assume_role {    role_arn     = "arn:aws:iam::CHILD_ACCT_ID:role/Terraform"    session_name = "terraform-network"    external_id  = "EXTERNAL_ID"  }}
  1. Log in to the master account and run terraform using this backend and provider config. The state will be stored in the parent account but terraform will assume the child account role.

Cross Account State Management

SeeUse AssumeRole to Provision AWS Resources Across Accounts for more information on this pattern.

This module is not intended to hold the state for the account in which it is created. If the account itself is also Terraform managed, it is recommended to create a separate bucket for its own state manually or via a different IaC method (e.g., CloudFormation) to avoid the chicken-and-egg problem. Seethis CloudFormation template to create terraform backend for this or any other single account.

You can test the ability to assume a role in the child account by logging in with the parent account and running this

export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \$(aws sts assume-role \--external-id EXTERNAL_ID \--role-arn arn:aws:iam::CHILD_ACCT_ID:role/Terraform \--role-session-name testme \--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \--output text))%  AWS_SECURITY_TOKEN=

Thenaws sts get-caller-identity should reveal you to be in the child account.

Requirements

NameVersion
terraform>= 0.14
aws>= 4.0
local>= 2.0
random>= 3.0

Providers

NameVersion
aws4.28.0
local2.2.3
random3.4.1

Modules

No modules.

Resources

NameType
aws_dynamodb_table.thisresource
aws_kms_alias.thisresource
aws_kms_key.thisresource
aws_s3_bucket.thisresource
aws_s3_bucket_lifecycle_configuration.thisresource
aws_s3_bucket_logging.thisresource
aws_s3_bucket_ownership_controls.thisresource
aws_s3_bucket_public_access_block.thisresource
aws_s3_bucket_server_side_encryption_configuration.thisresource
aws_s3_bucket_versioning.thisresource
local_file.assumerole_addroleresource
local_sensitive_file.assumerole_tfassumeroleresource
random_password.external_idresource
aws_caller_identity.currentdata source
aws_iam_policy_document.keydata source
aws_partition.currentdata source
aws_region.currentdata source

Inputs

NameDescriptionTypeDefaultRequired
assumerole_role_attach_policiesPolicy ARNs to attach to role (can be managed or custom but must exist)list(string)
[
"arn:aws:iam::aws:policy/AdministratorAccess"
]
no
assumerole_role_external_idExternal ID to attach to role (this is required, a random ID will be generated if not specified here)stringnullno
assumerole_role_nameName of role to create in assumerole templatestring"Terraform"no
assumerole_stack_nameName of CloudFormation stackstring"tf-assumerole"no
assumerole_template_nameFile name of assumerole cloudformation templatestring"assumerole/tfassumerole.cfn.yml"no
bucket_nameName of bucket to hold tf statestringn/ayes
create_assumerole_templateIf true, create a CloudFormation template that can be run against accounts to create an assumable roleboolfalseno
dynamo_locktable_nameName of lock table for terraformstring"tf-locktable"no
dynamodb_kms_key_arnKMS key arn to enable encryption on dynamodb table. Defaults toalias/aws/dynamodbstringnullno
dynamodb_point_in_time_recoveryDynamoDB point-in-time recovery.booltrueno
dynamodb_server_side_encryptionBool to enable encryption on dynamodb tablebooltrueno
kms_alias_nameName of KMS Aliasstringnullno
kms_key_idARN for KMS key for all encryption operations (a key will be created if this is not provided)stringnullno
lifecycle_ruleslifecycle rules to apply to the bucket (set to null to skip lifecycle rules)
list(object(
{
id = string
enabled = bool
prefix = string
expiration = number
noncurrent_version_expiration = number
}))
nullno
logging_target_bucketThe name of the bucket that will receive the log objects (logging will be disabled if null)stringnullno
logging_target_prefixA key prefix for log objectsstringnullno
tagsMapping of any extra tags you want added to resourcesmap(string){}no

Outputs

NameDescription
backend_config_stubBackend config stub to be used in child account(s)
external_idExternal ID attached to IAM role in managed accounts
kms_key_arnARN of KMS Key for S3 bucket
provider_config_stubProvider config stub to be used in child account(s)
s3_bucket_backendS3 bucket used to store TF state

About

Creates a backend S3 bucket and DynamoDB table for managing Terraform state in external accounts

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp