AWS Amazon S3 Bucket
This page shows how to write Terraform and CloudFormation for Amazon S3 Bucket and write them securely.
aws_s3_bucket (Terraform)
The Bucket in Amazon S3 can be configured in Terraform with the resource name aws_s3_bucket. The following sections describe 2 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_s3_bucket" "module1-state" {
bucket = "mypizdiuchky-state"
versioning {
enabled = true
}
resource "aws_s3_bucket" "registry-bucket" {
bucket = "gitlab-container-registry-123"
acl = "public-read-write"
}
resource "aws_s3_bucket" "artifacts-bucket" {
Security Best Practices for aws_s3_bucket
There are 3 settings in aws_s3_bucket that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure S3 bucket access policy is well configured
It is better to configure the S3 bucket access policy properly to limit it unless explicitly required.
Ensure S3 bucket versoning is enabled
It is better to enable versioning of the S3 bucket to preserve, retrieve, and restore old versions of every object. It may help handle availability and integrity issues.
Ensure S3 bucket logging is enabled
It's better to enable S3 bucket logging. S3 bucket logging helps audit activities related to the bucket.
Parameters
-
acceleration_statusoptional computed - string -
acloptional - string -
arnoptional computed - string -
bucketoptional computed - string -
bucket_domain_nameoptional computed - string -
bucket_prefixoptional - string -
bucket_regional_domain_nameoptional computed - string -
force_destroyoptional - bool -
hosted_zone_idoptional computed - string -
idoptional computed - string -
policyoptional - string -
regionoptional computed - string -
request_payeroptional computed - string -
tagsoptional - map from string to string -
website_domainoptional computed - string -
website_endpointoptional computed - string -
cors_rulelist block-
allowed_headersoptional - list of string -
allowed_methodsrequired - list of string -
allowed_originsrequired - list of string -
expose_headersoptional - list of string -
max_age_secondsoptional - number
-
-
grantset block-
idoptional - string -
permissionsrequired - set of string -
typerequired - string -
urioptional - string
-
-
lifecycle_rulelist block-
abort_incomplete_multipart_upload_daysoptional - number -
enabledrequired - bool -
idoptional computed - string -
prefixoptional - string -
tagsoptional - map from string to string -
expirationlist block-
dateoptional - string -
daysoptional - number -
expired_object_delete_markeroptional - bool
-
-
noncurrent_version_expirationlist block-
daysoptional - number
-
-
noncurrent_version_transitionset block-
daysoptional - number -
storage_classrequired - string
-
-
transitionset block-
dateoptional - string -
daysoptional - number -
storage_classrequired - string
-
-
-
loggingset block-
target_bucketrequired - string -
target_prefixoptional - string
-
-
object_lock_configurationlist block-
object_lock_enabledrequired - string -
rulelist block-
default_retentionlist block
-
-
-
replication_configurationlist block-
rolerequired - string -
rulesset block-
idoptional - string -
prefixoptional - string -
priorityoptional - number -
statusrequired - string -
destinationlist block-
account_idoptional - string -
bucketrequired - string -
replica_kms_key_idoptional - string -
storage_classoptional - string -
access_control_translationlist block-
ownerrequired - string
-
-
-
filterlist block -
source_selection_criterialist block-
sse_kms_encrypted_objectslist block-
enabledrequired - bool
-
-
-
-
-
server_side_encryption_configurationlist block-
rulelist block-
bucket_key_enabledoptional - bool -
apply_server_side_encryption_by_defaultlist block-
kms_master_key_idoptional - string -
sse_algorithmrequired - string
-
-
-
-
versioninglist block-
enabledoptional - bool -
mfa_deleteoptional - bool
-
-
websitelist block-
error_documentoptional - string -
index_documentoptional - string -
redirect_all_requests_tooptional - string -
routing_rulesoptional - string
-
Explanation in Terraform Registry
Provides a S3 bucket resource. -> This functionality is for managing S3 in an AWS Partition. To manage S3 on Outposts, see the
aws_s3control_bucketresource.
Tips: Best Practices for The Other AWS Amazon S3 Resources
In addition to the aws_s3_bucket_public_access_block, AWS Amazon S3 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_s3_bucket_public_access_block
Ensure S3 bucket-level Public Access Block restricts public bucket policies
It is better to enable S3 bucket-level Public Access Block if you don't need public buckets.
AWS::S3::Bucket (CloudFormation)
The Bucket in S3 can be configured in CloudFormation with the resource name AWS::S3::Bucket. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref MugShotCodeBucketName
SubmissionMugShotBucket:
Type: AWS::S3::Bucket
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub '${Topic.TopicName}-bucket'
S3Bucket2:
Type: AWS::S3::Bucket
Type: AWS::S3::Bucket
expectations:
rules:
delta_s3_public_access_control: FAIL
- name: MyTest4
input:
Type: AWS::S3::Bucket
Properties:
BucketName: cfd-shared-content
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
Type: AWS::S3::Bucket
Properties:
BucketName: "at-hella-calibrations"
BuildAUBucket:
Type: AWS::S3::Bucket
Properties:
"Type" : "AWS::S3::Bucket",
"Properties" : {
"BucketName" : "fakebucketfakebucket"
}
},
"resourceType" : "AWS::S3::Bucket",
"properties" : [ {
"propertyName" : "AccessControl",
"propertyType" : "String",
"required" : false
}, {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketEncryption": {
"ServerSideEncryptionConfiguration": [
{
"ServerSideEncryptionByDefault": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketEncryption": {
"ServerSideEncryptionConfiguration": [
{
"ServerSideEncryptionByDefault": {
"Type": "AWS::S3::Bucket"
},
"TransformLogGroup": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"LogGroupName": "/aws/lambda/thumbnail-creator-dev-transform"
Parameters
-
AccelerateConfigurationoptional - AccelerateConfiguration -
AccessControloptional - String -
AnalyticsConfigurationsoptional - List of AnalyticsConfiguration -
BucketEncryptionoptional - BucketEncryption -
BucketNameoptional - String -
CorsConfigurationoptional - CorsConfiguration -
IntelligentTieringConfigurationsoptional - List of IntelligentTieringConfiguration -
InventoryConfigurationsoptional - List of InventoryConfiguration -
LifecycleConfigurationoptional - LifecycleConfiguration -
LoggingConfigurationoptional - LoggingConfiguration -
MetricsConfigurationsoptional - List of MetricsConfiguration -
NotificationConfigurationoptional - NotificationConfiguration -
ObjectLockConfigurationoptional - ObjectLockConfiguration -
ObjectLockEnabledoptional - Boolean -
OwnershipControlsoptional - OwnershipControls -
PublicAccessBlockConfigurationoptional - PublicAccessBlockConfiguration -
ReplicationConfigurationoptional - ReplicationConfiguration -
Tagsoptional - List of Tag -
VersioningConfigurationoptional - VersioningConfiguration -
WebsiteConfigurationoptional - WebsiteConfiguration
Explanation in CloudFormation Registry
The
AWS::S3::Bucketresource creates an Amazon S3 bucket in the same AWS Region where you create the AWS CloudFormation stack.To control how AWS CloudFormation handles the bucket when the stack is deleted, you can set a deletion policy for your bucket. You can choose to retain the bucket or to delete the bucket. For more information, see DeletionPolicy Attribute.
Important You can only delete empty buckets. Deletion fails for buckets that have contents.
Frequently asked questions
What is AWS Amazon S3 Bucket?
AWS Amazon S3 Bucket is a resource for Amazon S3 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon S3 Bucket?
For Terraform, the mirnujAtom/terraform-aws-modern-application-workshop and jay-jain/aws-terraform source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the victorsalaun/mugshot-aws-serverless, org-formation/org-formation-cli and amit873/CloudFormation_Templates source code examples are useful. See the CloudFormation Example section for further details.