Skip to content

Instantly share code, notes, and snippets.

@andymotta
Created December 28, 2018 18:05
Show Gist options
  • Save andymotta/a0cdebdf70527bcac3a6dd551f9bad77 to your computer and use it in GitHub Desktop.
Save andymotta/a0cdebdf70527bcac3a6dd551f9bad77 to your computer and use it in GitHub Desktop.
Parameterize Terraform remote state (AWS)
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"]
}
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