Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{

Check failure on line 1 in packages/@aws-cdk-testing/framework-integ/test/aws-s3/test/integ.bucket-auto-delete-objects.js.snapshot/cdk-s3-bucket-auto-delete-objects.template.json

View workflow job for this annotation

GitHub Actions / Security Guardian Results

iam-no-wildcard-actions-inline.guard

IAM_NO_WILDCARD_ACTIONS_INLINE for Type: Static
Raw output
Check was not compliant as property [Policies[*].PolicyDocument.Statement[*]] is missing. Value traversed to [Path=/Resources/CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092/Properties[L:88,C:17] Value={"AssumeRolePolicyDocument":{"Version":"2012-10-17","Statement":[{"Action":"sts:AssumeRole","Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"}}]},"ManagedPolicyArns":[{"Fn::Sub":"arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"}]}]
Check was not compliant as property [Policies[*].PolicyDocument.Statement[*]] is missing. Value traversed to [Path=/Resources/AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2/Properties[L:498,C:17] Value={"AssumeRolePolicyDocument":{"Statement":[{"Action":"sts:AssumeRole","Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"}}],"Version":"2012-10-17"},"ManagedPolicyArns":[{"Fn::Join":["",["arn:",{"Ref":"AWS::Partition"},":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]]}]}]

Check failure on line 1 in packages/@aws-cdk-testing/framework-integ/test/aws-s3/test/integ.bucket-auto-delete-objects.js.snapshot/cdk-s3-bucket-auto-delete-objects.template.json

View workflow job for this annotation

GitHub Actions / Security Guardian Results

s3-encryption-enabled.guard

S3_ENCRYPTION_ENABLED for Type: Static
Raw output
Check was not compliant as property [BucketEncryption] is missing. Value traversed to [Path=/Resources/Bucket83908E77/Properties[L:4,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration] is missing. Value traversed to [Path=/Resources/Bucket83908E77/Properties[L:4,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault] is missing. Value traversed to [Path=/Resources/Bucket83908E77/Properties[L:4,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption] is missing. Value traversed to [Path=/Resources/BucketViaMethodAF1D9165/Properties[L:146,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration] is missing. Value traversed to [Path=/Resources/BucketViaMethodAF1D9165/Properties[L:146,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault] is missing. Value traversed to [Path=/Resources/BucketViaMethodAF1D9165/Properties[L:146,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption] is missing. Value traversed to [Path=/Resources/RemovedBucket4FCCEBAD/Properties[L:352,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration] is missing. Value traversed to [Path=/Resources/RemovedBucket4FCCEBAD/Properties[L:352,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault] is missing. Value traversed to [Path=/Resources/RemovedBucket4FCCEBAD/Properties[L:352,C:17] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].

Check failure on line 1 in packages/@aws-cdk-testing/framework-integ/test/aws-s3/test/integ.bucket-auto-delete-objects.js.snapshot/cdk-s3-bucket-auto-delete-objects.template.json

View workflow job for this annotation

GitHub Actions / Security Guardian Results with resolved templates

iam-no-wildcard-actions-inline.guard

IAM_NO_WILDCARD_ACTIONS_INLINE for Type: Resolved
Raw output
Check was not compliant as property [Policies[*].PolicyDocument.Statement[*]] is missing. Value traversed to [Path=/Resources/CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092/Properties[L:56,C:20] Value={"AssumeRolePolicyDocument":{"Version":"2012-10-17","Statement":[{"Action":"sts:AssumeRole","Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"}}]},"ManagedPolicyArns":["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]}]
Check was not compliant as property [Policies[*].PolicyDocument.Statement[*]] is missing. Value traversed to [Path=/Resources/AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2/Properties[L:303,C:20] Value={"AssumeRolePolicyDocument":{"Version":"2012-10-17","Statement":[{"Action":"sts:AssumeRole","Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"}}]},"ManagedPolicyArns":["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]}]

Check failure on line 1 in packages/@aws-cdk-testing/framework-integ/test/aws-s3/test/integ.bucket-auto-delete-objects.js.snapshot/cdk-s3-bucket-auto-delete-objects.template.json

View workflow job for this annotation

GitHub Actions / Security Guardian Results with resolved templates

s3-encryption-enabled.guard

S3_ENCRYPTION_ENABLED for Type: Resolved
Raw output
Check was not compliant as property [BucketEncryption] is missing. Value traversed to [Path=/Resources/Bucket83908E77/Properties[L:4,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration] is missing. Value traversed to [Path=/Resources/Bucket83908E77/Properties[L:4,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault] is missing. Value traversed to [Path=/Resources/Bucket83908E77/Properties[L:4,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption] is missing. Value traversed to [Path=/Resources/BucketViaMethodAF1D9165/Properties[L:94,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration] is missing. Value traversed to [Path=/Resources/BucketViaMethodAF1D9165/Properties[L:94,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault] is missing. Value traversed to [Path=/Resources/BucketViaMethodAF1D9165/Properties[L:94,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption] is missing. Value traversed to [Path=/Resources/RemovedBucket4FCCEBAD/Properties[L:219,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration] is missing. Value traversed to [Path=/Resources/RemovedBucket4FCCEBAD/Properties[L:219,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
Check was not compliant as property [BucketEncryption.ServerSideEncryptionConfiguration[*].ServerSideEncryptionByDefault] is missing. Value traversed to [Path=/Resources/RemovedBucket4FCCEBAD/Properties[L:219,C:20] Value={"Tags":[{"Key":"aws-cdk:auto-delete-objects","Value":"true"}]}].
"Resources": {
"Bucket83908E77": {
"Type": "AWS::S3::Bucket",
Expand Down Expand Up @@ -142,6 +142,90 @@
"CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092"
]
},
"BucketViaMethodAF1D9165": {
"Type": "AWS::S3::Bucket",
"Properties": {
"Tags": [
{
"Key": "aws-cdk:auto-delete-objects",
"Value": "true"
}
]
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"BucketViaMethodPolicyDC86AE49": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "BucketViaMethodAF1D9165"
},
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:DeleteObject*",
"s3:GetBucket*",
"s3:List*",
"s3:PutBucketPolicy"
],
"Effect": "Allow",
"Principal": {
"AWS": {
"Fn::GetAtt": [
"CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092",
"Arn"
]
}
},
"Resource": [
{
"Fn::GetAtt": [
"BucketViaMethodAF1D9165",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"BucketViaMethodAF1D9165",
"Arn"
]
},
"/*"
]
]
}
]
}
],
"Version": "2012-10-17"
}
}
},
"BucketViaMethodAutoDeleteObjectsCustomResource9AB8AF36": {
"Type": "Custom::S3AutoDeleteObjects",
"Properties": {
"ServiceToken": {
"Fn::GetAtt": [
"CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F",
"Arn"
]
},
"BucketName": {
"Ref": "BucketViaMethodAF1D9165"
}
},
"DependsOn": [
"BucketViaMethodPolicyDC86AE49"
],
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"CustomS3PutObjectsCustomResourceProviderRole40D98C91": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -171,20 +255,36 @@
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": {
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"Bucket83908E77",
"Arn"
]
},
"/*"
"Resource": [
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"Bucket83908E77",
"Arn"
]
},
"/*"
]
]
]
}
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"BucketViaMethodAF1D9165",
"Arn"
]
},
"/*"
]
]
}
]
}
]
}
Expand Down Expand Up @@ -232,6 +332,22 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"PutObjectsMethodCustomResource": {
"Type": "Custom::S3PutObjects",
"Properties": {
"ServiceToken": {
"Fn::GetAtt": [
"CustomS3PutObjectsCustomResourceProviderHandler1D33F0A9",
"Arn"
]
},
"BucketName": {
"Ref": "BucketViaMethodAF1D9165"
}
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"RemovedBucket4FCCEBAD": {
"Type": "AWS::S3::Bucket",
"Properties": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ class TestStack extends Stack {
autoDeleteObjects: true,
});

const bucketViaMethod = new s3.Bucket(this, 'BucketViaMethod', {
removalPolicy: RemovalPolicy.DESTROY,
});

// Call the public method
bucketViaMethod.enableAutoDeleteObjects();

// Put objects in the bucket to ensure auto delete works as expected
const serviceToken = CustomResourceProvider.getOrCreate(this, PUT_OBJECTS_RESOURCE_TYPE, {
codeDirectory: path.join(__dirname, 'put-objects-handler'),
runtime: STANDARD_CUSTOM_RESOURCE_PROVIDER_RUNTIME,
policyStatements: [{
Effect: 'Allow',
Action: 's3:PutObject',
Resource: bucket.arnForObjects('*'),
Resource: [
bucket.arnForObjects('*'),
bucketViaMethod.arnForObjects('*'),
],
}],
});

new CustomResource(this, 'PutObjectsCustomResource', {
resourceType: PUT_OBJECTS_RESOURCE_TYPE,
serviceToken,
Expand All @@ -35,6 +46,14 @@ class TestStack extends Stack {
},
});

new CustomResource(this, 'PutObjectsMethodCustomResource', {
resourceType: PUT_OBJECTS_RESOURCE_TYPE,
serviceToken,
properties: {
BucketName: bucketViaMethod.bucketName,
},
});

const bucketThatWillBeRemoved = new s3.Bucket(this, 'RemovedBucket', {
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: true,
Expand Down
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/aws-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,16 @@ const bucket = new s3.Bucket(this, 'MyTempFileBucket', {
});
```

Alternatively, you can enable object auto-deletion after the bucket has been created, allowing for conditional or dynamic configuration:

```ts
const bucket = new s3.Bucket(this, 'MyTempFileBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
});

bucket.enableAutoDeleteObjects();
```

**Warning** if you have deployed a bucket with `autoDeleteObjects: true`,
switching this to `false` in a CDK version _before_ `1.126.0` will lead to
all objects in the bucket being deleted. Be sure to update your bucket resources
Expand Down
Loading
Loading