- The OpenTofu Language
- OpenTofu Settings
- Backends
- s3
Backend Type: s3
Stores the state as a given key in a given bucket on
Amazon S3.
This backend supports multiple locking mechanisms. The preferred one is a native S3 locking via
conditional writes with If-None-Match header. This can be enabled by setting use_lockfile=true.
Another option is to use Dynamo DB locking, which can be enabled by setting
the dynamodb_table field to an existing DynamoDB table name.
A single DynamoDB table can be used to lock multiple remote state files. OpenTofu generates key names that include the values of the bucket and key variables.
It is highly recommended that you enable Bucket Versioning on the S3 bucket to allow for state recovery in the case of accidental deletions and human error.
Both S3 and DynamoDB locking mechanisms are fully supported, and the OpenTofu team has no plans to deprecate either option. You should choose the locking mechanism that best fits your infrastructure requirements.
If you wish to migrate from DynamoDB to the S3-native state locking, please read the dedicated section.
Example Configuration​
terraform {
backend "s3" {
bucket = "mybucket"
key = "path/to/my/key"
region = "us-east-1"
}
}
This assumes we have a bucket created called mybucket. The
OpenTofu state is written to the key path/to/my/key.
Note that for the access credentials we recommend using a partial configuration.
S3 Bucket Permissions​
OpenTofu will need the following AWS IAM permissions on the target backend bucket:
s3:ListBucketonarn:aws:s3:::mybuckets3:GetObjectonarn:aws:s3:::mybucket/path/to/my/keys3:PutObjectonarn:aws:s3:::mybucket/path/to/my/keys3:DeleteObjectonarn:aws:s3:::mybucket/path/to/my/key
OpenTofu may also need the following AWS IAM permissions on the target backend bucket:
s3:PutObjectTaggingonarn:aws:s3:::mybucket/path/to/my/key
This is seen in the following AWS IAM Statement:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::mybucket"
},
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
"Resource": "arn:aws:s3:::mybucket/path/to/my/key"
}
]
}
AWS can control access to S3 buckets with either IAM policies
attached to users/groups/roles (like the example above) or resource policies
attached to bucket objects (which look similar but also require a Principal to
indicate which entity has those permissions). For more details, see Amazon's
documentation about
S3 access control.