Created
July 16, 2021 11:50
-
-
Save alexkates/07f7aa1ba80bfd5b20412da2e0164b09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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