Created
December 28, 2018 18:05
-
-
Save andymotta/a0cdebdf70527bcac3a6dd551f9bad77 to your computer and use it in GitHub Desktop.
Parameterize Terraform remote state (AWS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data "aws_caller_identity" "current" {} | |
resource "aws_s3_bucket" "terraform_state" { | |
bucket = "${data.aws_caller_identity.current.account_id}-tfstate" | |
versioning { | |
enabled = true | |
} | |
policy = <<POLICY | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": "s3:ListBucket", | |
"Principal": { | |
"AWS": [ | |
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/svc-jenkins", | |
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/amotta" | |
] | |
}, | |
"Resource": "arn:aws:s3:::${data.aws_caller_identity.current.account_id}-tfstate" | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"s3:GetObject", | |
"s3:PutObject" | |
], | |
"Principal": { | |
"AWS": [ | |
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/svc-jenkins", | |
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/amotta" | |
] | |
}, | |
"Resource": "arn:aws:s3:::${data.aws_caller_identity.current.account_id}-tfstate/*" | |
} | |
] | |
} | |
POLICY | |
} | |
terraform { | |
backend "s3" {} | |
depends_on = ["aws_s3_bucket.terraform_state"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform init -backend-config="bucket=${ACCOUNT}-tfstate" -backend-config="key=${TF_VAR_stack_name}/terraform.tfstate" -backend-config="region=us-west-2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment