S3 Bucket Notifications Destinations
This module includes integration classes for using Topics, Queues or Lambdas as S3 Notification Destinations.
Examples
The following example shows how to send a notification to an SNS topic when an object is created in an S3 bucket:
ts
import * as sns from 'aws-cdk-lib/aws-sns';
const bucket = new s3.Bucket(this, 'Bucket');
const topic = new sns.Topic(this, 'Topic');
bucket.addEventNotification(s3.EventType.OBJECT_CREATED_PUT, new s3n.SnsDestination(topic));
python
import aws_cdk.aws_sns as sns
bucket = s3.Bucket(self, "Bucket")
topic = sns.Topic(self, "Topic")
bucket.add_event_notification(s3.EventType.OBJECT_CREATED_PUT, s3n.SnsDestination(topic))
java
import software.amazon.awscdk.services.sns.*;
Bucket bucket = new Bucket(this, "Bucket");
Topic topic = new Topic(this, "Topic");
bucket.addEventNotification(EventType.OBJECT_CREATED_PUT, new SnsDestination(topic));
csharp
using Amazon.CDK.AWS.SNS;
var bucket = new Bucket(this, "Bucket");
var topic = new Topic(this, "Topic");
bucket.AddEventNotification(EventType.OBJECT_CREATED_PUT, new SnsDestination(topic));
go
import sns "github.com/aws/aws-cdk-go/awscdk"
bucket := s3.NewBucket(this, jsii.String("Bucket"))
topic := sns.NewTopic(this, jsii.String("Topic"))
bucket.AddEventNotification(s3.EventType_OBJECT_CREATED_PUT, s3n.NewSnsDestination(topic))
The following example shows how to send a notification to an SQS queue when an object is created in an S3 bucket:
ts
import * as sqs from 'aws-cdk-lib/aws-sqs';
const bucket = new s3.Bucket(this, 'Bucket');
const queue = new sqs.Queue(this, 'Queue');
bucket.addEventNotification(s3.EventType.OBJECT_CREATED_PUT, new s3n.SqsDestination(queue));
python
import aws_cdk.aws_sqs as sqs
bucket = s3.Bucket(self, "Bucket")
queue = sqs.Queue(self, "Queue")
bucket.add_event_notification(s3.EventType.OBJECT_CREATED_PUT, s3n.SqsDestination(queue))
java
import software.amazon.awscdk.services.sqs.*;
Bucket bucket = new Bucket(this, "Bucket");
Queue queue = new Queue(this, "Queue");
bucket.addEventNotification(EventType.OBJECT_CREATED_PUT, new SqsDestination(queue));
csharp
using Amazon.CDK.AWS.SQS;
var bucket = new Bucket(this, "Bucket");
var queue = new Queue(this, "Queue");
bucket.AddEventNotification(EventType.OBJECT_CREATED_PUT, new SqsDestination(queue));
go
import sqs "github.com/aws/aws-cdk-go/awscdk"
bucket := s3.NewBucket(this, jsii.String("Bucket"))
queue := sqs.NewQueue(this, jsii.String("Queue"))
bucket.AddEventNotification(s3.EventType_OBJECT_CREATED_PUT, s3n.NewSqsDestination(queue))
The following example shows how to send a notification to a Lambda function when an object is created in an S3 bucket:
ts
import * as lambda from 'aws-cdk-lib/aws-lambda';
const bucket = new s3.Bucket(this, 'Bucket');
const fn = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
});
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.LambdaDestination(fn));
python
import aws_cdk.aws_lambda as lambda_
bucket = s3.Bucket(self, "Bucket")
fn = lambda_.Function(self, "MyFunction",
runtime=lambda_.Runtime.NODEJS_LATEST,
handler="index.handler",
code=lambda_.Code.from_asset(path.join(__dirname, "lambda-handler"))
)
bucket.add_event_notification(s3.EventType.OBJECT_CREATED, s3n.LambdaDestination(fn))
java
import software.amazon.awscdk.services.lambda.*;
Bucket bucket = new Bucket(this, "Bucket");
Function fn = Function.Builder.create(this, "MyFunction")
.runtime(Runtime.NODEJS_LATEST)
.handler("index.handler")
.code(Code.fromAsset(join(__dirname, "lambda-handler")))
.build();
bucket.addEventNotification(EventType.OBJECT_CREATED, new LambdaDestination(fn));
csharp
using Amazon.CDK.AWS.Lambda;
var bucket = new Bucket(this, "Bucket");
var fn = new Function(this, "MyFunction", new FunctionProps {
Runtime = Runtime.NODEJS_LATEST,
Handler = "index.handler",
Code = Code.FromAsset(Join(__dirname, "lambda-handler"))
});
bucket.AddEventNotification(EventType.OBJECT_CREATED, new LambdaDestination(fn));
go
import "github.com/aws/aws-cdk-go/awscdk"
bucket := s3.NewBucket(this, jsii.String("Bucket"))
fn := lambda.NewFunction(this, jsii.String("MyFunction"), &FunctionProps{
Runtime: lambda.Runtime_NODEJS_LATEST(),
Handler: jsii.String("index.handler"),
Code: lambda.Code_FromAsset(path.join(__dirname, jsii.String("lambda-handler"))),
})
bucket.AddEventNotification(s3.EventType_OBJECT_CREATED, s3n.NewLambdaDestination(fn))
Types
| Name | Kind | Summary |
|---|---|---|
LambdaDestination | Class | Use a Lambda function as a bucket notification destination. |
SnsDestination | Class | Use an SNS topic as a bucket notification destination. |
SqsDestination | Class | Use an SQS queue as a bucket notification destination. |