Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexkates/07f7aa1ba80bfd5b20412da2e0164b09 to your computer and use it in GitHub Desktop.
Save alexkates/07f7aa1ba80bfd5b20412da2e0164b09 to your computer and use it in GitHub Desktop.
// Import aws-cdk packages
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import * as lambda from '@aws-cdk/aws-lambda';
import * as lambdaEventSources from '@aws-cdk/aws-lambda-event-sources';
export class HowToTriggerLambdaFromS3Stack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a new S3 bucket
const bucket = new s3.Bucket(this, 'OurBucket', {
/**
* The following properties ensure the bucket is properly
* deleted when we run cdk destroy */
autoDeleteObjects: true,
removalPolicy: cdk.RemovalPolicy.DESTROY
});
// Create a new Lambda Function
const lambdaFunction = new lambda.Function(this, 'Function', {
code: lambda.Code.fromAsset('src'),
handler: 'index.handler',
functionName: 'BucketPutHandler',
runtime: lambda.Runtime.NODEJS_12_X,
});
// Create a new S3 Event Source
const s3PutEventSource = new lambdaEventSources.S3EventSource(bucket, {
events: [
s3.EventType.OBJECT_CREATED_PUT
]
});
// Attach the S3 event source to the Lambda Function
lambdaFunction.addEventSource(s3PutEventSource);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment