Skip to content

Commit d019ac1

Browse files
committed
Use CDK built-in S3 notificati in S3LambdaNotification construct
1 parent 947be6a commit d019ac1

File tree

1 file changed

+7
-74
lines changed

1 file changed

+7
-74
lines changed
Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,21 @@
11
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"
54
import {Function as LambdaFunction} from "aws-cdk-lib/aws-lambda"
6-
import {AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId} from "aws-cdk-lib/custom-resources"
75

86
export interface S3LambdaNotificationProps {
97
bucket: Bucket
108
lambdaFunction: LambdaFunction
11-
events?: Array<string>
129
}
1310

1411
export class S3LambdaNotification extends Construct {
1512
constructor(scope: Construct, id: string, props: S3LambdaNotificationProps) {
1613
super(scope, id)
1714

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+
)
8720
}
8821
}

0 commit comments

Comments
 (0)