Skip to content

Instantly share code, notes, and snippets.

@jsonmaur jsonmaur/backend.json
Last active Sep 26, 2018

Embed
What would you like to do?
Initialize Terraform S3 Backend with CloudFormation
{
"Description": "Resources for Terraform backend",
"Resources": {
"MainBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"VersioningConfiguration": {
"Status": "Enabled"
},
"LifecycleConfiguration": {
"Rules": [{
"Status": "Enabled",
"NoncurrentVersionExpirationInDays": 90
}]
}
}
}
},
"Outputs": {
"BucketName": {
"Value": {
"Ref": "MainBucket"
}
}
}
}
#!/bin/bash
STACK_NAME=""
AWS_REGION=""
AWS_PROFILE=""
getBucketName () {
aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--query "Stacks[0].Outputs[?OutputKey=='BucketName'].OutputValue" \
--region $AWS_REGION \
--profile $AWS_PROFILE \
--output text
return $?
}
aws cloudformation deploy \
--stack-name $STACK_NAME \
--region $AWS_REGION \
--profile $AWS_PROFILE \
--template-file backend.json
terraform init \
-backend-config="bucket=$(getBucketName)" \
-backend-config="region=$AWS_REGION" \
-backend-config="profile=$AWS_PROFILE"
terraform {
backend "s3" {
encrypt = true
key = "terraform.tfstate"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.