Applicable when
- Custom logic should be invoked on S3 file upload (useful with Transfer Family SFTP server)
Implementation
First, you need to create the handler lambda
import { S3CreateEvent } from 'aws-lambda';
export async function handler(event: S3CreateEvent): Promise<void> {
await Promise.all(
event.Records.map(async record => {
// Filename: record.s3.object.key
})
);
}
Then you need to add the following CDK code
import { Bucket, EventType } from '@aws-cdk/aws-s3';
import { LambdaDestination } from '@aws-cdk/aws-s3-notifications';
import { DATA_BUCKET_TAG, withEnv } from '../util/consts';
const uploadBucketName = withEnv(DATA_BUCKET_TAG);
const uploadBucket = new Bucket(this, uploadBucketName, {
bucketName: uploadBucketName,
});
const triggerLambda = this.lambdas.createLambda('<handler>', 'S3PutObjectTrigger');
/*
* This will create a placeholder lambda named: <stack-name>-BucketNotificationsHandler-<id>. No clean workaround
* yet. See: https://github.com/aws/aws-cdk/issues/2781
*/
uploadBucket.addEventNotification(EventType.OBJECT_CREATED, new LambdaDestination(triggerLambda));
Comments
0 comments
Please sign in to leave a comment.