Skip to content

Instantly share code, notes, and snippets.

@charity
Created May 18, 2016 20:19
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save charity/49d90285f20803f652fffe641598a9cc to your computer and use it in GitHub Desktop.
Save charity/49d90285f20803f652fffe641598a9cc to your computer and use it in GitHub Desktop.
terraform environment init.sh
#!/bin/bash
# Usage: ./init.sh once to initialize remote storage for this environment.
# Subsequent tf actions in this environment don't require re-initialization,
# unless you have completely cleared your .terraform cache.
#
# terraform plan -var-file=./production.tfvars
# terraform apply -var-file=./production.tfvars
tf_env="production"
terraform remote config -backend=s3 \
-backend-config="bucket=hound-terraform-state" \
-backend-config="key=$tf_env.tfstate" \
-backend-config="region=us-east-1"
echo "set remote s3 state to $tf_env.tfstate"
@charity
Copy link
Author

charity commented May 18, 2016

path: $gitroot/infra/terraform/env-prod/init.sh

@sean-abbott
Copy link

So, the question I have about this, is does it always sync the state to the same .terraform file? So if I'm running a dev cycle, and do env-dev/init.sh, finalize it, and then do env-prod/init.sh, will the local file be .terraform/terraform.tfstate no matter what?

Has that been a problem (following along with your wonderful and timely blog post, btw).

Thanks!

@jedi4ever
Copy link

jedi4ever commented Jul 18, 2016

@sean-abbott I guess it will use $(PWD) to use .terraform . So if you switch dirs (cd env-dev or cd env-production) and then run init.sh you'll get:

[env-dev]
   - [.terraform]
      - .terraform.state
[env-prod]
   - [.terraform]
      - .terraform.state

@spanktar
Copy link

We're doing much of what you've outlined here (hooray, we're doing it right too!), but my question is this: if you have a terraform remote state resource, why do you need to initialize it with the shell script? We had our own shell script already and it was my thinking that putting the remote state into Terraform as a resource would eliminate the need for it. BY which I mean, we had the shell script and NO terraform remote state resource declaration and it worked just fine.

Thoughts?

@pporada-gl
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment