AWS Amazon S3 Bucket Notification

This page shows how to write Terraform and CloudFormation for Amazon S3 Bucket Notification and write them securely.

aws_s3_bucket_notification (Terraform)

The Bucket Notification in Amazon S3 can be configured in Terraform with the resource name aws_s3_bucket_notification. The following sections describe 2 examples of how to use the resource and its parameters.

Example Usage from GitHub

notification.tf#L37
resource "aws_s3_bucket_notification" "create_songs" {
  bucket = aws_s3_bucket.songs_reference.id

  topic {
    id        = "NotifyCreated"
    topic_arn = aws_sns_topic.s3_notification.arn
main.tf#L9
resource "aws_s3_bucket_notification" "bucket_notification" {
  count = var.enable_bucket_notification == "true" && var.s3_trigger_lambda == "true" ? 1:0
  bucket = aws_s3_bucket.aws_s3_bucket[0].id

  lambda_function {
    lambda_function_arn = var.lambda_arn

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages a S3 Bucket Notification Configuration. For additional information, see the Configuring S3 Event Notifications section in the Amazon S3 Developer Guide.

NOTE: S3 Buckets only support a single notification configuration. Declaring multiple aws_s3_bucket_notification resources to the same S3 Bucket will cause a perpetual difference in configuration. See the example "Trigger multiple Lambda functions" for an option.

Tips: Best Practices for The Other AWS Amazon S3 Resources

In addition to the aws_s3_bucket_public_access_block, AWS Amazon S3 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_s3_bucket_public_access_block

Ensure S3 bucket-level Public Access Block restricts public bucket policies

It is better to enable S3 bucket-level Public Access Block if you don't need public buckets.

risk-label

aws_s3_bucket

Ensure S3 bucket access policy is well configured

It is better to configure the S3 bucket access policy properly to limit it unless explicitly required.

Review your AWS Amazon S3 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

AWS::S3::Bucket NotificationConfiguration (CloudFormation)

The Bucket NotificationConfiguration in S3 can be configured in CloudFormation with the resource name AWS::S3::Bucket NotificationConfiguration. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

An example could not be found in GitHub.

Parameters

LambdaConfigurations Describes the AWS Lambda functions to invoke and the events for which to invoke them.
Required: No
Type: List of LambdaConfiguration
Update requires: No interruption

QueueConfigurations The Amazon Simple Queue Service queues to publish messages to and the events for which to publish messages.
Required: No
Type: List of QueueConfiguration
Update requires: No interruption

TopicConfigurations The topic to which notifications are sent and the events for which notifications are generated.
Required: No
Type: List of TopicConfiguration
Update requires: No interruption

Explanation in CloudFormation Registry

Describes the notification configuration for an Amazon S3 bucket.

Note If you create the target resource and related permissions in the same template, you might have a circular dependency. For example, you might use the AWS::Lambda::Permission resource to grant the bucket permission to invoke an AWS Lambda function. However, AWS CloudFormation can't create the bucket until the bucket has permission to invoke the function (AWS CloudFormation checks whether the bucket can invoke the function). If you're using Refs to pass the bucket name, this leads to a circular dependency. To avoid this dependency, you can create all resources without specifying the notification configuration. Then, update the stack with a notification configuration. For more information on permissions, see AWS::Lambda::Permission and Granting Permissions to Publish Event Notification Messages to a Destination.

Frequently asked questions

What is AWS Amazon S3 Bucket Notification?

AWS Amazon S3 Bucket Notification is a resource for Amazon S3 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS Amazon S3 Bucket Notification?

For Terraform, the unipv-ce18/harmony and reachmenetym/terraform-s3-lambda source code examples are useful. See the Terraform Example section for further details.