Skip to content

Instantly share code, notes, and snippets.

@thsutton
Last active March 9, 2018 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thsutton/3e5d88dc9a7783d26692c5184835708b to your computer and use it in GitHub Desktop.
Save thsutton/3e5d88dc9a7783d26692c5184835708b to your computer and use it in GitHub Desktop.
Putting Terraform state into an S3 bucket you manage with Terraform
$ mkdir example
$ cd example
$ cat > main.tf <<EOF
provider "aws" { region = "ap-southeast-2" }
resource "aws_s3_bucket" "state" {
bucket = "thomas-example-s3-stateception"
acl = "private"
}
output "bucket" {
value = "${aws_s3_bucket.state.bucket}"
}
EOF
$ terraform init
Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (1.10.0)...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.aws: version = "~> 1.10"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ terraform apply -auto-approve
aws_s3_bucket.state: Creating...
acceleration_status: "" => "<computed>"
acl: "" => "private"
arn: "" => "<computed>"
bucket: "" => "thomas-example-s3-stateception"
bucket_domain_name: "" => "<computed>"
force_destroy: "" => "false"
hosted_zone_id: "" => "<computed>"
region: "" => "<computed>"
request_payer: "" => "<computed>"
versioning.#: "" => "<computed>"
website_domain: "" => "<computed>"
website_endpoint: "" => "<computed>"
aws_s3_bucket.state: Still creating... (10s elapsed)
aws_s3_bucket.state: Creation complete after 12s (ID: thomas-example-s3-stateception)
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
bucket = thomas-example-s3-stateception
$ cat > backend.tf << EOF
> terraform {
> backend "s3" {
> bucket = "$(terraform output bucket)"
> key = "stateception/terraform.tfstate"
> region = "ap-southeast-2"
> }
> }
> EOF
$ terraform init -force-copy
Initializing the backend...
Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.aws: version = "~> 1.10"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ aws s3 ls --recursive s3://$(terraform output bucket)/
2018-03-09 15:45:36 2122 stateception/terraform.tfstate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment