getBucketNotification function
- GetBucketNotificationArgs args, {
- InvokeOptions? options,
Provides details about the notification configuration of an S3 bucket.
Useful when aws.s3.BucketNotification is the right resource but the bucket already has notifications you do not manage. Read the existing notifications with this data source and re-emit them — alongside your own — in a single aws.s3.BucketNotification resource. See issue #501 for the longer story. For sharing a bucket across many independent consumers, enabling EventBridge on the resource is usually a better fit.
Example Usage
Basic Usage
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const example = aws.s3.getBucketNotification({
bucket: "example-bucket",
});
import pulumi
import pulumi_aws as aws
example = aws.s3.get_bucket_notification(bucket="example-bucket")
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(() =>
{
var example = Aws.S3.GetBucketNotification.Invoke(new()
{
Bucket = "example-bucket",
});
});
package main
import (
"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/s3"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := s3.LookupBucketNotification(ctx, &s3.LookupBucketNotificationArgs{
Bucket: "example-bucket",
}, nil)
if err != nil {
return err
}
return nil
})
}
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
}
}
data "aws_s3_getbucketnotification" "example" {
bucket = "example-bucket"
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.s3.S3Functions;
import com.pulumi.aws.s3.inputs.GetBucketNotificationArgs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
final var example = S3Functions.getBucketNotification(GetBucketNotificationArgs.builder()
.bucket("example-bucket")
.build());
}
}
variables:
example:
fn::invoke:
function: aws:s3:getBucketNotification
arguments:
bucket: example-bucket
Conditionally Subscribe via EventBridge
When the bucket forwards events to Amazon EventBridge, independent consumers can subscribe with their own aws.cloudwatch.EventRule resources. Use this data source to subscribe only when EventBridge is in fact enabled on the bucket.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
export = async () => {
const shared = await aws.s3.getBucketNotification({
bucket: "shared-bucket",
});
const s3ObjectCreated: aws.cloudwatch.EventRule[] = [];
for (let range = 0; range < (shared.eventbridge ? 1 : 0); range++) {
s3ObjectCreated.push(new aws.cloudwatch.EventRule(`s3_object_created-${range}`, {
name: "shared-bucket-object-created",
description: "S3 object-created events from the shared bucket.",
eventPattern: JSON.stringify({
source: ["aws.s3"],
"detail-type": ["Object Created"],
detail: {
bucket: {
name: [shared.bucket],
},
},
}),
}));
}
}
import pulumi
from typing import Any
import json
import pulumi_aws as aws
shared = aws.s3.get_bucket_notification(bucket="shared-bucket")
s3_object_created: list[aws.cloudwatch.EventRule] = []
for s3_object_created_range in [{"value": i} for i in range(0, 1 if shared.eventbridge else 0)]:
s3_object_created.append(aws.cloudwatch.EventRule(f"s3_object_created-{s3_object_created_range['value']}",
name="shared-bucket-object-created",
description="S3 object-created events from the shared bucket.",
event_pattern=json.dumps({
"source": ["aws.s3"],
"detail-type": ["Object Created"],
"detail": {
"bucket": {
"name": [shared.bucket],
},
},
})))
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(async() =>
{
var shared = await Aws.S3.GetBucketNotification.InvokeAsync(new()
{
Bucket = "shared-bucket",
});
var s3ObjectCreated = new List<Aws.CloudWatch.EventRule>();
for (var rangeIndex = 0; rangeIndex < shared.Eventbridge ? 1 : 0; rangeIndex++)
{
var range = new { Value = rangeIndex };
s3ObjectCreated.Add(new Aws.CloudWatch.EventRule($"s3_object_created-{range.Value}", new()
{
Name = "shared-bucket-object-created",
Description = "S3 object-created events from the shared bucket.",
EventPattern = JsonSerializer.Serialize(new Dictionary<string, object?>
{
["source"] = new[]
{
"aws.s3",
},
["detail-type"] = new[]
{
"Object Created",
},
["detail"] = new Dictionary<string, object?>
{
["bucket"] = new Dictionary<string, object?>
{
["name"] = new[]
{
shared.Bucket,
},
},
},
}),
}));
}
});
package main
import (
"encoding/json"
"fmt"
"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/cloudwatch"
"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/s3"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
shared, err := s3.LookupBucketNotification(ctx, &s3.LookupBucketNotificationArgs{
Bucket: "shared-bucket",
}, nil)
if err != nil {
return err
}
tmpJSON0, err := json.Marshal(map[string]interface{}{
"source": []string{
"aws.s3",
},
"detail-type": []string{
"Object Created",
},
"detail": map[string]map[string][]*string{
"bucket": map[string][]*string{
"name": []*string{
shared.Bucket,
},
},
},
})
if err != nil {
return err
}
json0 := string(tmpJSON0)
var tmp0 int
if pulumi.Bool(shared.Eventbridge) {
tmp0 = 1
} else {
tmp0 = 0
}
var s3ObjectCreated []*cloudwatch.EventRule
for index := 0; index < tmp0; index++ {
key0 := index
__res, err := cloudwatch.NewEventRule(ctx, fmt.Sprintf("s3_object_created-%v", key0), &cloudwatch.EventRuleArgs{
Name: pulumi.String("shared-bucket-object-created"),
Description: pulumi.String("S3 object-created events from the shared bucket."),
EventPattern: json0,
})
if err != nil {
return err
}
s3ObjectCreated = append(s3ObjectCreated, __res)
}
return nil
})
}
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
}
}
data "aws_s3_getbucketnotification" "shared" {
bucket = "shared-bucket"
}
resource "aws_cloudwatch_eventrule" "s3_object_created" {
count = data.aws_s3_getbucketnotification.shared.eventbridge ? 1 : 0
name = "shared-bucket-object-created"
description = "S3 object-created events from the shared bucket."
event_pattern = jsonencode({
"source" = ["aws.s3"]
"detail-type" = ["Object Created"]
"detail" = {
"bucket" = {
"name" = [data.aws_s3_getbucketnotification.shared.bucket]
}
}
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.s3.S3Functions;
import com.pulumi.aws.s3.inputs.GetBucketNotificationArgs;
import com.pulumi.aws.cloudwatch.EventRule;
import com.pulumi.aws.cloudwatch.EventRuleArgs;
import static com.pulumi.codegen.internal.Serialization.*;
import com.pulumi.codegen.internal.KeyedValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
final var shared = S3Functions.getBucketNotification(GetBucketNotificationArgs.builder()
.bucket("shared-bucket")
.build());
for (var i = 0; i < shared.eventbridge() ? 1 : 0; i++) {
new EventRule("s3ObjectCreated-" + i, EventRuleArgs.builder()
.name("shared-bucket-object-created")
.description("S3 object-created events from the shared bucket.")
.eventPattern(serializeJson(
jsonObject(
jsonProperty("source", jsonArray("aws.s3")),
jsonProperty("detail-type", jsonArray("Object Created")),
jsonProperty("detail", jsonObject(
jsonProperty("bucket", jsonObject(
jsonProperty("name", jsonArray(shared.bucket()))
))
))
)))
.build());
}
}
}
resources:
s3ObjectCreated:
type: aws:cloudwatch:EventRule
name: s3_object_created
properties:
name: shared-bucket-object-created
description: S3 object-created events from the shared bucket.
eventPattern:
fn::toJSON:
source:
- aws.s3
detail-type:
- Object Created
detail:
bucket:
name:
- ${shared.bucket}
options: {}
variables:
shared:
fn::invoke:
function: aws:s3:getBucketNotification
arguments:
bucket: shared-bucket
Read Existing Notifications and Re-emit Them
The S3 PutBucketNotificationConfiguration API replaces the entire notification configuration on every call, so a single aws.s3.BucketNotification resource owns the bucket. To preserve notifications already on the bucket — or to mirror one bucket's configuration onto another — read them with this data source and pass them through dynamic blocks. The data source's output shape matches the resource's input shape, so each block forwards directly.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const existing = aws.s3.getBucketNotification({
bucket: exampleAwsS3Bucket.id,
});
const example = new aws.s3.BucketNotification("example", {
lambdaFunctions: existing.then(existing => .map(entry => ({
id: entry.id,
lambdaFunctionArn: entry.lambdaFunctionArn,
events: entry.events,
filterPrefix: entry.filterPrefix,
filterSuffix: entry.filterSuffix,
}))),
queues: existing.then(existing => .map(entry2 => ({
id: entry2.id,
queueArn: entry2.queueArn,
events: entry2.events,
filterPrefix: entry2.filterPrefix,
filterSuffix: entry2.filterSuffix,
}))),
topics: existing.then(existing => .map(entry3 => ({
id: entry3.id,
topicArn: entry3.topicArn,
events: entry3.events,
filterPrefix: entry3.filterPrefix,
filterSuffix: entry3.filterSuffix,
}))),
bucket: exampleAwsS3Bucket.id,
eventbridge: existing.then(existing => existing.eventbridge),
});
import pulumi
import pulumi_aws as aws
existing = aws.s3.get_bucket_notification(bucket=example_aws_s3_bucket["id"])
example = aws.s3.BucketNotification("example",
lambda_functions=[{
"id": entry.id,
"lambda_function_arn": entry.lambda_function_arn,
"events": entry.events,
"filter_prefix": entry.filter_prefix,
"filter_suffix": entry.filter_suffix,
} for entry in existing.lambda_functions],
queues=[{
"id": entry2.id,
"queue_arn": entry2.queue_arn,
"events": entry2.events,
"filter_prefix": entry2.filter_prefix,
"filter_suffix": entry2.filter_suffix,
} for entry2 in existing.queues],
topics=[{
"id": entry3.id,
"topic_arn": entry3.topic_arn,
"events": entry3.events,
"filter_prefix": entry3.filter_prefix,
"filter_suffix": entry3.filter_suffix,
} for entry3 in existing.topics],
bucket=example_aws_s3_bucket["id"],
eventbridge=existing.eventbridge)
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(() =>
{
var existing = Aws.S3.GetBucketNotification.Invoke(new()
{
Bucket = exampleAwsS3Bucket.Id,
});
var example = new Aws.S3.BucketNotification("example", new()
{
LambdaFunctions = .Select(entry =>
{
return
{
{ "id", entry.Id },
{ "lambdaFunctionArn", entry.LambdaFunctionArn },
{ "events", entry.Events },
{ "filterPrefix", entry.FilterPrefix },
{ "filterSuffix", entry.FilterSuffix },
};
}).ToList(),
Queues = .Select(entry2 =>
{
return
{
{ "id", entry2.Id },
{ "queueArn", entry2.QueueArn },
{ "events", entry2.Events },
{ "filterPrefix", entry2.FilterPrefix },
{ "filterSuffix", entry2.FilterSuffix },
};
}).ToList(),
Topics = .Select(entry3 =>
{
return
{
{ "id", entry3.Id },
{ "topicArn", entry3.TopicArn },
{ "events", entry3.Events },
{ "filterPrefix", entry3.FilterPrefix },
{ "filterSuffix", entry3.FilterSuffix },
};
}).ToList(),
Bucket = exampleAwsS3Bucket.Id,
Eventbridge = existing.Apply(getBucketNotificationResult => getBucketNotificationResult.Eventbridge),
});
});
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
}
}
data "aws_s3_getbucketnotification" "existing" {
bucket = exampleAwsS3Bucket.id
}
resource "aws_s3_bucketnotification" "example" {
dynamic "lambda_functions" {
for_each = data.aws_s3_getbucketnotification.existing.lambda_functions
content {
id = lambda_functions.value.id
lambda_function_arn = lambda_functions.value.lambdaFunctionArn
events = lambda_functions.value.events
filter_prefix = lambda_functions.value.filterPrefix
filter_suffix = lambda_functions.value.filterSuffix
}
}
dynamic "queues" {
for_each = data.aws_s3_getbucketnotification.existing.queues
content {
id = queues.value.id
queue_arn = queues.value.queueArn
events = queues.value.events
filter_prefix = queues.value.filterPrefix
filter_suffix = queues.value.filterSuffix
}
}
dynamic "topics" {
for_each = data.aws_s3_getbucketnotification.existing.topics
content {
id = topics.value.id
topic_arn = topics.value.topicArn
events = topics.value.events
filter_prefix = topics.value.filterPrefix
filter_suffix = topics.value.filterSuffix
}
}
bucket = exampleAwsS3Bucket.id
eventbridge = data.aws_s3_getbucketnotification.existing.eventbridge
}
To add a new rule alongside existing ones, exclude IDs your resource owns from the iteration to avoid duplicates, and declare those rules separately:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const example = new aws.s3.BucketNotification("example", {
lambdaFunctions: [{
id: "my-team-rule",
lambdaFunctionArn: mine.arn,
events: ["s3:ObjectRemoved:*"],
}],
bucket: exampleAwsS3Bucket.id,
});
import pulumi
import pulumi_aws as aws
example = aws.s3.BucketNotification("example",
lambda_functions=[{
"id": "my-team-rule",
"lambda_function_arn": mine["arn"],
"events": ["s3:ObjectRemoved:*"],
}],
bucket=example_aws_s3_bucket["id"])
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(() =>
{
var example = new Aws.S3.BucketNotification("example", new()
{
LambdaFunctions = new[]
{
new Aws.S3.Inputs.BucketNotificationLambdaFunctionArgs
{
Id = "my-team-rule",
LambdaFunctionArn = mine.Arn,
Events = new[]
{
"s3:ObjectRemoved:*",
},
},
},
Bucket = exampleAwsS3Bucket.Id,
});
});
package main
import (
"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/s3"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := s3.NewBucketNotification(ctx, "example", &s3.BucketNotificationArgs{
LambdaFunctions: s3.BucketNotificationLambdaFunctionArray{
&s3.BucketNotificationLambdaFunctionArgs{
Id: pulumi.String("my-team-rule"),
LambdaFunctionArn: pulumi.Any(mine.Arn),
Events: pulumi.StringArray{
pulumi.String("s3:ObjectRemoved:*"),
},
},
},
Bucket: pulumi.Any(exampleAwsS3Bucket.Id),
})
if err != nil {
return err
}
return nil
})
}
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
}
}
resource "aws_s3_bucketnotification" "example" {
lambda_functions {
id = "my-team-rule"
lambda_function_arn = mine.arn
events = ["s3:ObjectRemoved:*"]
}
bucket = exampleAwsS3Bucket.id
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.s3.BucketNotification;
import com.pulumi.aws.s3.BucketNotificationArgs;
import com.pulumi.aws.s3.inputs.BucketNotificationLambdaFunctionArgs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var example = new BucketNotification("example", BucketNotificationArgs.builder()
.lambdaFunctions(BucketNotificationLambdaFunctionArgs.builder()
.id("my-team-rule")
.lambdaFunctionArn(mine.arn())
.events("s3:ObjectRemoved:*")
.build())
.bucket(exampleAwsS3Bucket.id())
.build());
}
}
resources:
example:
type: aws:s3:BucketNotification
properties:
lambdaFunctions:
- id: my-team-rule
lambdaFunctionArn: ${mine.arn}
events:
- s3:ObjectRemoved:*
bucket: ${exampleAwsS3Bucket.id}
> Note: The S3 API has no per-rule mutation primitive and no compare-and-swap, so two pulumi up runs from different state files writing to the same bucket can still race. For independent consumers of one bucket, EventBridge is generally a better fit.
args Arguments passed to this invoke. Arguments for getBucketNotification.
options Invoke options controlling this call.
Implementation
Future<GetBucketNotificationResult> getBucketNotification(
GetBucketNotificationArgs args, {
pulumi.InvokeOptions? options,
}) async {
final deployment = pulumi.Deployment.instance;
final result = await deployment.invoke<Map<String, dynamic>>(
'aws:s3/getBucketNotification:getBucketNotification',
args.toMap(),
options: pulumi.toDeploymentInvokeOptions(options),
);
return GetBucketNotificationResult.fromMap(result);
}