Buckets with Terraform
chili speaks the native S3 API, so Terraform’s existing AWS provider can manage it. You point the provider at the chili endpoint, and your bucket settings such as versioning and Object Lock become reproducible code. You create the buckets themselves in the chili Console.
Prerequisites
- Terraform 1.5 or newer
- chili access keys (access key and secret key)
- the keys as environment variables, not in code
1. Configure the provider
The skip_* options disable the AWS-specific metadata checks, and s3_use_path_style ensures the right addressing:
provider "aws" {
region = "ch-east"
access_key = var.chili_access_key
secret_key = var.chili_secret_key
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
s3_use_path_style = true
endpoints {
s3 = "https://s3-ch-east.chilistorage.com"
}
}
variable "chili_access_key" { sensitive = true }
variable "chili_secret_key" { sensitive = true }Pass the keys via environment variables so they never land in the repository:
export TF_VAR_chili_access_key="AKIA..."
export TF_VAR_chili_secret_key="..."
export AWS_REQUEST_CHECKSUM_CALCULATION=when_required
export AWS_RESPONSE_CHECKSUM_VALIDATION=when_required2. Adopt the bucket
Create the bucket in the chili Console, for example my-backup, and enable Object Lock. Terraform then adopts the existing bucket with an import block:
import {
to = aws_s3_bucket.backup
id = "my-backup"
}
resource "aws_s3_bucket" "backup" {
bucket = "my-backup"
object_lock_enabled = true
lifecycle {
prevent_destroy = true
}
}prevent_destroy stops terraform destroy from deleting the bucket. Object Lock can only be enabled when the bucket is created in the Console, not afterwards.
3. Versioning and Object Lock
Object Lock requires versioning to be enabled:
resource "aws_s3_bucket_versioning" "backup" {
bucket = aws_s3_bucket.backup.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_object_lock_configuration" "backup" {
bucket = aws_s3_bucket.backup.id
rule {
default_retention {
mode = "GOVERNANCE"
days = 30
}
}
}The example uses governance mode, the default on chili. More under Object Lock.
4. State and good practice
- Keep the Terraform state remote, for example in a dedicated chili bucket, not local and not in the repo.
- Provide keys only via environment variables or a secret store.
- Run
terraform planbeforeterraform apply.