github.com/keshavdv/terraform@v0.7.0-rc2.0.20160711232630-d69256dcb425/website/source/docs/providers/aws/r/s3_bucket_notification.html.markdown (about) 1 --- 2 layout: "aws" 3 page_title: "AWS: aws_s3_bucket_notification" 4 side_bar_current: "docs-aws-resource-s3-bucket-notification" 5 description: |- 6 Provides a S3 bucket notification resource. 7 --- 8 9 # aws\_s3\_bucket\_notification 10 11 Provides a S3 bucket notification resource. 12 13 ## Example Usage 14 15 ### Add notification configuration to SNS Topic 16 17 ``` 18 resource "aws_sns_topic" "topic" { 19 name = "s3-event-notification-topic" 20 policy = <<POLICY 21 { 22 "Version":"2012-10-17", 23 "Statement":[{ 24 "Effect": "Allow", 25 "Principal": {"AWS":"*"}, 26 "Action": "SNS:Publish", 27 "Resource": "arn:aws:sns:*:*:s3-event-notification-topic", 28 "Condition":{ 29 "ArnLike":{"aws:SourceArn":"${aws_s3_bucket.bucket.arn}"} 30 } 31 }] 32 } 33 POLICY 34 } 35 36 resource "aws_s3_bucket" "bucket" { 37 bucket = "your_bucket_name" 38 } 39 40 resource "aws_s3_bucket_notification" "bucket_notification" { 41 bucket = "${aws_s3_bucket.bucket.id}" 42 topic { 43 topic_arn = "${aws_sns_topic.topic.arn}" 44 events = ["s3:ObjectCreated:*"] 45 filter_suffix = ".log" 46 } 47 } 48 ``` 49 50 ### Add notification configuration to SQS Queue 51 52 ``` 53 resource "aws_sqs_queue" "queue" { 54 name = "s3-event-notification-queue" 55 policy = <<POLICY 56 { 57 "Version": "2012-10-17", 58 "Statement": [ 59 { 60 "Effect": "Allow", 61 "Principal": "*", 62 "Action": "sqs:SendMessage", 63 "Resource": "arn:aws:sqs:*:*:s3-event-notification-queue", 64 "Condition": { 65 "ArnEquals": { "aws:SourceArn": "${aws_s3_bucket.bucket.arn}" } 66 } 67 } 68 ] 69 } 70 POLICY 71 } 72 73 resource "aws_s3_bucket" "bucket" { 74 bucket = "your_bucket_name" 75 } 76 77 resource "aws_s3_bucket_notification" "bucket_notification" { 78 bucket = "${aws_s3_bucket.bucket.id}" 79 queue { 80 queue_arn = "${aws_sqs_queue.queue.arn}" 81 events = ["s3:ObjectCreated:*"] 82 filter_suffix = ".log" 83 } 84 } 85 ``` 86 87 ### Add notification configuration to Lambda Function 88 89 ``` 90 resource "aws_iam_role" "iam_for_lambda" { 91 name = "iam_for_lambda" 92 assume_role_policy = <<EOF 93 { 94 "Version": "2012-10-17", 95 "Statement": [ 96 { 97 "Action": "sts:AssumeRole", 98 "Principal": { 99 "Service": "lambda.amazonaws.com" 100 }, 101 "Effect": "Allow" 102 } 103 ] 104 } 105 EOF 106 } 107 108 resource "aws_lambda_permission" "allow_bucket" { 109 statement_id = "AllowExecutionFromS3Bucket" 110 action = "lambda:InvokeFunction" 111 function_name = "${aws_lambda_function.func.arn}" 112 principal = "s3.amazonaws.com" 113 source_arn = "${aws_s3_bucket.bucket.arn}" 114 } 115 116 resource "aws_lambda_function" "func" { 117 filename = "your-function.zip" 118 function_name = "example_lambda_name" 119 role = "${aws_iam_role.iam_for_lambda.arn}" 120 handler = "exports.example" 121 } 122 123 resource "aws_s3_bucket" "bucket" { 124 bucket = "your_bucket_name" 125 } 126 127 resource "aws_s3_bucket_notification" "bucket_notification" { 128 bucket = "${aws_s3_bucket.bucket.id}" 129 lambda_function { 130 lambda_function_arn = "${aws_lambda_function.func.arn}" 131 events = ["s3:ObjectCreated:*"] 132 filter_prefix = "AWSLogs/" 133 filter_suffix = ".log" 134 } 135 } 136 ``` 137 138 ### Add multiple notification configurations to SQS Queue 139 140 ``` 141 resource "aws_sqs_queue" "queue" { 142 name = "s3-event-notification-queue" 143 policy = <<POLICY 144 { 145 "Version": "2012-10-17", 146 "Statement": [ 147 { 148 "Effect": "Allow", 149 "Principal": "*", 150 "Action": "sqs:SendMessage", 151 "Resource": "arn:aws:sqs:*:*:s3-event-notification-queue", 152 "Condition": { 153 "ArnEquals": { "aws:SourceArn": "${aws_s3_bucket.bucket.arn}" } 154 } 155 } 156 ] 157 } 158 POLICY 159 } 160 161 resource "aws_s3_bucket" "bucket" { 162 bucket = "your_bucket_name" 163 } 164 165 resource "aws_s3_bucket_notification" "bucket_notification" { 166 bucket = "${aws_s3_bucket.bucket.id}" 167 queue { 168 id = "image-upload-event" 169 queue_arn = "${aws_sqs_queue.queue.arn}" 170 events = ["s3:ObjectCreated:*"] 171 filter_prefix = "images/" 172 } 173 queue { 174 id = "video-upload-event" 175 queue_arn = "${aws_sqs_queue.queue.arn}" 176 events = ["s3:ObjectCreated:*"] 177 filter_prefix = "videos/" 178 } 179 } 180 ``` 181 182 For Terraform's [JSON syntax](https://www.terraform.io/docs/configuration/syntax.html), use an array instead of defining the `queue` key twice. 183 184 ``` 185 { 186 "bucket": "${aws_s3_bucket.bucket.id}", 187 "queue": [ 188 { 189 "id": "image-upload-event", 190 "queue_arn": "${aws_sqs_queue.queue.arn}", 191 "events": ["s3:ObjectCreated:*"], 192 "filter_prefix": "images/" 193 }, 194 { 195 "id": "video-upload-event", 196 "queue_arn": "${aws_sqs_queue.queue.arn}", 197 "events": ["s3:ObjectCreated:*"], 198 "filter_prefix": "videos/" 199 } 200 ] 201 } 202 ``` 203 204 ## Argument Reference 205 206 The following arguments are supported: 207 208 * `bucket` - (Required) The name of the bucket to put notification configuration. 209 * `topic` - (Optional) The notification configuration to SNS Topic (documented below). 210 * `queue` - (Optional) The notification configuration to SQS Queue (documented below). 211 * `lambda_function` - (Optional) The notification configuration to Lambda Function (documented below). 212 213 The `topic` notification configuration supports the following: 214 215 * `id` - (Optional) Specifies unique identifier for each of the notification configurations. 216 * `topic_arn` - (Required) Specifies Amazon SNS topic ARN. 217 * `events` - (Required) Specifies [event](http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html#notification-how-to-event-types-and-destinations) for which to send notifications. 218 * `filter_prefix` - (Optional) Specifies object key name prefix. 219 * `filter_suffix` - (Optional) Specifies object key name suffix. 220 221 The `queue` notification configuration supports the following: 222 223 * `id` - (Optional) Specifies unique identifier for each of the notification configurations. 224 * `queue_arn` - (Required) Specifies Amazon SQS queue ARN. 225 * `events` - (Required) Specifies [event](http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html#notification-how-to-event-types-and-destinations) for which to send notifications. 226 * `filter_prefix` - (Optional) Specifies object key name prefix. 227 * `filter_suffix` - (Optional) Specifies object key name suffix. 228 229 The `lambda_function` notification configuration supports the following: 230 231 * `id` - (Optional) Specifies unique identifier for each of the notification configurations. 232 * `lambda_function_arn` - (Required) Specifies Amazon Lambda function ARN. 233 * `events` - (Required) Specifies [event](http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html#notification-how-to-event-types-and-destinations) for which to send notifications. 234 * `filter_prefix` - (Optional) Specifies object key name prefix. 235 * `filter_suffix` - (Optional) Specifies object key name suffix. 236