From 71d771c60c784d6ae981d40de4c42b45ed064148 Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Fri, 25 Dec 2020 14:17:24 +0100 Subject: [PATCH] put objects in integ test --- .../lib/auto-delete-objects-handler/index.ts | 4 +- ...g.bucket-auto-delete-objects.expected.json | 152 ++++++++++++++++-- .../test/integ.bucket-auto-delete-objects.ts | 25 ++- .../aws-s3/test/put-objects-handler/index.ts | 28 ++++ 4 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 packages/@aws-cdk/aws-s3/test/put-objects-handler/index.ts diff --git a/packages/@aws-cdk/aws-s3/lib/auto-delete-objects-handler/index.ts b/packages/@aws-cdk/aws-s3/lib/auto-delete-objects-handler/index.ts index d3a2e0e616eb2..5dd144b446e8e 100644 --- a/packages/@aws-cdk/aws-s3/lib/auto-delete-objects-handler/index.ts +++ b/packages/@aws-cdk/aws-s3/lib/auto-delete-objects-handler/index.ts @@ -1,7 +1,7 @@ // eslint-disable-next-line import/no-extraneous-dependencies -import * as AWS from 'aws-sdk'; +import { S3 } from 'aws-sdk'; -const s3 = new AWS.S3(); +const s3 = new S3(); export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent) { switch (event.RequestType) { diff --git a/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.expected.json b/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.expected.json index b4f691d566a7c..d9f263a8d840d 100644 --- a/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.expected.json +++ b/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.expected.json @@ -103,7 +103,7 @@ "Properties": { "Code": { "S3Bucket": { - "Ref": "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3Bucket54B0BA6B" + "Ref": "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3BucketF01ADF6B" }, "S3Key": { "Fn::Join": [ @@ -116,7 +116,7 @@ "Fn::Split": [ "||", { - "Ref": "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3VersionKey5D05A836" + "Ref": "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3VersionKey6FC34F51" } ] } @@ -129,7 +129,7 @@ "Fn::Split": [ "||", { - "Ref": "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3VersionKey5D05A836" + "Ref": "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3VersionKey6FC34F51" } ] } @@ -153,20 +153,154 @@ "DependsOn": [ "CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092" ] + }, + "CustomS3PutObjectsCustomResourceProviderRole40D98C91": { + "Type": "AWS::IAM::Role", + "Properties": { + "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" + } + ], + "Policies": [ + { + "PolicyName": "Inline", + "PolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": "s3:PutObject", + "Resource": { + "Fn::Join": [ + "", + [ + { + "Fn::GetAtt": [ + "Bucket83908E77", + "Arn" + ] + }, + "/*" + ] + ] + } + } + ] + } + } + ] + } + }, + "CustomS3PutObjectsCustomResourceProviderHandler1D33F0A9": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": { + "Ref": "AssetParametersf7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653S3BucketDB5FAF47" + }, + "S3Key": { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 0, + { + "Fn::Split": [ + "||", + { + "Ref": "AssetParametersf7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653S3VersionKey9809F0E6" + } + ] + } + ] + }, + { + "Fn::Select": [ + 1, + { + "Fn::Split": [ + "||", + { + "Ref": "AssetParametersf7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653S3VersionKey9809F0E6" + } + ] + } + ] + } + ] + ] + } + }, + "Timeout": 900, + "MemorySize": 128, + "Handler": "__entrypoint__.handler", + "Role": { + "Fn::GetAtt": [ + "CustomS3PutObjectsCustomResourceProviderRole40D98C91", + "Arn" + ] + }, + "Runtime": "nodejs12.x" + }, + "DependsOn": [ + "CustomS3PutObjectsCustomResourceProviderRole40D98C91" + ] + }, + "PutObjectsCustomResource": { + "Type": "Custom::S3PutObjects", + "Properties": { + "ServiceToken": { + "Fn::GetAtt": [ + "CustomS3PutObjectsCustomResourceProviderHandler1D33F0A9", + "Arn" + ] + }, + "BucketName": { + "Ref": "Bucket83908E77" + } + }, + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Delete" } }, "Parameters": { - "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3Bucket54B0BA6B": { + "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3BucketF01ADF6B": { + "Type": "String", + "Description": "S3 bucket for asset \"1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1\"" + }, + "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3VersionKey6FC34F51": { + "Type": "String", + "Description": "S3 key for asset version \"1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1\"" + }, + "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1ArtifactHash9ECACDFD": { + "Type": "String", + "Description": "Artifact hash for asset \"1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1\"" + }, + "AssetParametersf7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653S3BucketDB5FAF47": { "Type": "String", - "Description": "S3 bucket for asset \"94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316\"" + "Description": "S3 bucket for asset \"f7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653\"" }, - "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3VersionKey5D05A836": { + "AssetParametersf7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653S3VersionKey9809F0E6": { "Type": "String", - "Description": "S3 key for asset version \"94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316\"" + "Description": "S3 key for asset version \"f7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653\"" }, - "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316ArtifactHashCBB49470": { + "AssetParametersf7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653ArtifactHash7CDE16B1": { "Type": "String", - "Description": "Artifact hash for asset \"94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316\"" + "Description": "Artifact hash for asset \"f7ee44e9b6217d201200d9abd42c6493b0b11e86be8a7f36163c3ea049c54653\"" } } } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.ts b/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.ts index 70f3197aaa07f..83243212409d7 100644 --- a/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.ts +++ b/packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.ts @@ -1,15 +1,36 @@ -import { App, RemovalPolicy, Stack, StackProps } from '@aws-cdk/core'; +import * as path from 'path'; +import { App, CustomResource, CustomResourceProvider, CustomResourceProviderRuntime, RemovalPolicy, Stack, StackProps } from '@aws-cdk/core'; import { Construct } from 'constructs'; import * as s3 from '../lib'; +const PUT_OBJECTS_RESOURCE_TYPE = 'Custom::S3PutObjects'; + class TestStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); - new s3.Bucket(this, 'Bucket', { + const bucket = new s3.Bucket(this, 'Bucket', { removalPolicy: RemovalPolicy.DESTROY, autoDeleteObjects: true, }); + + // 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: CustomResourceProviderRuntime.NODEJS_12, + policyStatements: [{ + Effect: 'Allow', + Action: 's3:PutObject', + Resource: bucket.arnForObjects('*'), + }], + }); + new CustomResource(this, 'PutObjectsCustomResource', { + resourceType: PUT_OBJECTS_RESOURCE_TYPE, + serviceToken, + properties: { + BucketName: bucket.bucketName, + }, + }); } } diff --git a/packages/@aws-cdk/aws-s3/test/put-objects-handler/index.ts b/packages/@aws-cdk/aws-s3/test/put-objects-handler/index.ts new file mode 100644 index 0000000000000..d4137760eb591 --- /dev/null +++ b/packages/@aws-cdk/aws-s3/test/put-objects-handler/index.ts @@ -0,0 +1,28 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { S3 } from 'aws-sdk'; + +const s3 = new S3(); + +export async function handler(event: AWSLambda.CloudFormationCustomResourceEvent): Promise { + switch (event.RequestType) { + case 'Create': + const bucketName = event.ResourceProperties.BucketName; + if (!bucketName) { + throw new Error('Missing BucketName'); + } + return putObjects(bucketName); + case 'Update': + case 'Delete': + return; + } +} + +async function putObjects(bucketName: string, n = 5) { + // Put n objects in parallel + await Promise.all([...Array(n).keys()] + .map(key => s3.putObject({ + Bucket: bucketName, + Key: `Key${key}`, + Body: `Body${key}`, + }).promise())); +}