Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
resource "aws_s3_bucket" "data" {
bucket = "acme-shop-data-prod"
tags = { env = "prod", team = "shop" }
}
resource "aws_s3_bucket_public_access_block" "data" {
bucket = aws_s3_bucket.data.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_ownership_controls" "data" {
bucket = aws_s3_bucket.data.id
rule { object_ownership = "BucketOwnerEnforced" }
}
resource "aws_s3_bucket_versioning" "data" {
bucket = aws_s3_bucket.data.id
versioning_configuration { status = "Enabled" }
}
resource "aws_s3_bucket_server_side_encryption_configuration" "data" {
bucket = aws_s3_bucket.data.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
kms_master_key_id = aws_kms_key.s3.arn
}
bucket_key_enabled = true
}
}Step by step
- What each block does:
| Resource | Why |
|---|---|
public_access_block | The bucket can never become public (policy or ACL). |
ownership_controls (BucketOwnerEnforced) | ACLs are off — access only by IAM/bucket policy. |
versioning | Deleted/overwritten objects can be restored. |
server_side_encryption_configuration | Encrypted at rest with your KMS key (bucket_key_enabled lowers KMS cost). |
- Refuse plain HTTP with a bucket policy:
data "aws_iam_policy_document" "tls_only" {
statement {
sid = "DenyInsecureTransport"
effect = "Deny"
actions = ["s3:*"]
resources = [
aws_s3_bucket.data.arn,
"${aws_s3_bucket.data.arn}/*",
]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
resource "aws_s3_bucket_policy" "data" {
bucket = aws_s3_bucket.data.id
policy = data.aws_iam_policy_document.tls_only.json
}- The KMS key with rotation:
resource "aws_kms_key" "s3" {
description = "S3 data bucket key"
enable_key_rotation = true
}- Lifecycle — old versions and unfinished uploads cost money:
resource "aws_s3_bucket_lifecycle_configuration" "data" {
bucket = aws_s3_bucket.data.id
rule {
id = "cleanup"
status = "Enabled"
filter {}
noncurrent_version_expiration { noncurrent_days = 90 }
abort_incomplete_multipart_upload { days_after_initiation = 7 }
}
}- Scan before apply:
trivy config .
checkov -d .- Many buckets? Put all of this into one module (see the module how-to) — every bucket gets the same safe defaults.
Check that it worked
terraform plan -out=tfplan && terraform apply tfplan
aws s3api get-public-access-block --bucket acme-shop-data-prod
aws s3api get-bucket-encryption --bucket acme-shop-data-prod --query 'ServerSideEncryptionConfiguration.Rules[0]'
aws s3api get-bucket-versioning --bucket acme-shop-data-prodAll four public-access settings are true, encryption shows aws:kms, versioning Enabled, and the scanners report no high findings.
Common problems
| Problem | Cause | Fix |
|---|---|---|
BucketAlreadyExists | Bucket names are global across all AWS accounts. | Choose a unique name (account/region prefix). |
Unsupported argument "versioning" in aws_s3_bucket | Old (v3) syntax. | Use the separate resources (provider v4+). |
AccessDenied writing objects with KMS | The writer has no rights on the KMS key. | Grant kms:GenerateDataKey/kms:Encrypt (and Decrypt to read). |
| ACL errors from an old tool | ACLs are disabled (BucketOwnerEnforced). | Update the tool to not send ACLs. |
terraform destroy fails (bucket not empty) | Versioned objects remain. | Empty it first, or force_destroy = true only for test buckets. |
| Scanner flags missing access logging | No server access logs. | Add aws_s3_bucket_logging to a log bucket if required. |
Go further
- Command pages: terraform, aws, trivy.
- Next how-tos: Find public S3 buckets, Write a Terraform module, Copy files to and from S3.
- Full guides: Terraform, Checkov. Learn it: Terraform — Resources, Scanning.
1Why are versioning and encryption separate resources in the AWS provider?
Since provider v4 each bucket setting is its own resource (aws_s3_bucket_versioning, …_server_side_encryption_configuration, …).
2How do you make S3 refuse plain HTTP requests?
A bucket policy that denies all actions when aws:SecureTransport is false.