Setup and customize the Terraform S3 backend through Terraform itself. This method is best suited for when you wish to manage all infrastructure via Terraform.
There are a small number of parameters that let you customize your S3 backend deployment. These parameters may be set on the command line with the -var
flag. Multiple parameters may be specified by repeated use of -var
. See the Input Variables topic in the Terraform documentation.
For example;
terraform apply -var 's3_bucket_name=MyCustomBucketName' -var 'dynamo_table_name=my-table-name'
-var "s3_bucket_name=MyCustomBucketName"
When left empty this will cause a new S3 bucket to be created with a somewhat random, but unique name.
-var "s3_key_prefix=my/custom/S3/key/prefix"
Overrides the default key prefix in the S3 bucket which stores your Terraform state objects. Set to /terraform-state
by default. Take care to the omitted the leading /
.
-var "dynamo_table_name=my-table-name"
Specify to override the default DynamoDB table name, which is terraform-locking
.
-var "iam_policy_name=my-policy-name"
Specify to override the default IAM policy name, which is Terraform-S3-Backend
.
-var "parameter_prefix=my/param/prefix"
Override the prefix of the SSM Parameter Store values that are created to store the S3 bucket name and DynamoDB table. Set to /terraform
by default. Take care to the omitted the leading /
.
When setting up the S3 backend using Terraform the state is stored locally and must be migrated into the backend afterwards (unless you wish to store the backend's state locally).
Once your backend configuration has been setup (see the project README for more information) you may then run the following to migrate your local state into the S3 backend:
terraform init -migrate-state
You will be prompted for confirmation by Terraform, the backend will be initialized, and your local state will be copied into the new backend.