Skip to content

Instantly share code, notes, and snippets.

@nitrocode
Created October 30, 2020 21:04
Show Gist options
  • Select an option

  • Save nitrocode/65ef493ced71cef0a15f9c6c605c3042 to your computer and use it in GitHub Desktop.

Select an option

Save nitrocode/65ef493ced71cef0a15f9c6c605c3042 to your computer and use it in GitHub Desktop.
Create the most awesome s3 backend.tf file
# provide the s3 bucket as the first param
export s3Bucket=${1:-bananananananana}
# get repo name
export repoName=$(git config --get remote.origin.url | cut -d '/' -f2 | cut -d '.' -f1)
# get current directory
export repoDir=$(git rev-parse --show-prefix | rev | cut -c 2- | rev)
# create backend
cat <<EOF > backend.tf
terraform {
required_version = ">=0.12"
backend "s3" {
encrypt = true
bucket = "$s3Bucket"
dynamodb_table = "TerraformLock"
region = "us-west-2"
key = "$repoName/$repoDir/terraform.tfstate"
}
}
EOF
# see the newly created file just to be safe
cat backend.tf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment