Reputation: 1008
I am using S3 backend for my Terraform scripts:
terraform {
backend "s3" {
bucket = "mybucket"
key = "path/to/my/key"
region = "us-east-1"
}
}
If I use multiple workspaces, will the state of all my workspaces be stored in the above mentioned S3 backend state file?
I tried creating 2 workspaces and the state file does not even get created on the S3 bucket mentioned above.
What am I missing?
Upvotes: 0
Views: 2150
Reputation: 4857
Terraform will create the state file in the bucket depending on which workspace you've selected with a workspace key prefix, the default settings will give you something like this:
env:/<my workspace>/path/to/my/key
That means if you run terraform workspace select <my workspace>
and terraform apply
the state file for the selected workspace will end up in the above folder.
However for storing state on an S3 bucket you will need to create the S3 storage bucket yourself (Terraform expects it to exist).
You can use the following commands:
aws s3 mb s3://<my bucket> --region <my region>
aws s3api put-bucket-versioning \
--region <my region> \
--bucket <my bucket> \
--versioning-configuration "Status=Enabled"
aws s3api put-public-access-block \
--region <my region> \
--bucket <my bucket> \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"
Terraform also recommends using a DynamoDB table for locking, you can create it like this:
aws dynamodb create-table \
--region <my region> \
--table-name <my dynamodb lock table> \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH
You will need to add the name of the DynamoDB lock table in your backend configuration then with dynamodb_table = "<my dynamodb lock table>"
.
Upvotes: 2