|
1 | 1 | import {Construct} from "constructs" |
2 | | -import {Duration} from "aws-cdk-lib" |
3 | | -import {PolicyStatement} from "aws-cdk-lib/aws-iam" |
4 | | -import {Bucket} from "aws-cdk-lib/aws-s3" |
| 2 | +import {Bucket, EventType} from "aws-cdk-lib/aws-s3" |
| 3 | +import {LambdaDestination} from "aws-cdk-lib/aws-s3-notifications" |
5 | 4 | import {Function as LambdaFunction} from "aws-cdk-lib/aws-lambda" |
6 | | -import {AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId} from "aws-cdk-lib/custom-resources" |
7 | 5 |
|
8 | 6 | export interface S3LambdaNotificationProps { |
9 | 7 | bucket: Bucket |
10 | 8 | lambdaFunction: LambdaFunction |
11 | | - events?: Array<string> |
12 | 9 | } |
13 | 10 |
|
14 | 11 | export class S3LambdaNotification extends Construct { |
15 | 12 | constructor(scope: Construct, id: string, props: S3LambdaNotificationProps) { |
16 | 13 | super(scope, id) |
17 | 14 |
|
18 | | - const events = props.events ?? ["s3:ObjectCreated:*"] |
19 | | - |
20 | | - // Create S3 bucket notification using custom resource |
21 | | - new AwsCustomResource(this, "BucketNotification", { |
22 | | - onCreate: { |
23 | | - service: "S3", |
24 | | - action: "putBucketNotificationConfiguration", |
25 | | - parameters: { |
26 | | - Bucket: props.bucket.bucketName, |
27 | | - NotificationConfiguration: { |
28 | | - LambdaConfigurations: [{ |
29 | | - Id: "LambdaNotification", |
30 | | - LambdaFunctionArn: props.lambdaFunction.functionArn, |
31 | | - Events: events |
32 | | - }] |
33 | | - } |
34 | | - }, |
35 | | - physicalResourceId: PhysicalResourceId.of(`${props.bucket.bucketName}-notification`) |
36 | | - }, |
37 | | - onDelete: { |
38 | | - service: "S3", |
39 | | - action: "putBucketNotificationConfiguration", |
40 | | - parameters: { |
41 | | - Bucket: props.bucket.bucketName, |
42 | | - NotificationConfiguration: {} |
43 | | - } |
44 | | - }, |
45 | | - policy: AwsCustomResourcePolicy.fromStatements([ |
46 | | - new PolicyStatement({ |
47 | | - actions: ["s3:PutBucketNotification", "s3:GetBucketNotification"], |
48 | | - resources: [props.bucket.bucketArn] |
49 | | - }), |
50 | | - new PolicyStatement({ |
51 | | - actions: ["lambda:AddPermission", "lambda:RemovePermission"], |
52 | | - resources: [props.lambdaFunction.functionArn] |
53 | | - }) |
54 | | - ]), |
55 | | - timeout: Duration.minutes(5) |
56 | | - }) |
57 | | - |
58 | | - // Add Lambda permission for S3 to invoke the function |
59 | | - new AwsCustomResource(this, "LambdaPermission", { |
60 | | - onCreate: { |
61 | | - service: "Lambda", |
62 | | - action: "addPermission", |
63 | | - parameters: { |
64 | | - FunctionName: props.lambdaFunction.functionName, |
65 | | - StatementId: "S3InvokePermission", |
66 | | - Action: "lambda:InvokeFunction", |
67 | | - Principal: "s3.amazonaws.com", |
68 | | - SourceArn: props.bucket.bucketArn |
69 | | - }, |
70 | | - physicalResourceId: PhysicalResourceId.of(`${props.lambdaFunction.functionName}-s3-permission`) |
71 | | - }, |
72 | | - onDelete: { |
73 | | - service: "Lambda", |
74 | | - action: "removePermission", |
75 | | - parameters: { |
76 | | - FunctionName: props.lambdaFunction.functionName, |
77 | | - StatementId: "S3InvokePermission" |
78 | | - } |
79 | | - }, |
80 | | - policy: AwsCustomResourcePolicy.fromStatements([ |
81 | | - new PolicyStatement({ |
82 | | - actions: ["lambda:AddPermission", "lambda:RemovePermission"], |
83 | | - resources: [props.lambdaFunction.functionArn] |
84 | | - }) |
85 | | - ]) |
86 | | - }) |
| 15 | + // Use CDK's built-in S3 notification |
| 16 | + props.bucket.addEventNotification( |
| 17 | + EventType.OBJECT_CREATED, |
| 18 | + new LambdaDestination(props.lambdaFunction) |
| 19 | + ) |
87 | 20 | } |
88 | 21 | } |
0 commit comments