policy variable for attaching arbitrary custom JSON policies.
Attaching a custom policy
Setattach_policy = true and provide a JSON policy document via the policy variable:
Policy placeholders
When writing a custom policy you often need to reference the bucket’s ID, ARN, or the owning account ID. These values are not always known ahead of time — especially whenbucket_prefix is used to generate a unique name. The module replaces the following placeholder strings at apply time:
| Placeholder | Replaced with |
|---|---|
_S3_BUCKET_ID_ | The bucket name (aws_s3_bucket.this[0].id) |
_S3_BUCKET_ARN_ | The bucket ARN (aws_s3_bucket.this[0].arn) |
_AWS_ACCOUNT_ID_ | The caller’s AWS account ID |
bucket_prefix, because the final bucket name is generated by AWS and is not known until after creation:
Built-in managed policies
The module includes built-in policies for common scenarios. Enable them with the corresponding boolean variable:| Variable | Description |
|---|---|
attach_deny_insecure_transport_policy | Deny all requests not using HTTPS |
attach_require_latest_tls_policy | Deny TLS versions older than 1.2 |
attach_deny_incorrect_encryption_headers | Deny PutObject with the wrong SSE algorithm header |
attach_deny_incorrect_kms_key_sse | Deny PutObject that uses a KMS key other than allowed_kms_key_arn |
attach_deny_unencrypted_object_uploads | Deny PutObject without an SSE header |
attach_deny_ssec_encrypted_object_uploads | Deny PutObject using SSE-C (customer-provided keys) |
attach_elb_log_delivery_policy | Allow Classic ELB to write access logs |
attach_lb_log_delivery_policy | Allow ALB/NLB to write access logs |
attach_cloudtrail_log_delivery_policy | Allow CloudTrail to write logs |
attach_waf_log_delivery_policy | Allow WAF to write logs |
attach_access_log_delivery_policy | Allow S3 server access log delivery |
aws_s3_bucket_policy resource. Custom (policy) and built-in policies are combined automatically.
Combining custom and built-in policies
Viewing the applied policy
The applied policy JSON is available via thes3_bucket_policy output:

