Skip to content

Commit

Permalink
put objects in integ test
Browse files Browse the repository at this point in the history
  • Loading branch information
jogold committed Dec 25, 2020
1 parent 328d213 commit 71d771c
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3Bucket54B0BA6B"
"Ref": "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3BucketF01ADF6B"
},
"S3Key": {
"Fn::Join": [
Expand All @@ -116,7 +116,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3VersionKey5D05A836"
"Ref": "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3VersionKey6FC34F51"
}
]
}
Expand All @@ -129,7 +129,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters94e60b8e13d1f4ded38f4813a8053fbe137c982a0b3c929daa8166d33f844316S3VersionKey5D05A836"
"Ref": "AssetParameters1a8becf42c48697a059094af1e94aa6bc6df0512d30433db8c22618ca02dfca1S3VersionKey6FC34F51"
}
]
}
Expand All @@ -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\""
}
}
}
25 changes: 23 additions & 2 deletions packages/@aws-cdk/aws-s3/test/integ.bucket-auto-delete-objects.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-s3/test/put-objects-handler/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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()));
}

0 comments on commit 71d771c

Please sign in to comment.