github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 ```hcl 18 resource "aws_sns_topic" "topic" { 19 name = "s3-event-notification-topic" 20 21 policy = <<POLICY 22 { 23 "Version":"2012-10-17", 24 "Statement":[{ 25 "Effect": "Allow", 26 "Principal": {"AWS":"*"}, 27 "Action": "SNS:Publish", 28 "Resource": "arn:aws:sns:*:*:s3-event-notification-topic", 29 "Condition":{ 30 "ArnLike":{"aws:SourceArn":"${aws_s3_bucket.bucket.arn}"} 31 } 32 }] 33 } 34 POLICY 35 } 36 37 resource "aws_s3_bucket" "bucket" { 38 bucket = "your_bucket_name" 39 } 40 41 resource "aws_s3_bucket_notification" "bucket_notification" { 42 bucket = "${aws_s3_bucket.bucket.id}" 43 44 topic { 45 topic_arn = "${aws_sns_topic.topic.arn}" 46 events = ["s3:ObjectCreated:*"] 47 filter_suffix = ".log" 48 } 49 } 50 ``` 51 52 ### Add notification configuration to SQS Queue 53 54 ```hcl 55 resource "aws_sqs_queue" "queue" { 56 name = "s3-event-notification-queue" 57 58 policy = <<POLICY 59 { 60 "Version": "2012-10-17", 61 "Statement": [ 62 { 63 "Effect": "Allow", 64 "Principal": "*", 65 "Action": "sqs:SendMessage", 66 "Resource": "arn:aws:sqs:*:*:s3-event-notification-queue", 67 "Condition": { 68 "ArnEquals": { "aws:SourceArn": "${aws_s3_bucket.bucket.arn}" } 69 } 70 } 71 ] 72 } 73 POLICY 74 } 75 76 resource "aws_s3_bucket" "bucket" { 77 bucket = "your_bucket_name" 78 } 79 80 resource "aws_s3_bucket_notification" "bucket_notification" { 81 bucket = "${aws_s3_bucket.bucket.id}" 82 83 queue { 84 queue_arn = "${aws_sqs_queue.queue.arn}" 85 events = ["s3:ObjectCreated:*"] 86 filter_suffix = ".log" 87 } 88 } 89 ``` 90 91 ### Add notification configuration to Lambda Function 92 93 ```hcl 94 resource "aws_iam_role" "iam_for_lambda" { 95 name = "iam_for_lambda" 96 97 assume_role_policy = <<EOF 98 { 99 "Version": "2012-10-17", 100 "Statement": [ 101 { 102 "Action": "sts:AssumeRole", 103 "Principal": { 104 "Service": "lambda.amazonaws.com" 105 }, 106 "Effect": "Allow" 107 } 108 ] 109 } 110 EOF 111 } 112 113 resource "aws_lambda_permission" "allow_bucket" { 114 statement_id = "AllowExecutionFromS3Bucket" 115 action = "lambda:InvokeFunction" 116 function_name = "${aws_lambda_function.func.arn}" 117 principal = "s3.amazonaws.com" 118 source_arn = "${aws_s3_bucket.bucket.arn}" 119 } 120 121 resource "aws_lambda_function" "func" { 122 filename = "your-function.zip" 123 function_name = "example_lambda_name" 124 role = "${aws_iam_role.iam_for_lambda.arn}" 125 handler = "exports.example" 126 } 127 128 resource "aws_s3_bucket" "bucket" { 129 bucket = "your_bucket_name" 130 } 131 132 resource "aws_s3_bucket_notification" "bucket_notification" { 133 bucket = "${aws_s3_bucket.bucket.id}" 134 135 lambda_function { 136 lambda_function_arn = "${aws_lambda_function.func.arn}" 137 events = ["s3:ObjectCreated:*"] 138 filter_prefix = "AWSLogs/" 139 filter_suffix = ".log" 140 } 141 } 142 ``` 143 144 ### Trigger multiple Lambda functions 145 146 ```hcl 147 resource "aws_iam_role" "iam_for_lambda" { 148 name = "iam_for_lambda" 149 150 assume_role_policy = <<EOF 151 { 152 "Version": "2012-10-17", 153 "Statement": [ 154 { 155 "Action": "sts:AssumeRole", 156 "Principal": { 157 "Service": "lambda.amazonaws.com" 158 }, 159 "Effect": "Allow" 160 } 161 ] 162 } 163 EOF 164 } 165 166 resource "aws_lambda_permission" "allow_bucket1" { 167 statement_id = "AllowExecutionFromS3Bucket1" 168 action = "lambda:InvokeFunction" 169 function_name = "${aws_lambda_function.func1.arn}" 170 principal = "s3.amazonaws.com" 171 source_arn = "${aws_s3_bucket.bucket.arn}" 172 } 173 174 resource "aws_lambda_function" "func1" { 175 filename = "your-function1.zip" 176 function_name = "example_lambda_name1" 177 role = "${aws_iam_role.iam_for_lambda.arn}" 178 handler = "exports.example" 179 } 180 181 resource "aws_lambda_permission" "allow_bucket2" { 182 statement_id = "AllowExecutionFromS3Bucket2" 183 action = "lambda:InvokeFunction" 184 function_name = "${aws_lambda_function.func2.arn}" 185 principal = "s3.amazonaws.com" 186 source_arn = "${aws_s3_bucket.bucket.arn}" 187 } 188 189 resource "aws_lambda_function" "func2" { 190 filename = "your-function2.zip" 191 function_name = "example_lambda_name2" 192 role = "${aws_iam_role.iam_for_lambda.arn}" 193 handler = "exports.example" 194 } 195 196 resource "aws_s3_bucket" "bucket" { 197 bucket = "your_bucket_name" 198 } 199 200 resource "aws_s3_bucket_notification" "bucket_notification" { 201 bucket = "${aws_s3_bucket.bucket.id}" 202 203 lambda_function { 204 lambda_function_arn = "${aws_lambda_function.func1.arn}" 205 events = ["s3:ObjectCreated:*"] 206 filter_prefix = "AWSLogs/" 207 filter_suffix = ".log" 208 } 209 210 lambda_function { 211 lambda_function_arn = "${aws_lambda_function.func2.arn}" 212 events = ["s3:ObjectCreated:*"] 213 filter_prefix = "OtherLogs/" 214 filter_suffix = ".log" 215 } 216 } 217 ``` 218 219 ### Add multiple notification configurations to SQS Queue 220 221 ```hcl 222 resource "aws_sqs_queue" "queue" { 223 name = "s3-event-notification-queue" 224 225 policy = <<POLICY 226 { 227 "Version": "2012-10-17", 228 "Statement": [ 229 { 230 "Effect": "Allow", 231 "Principal": "*", 232 "Action": "sqs:SendMessage", 233 "Resource": "arn:aws:sqs:*:*:s3-event-notification-queue", 234 "Condition": { 235 "ArnEquals": { "aws:SourceArn": "${aws_s3_bucket.bucket.arn}" } 236 } 237 } 238 ] 239 } 240 POLICY 241 } 242 243 resource "aws_s3_bucket" "bucket" { 244 bucket = "your_bucket_name" 245 } 246 247 resource "aws_s3_bucket_notification" "bucket_notification" { 248 bucket = "${aws_s3_bucket.bucket.id}" 249 250 queue { 251 id = "image-upload-event" 252 queue_arn = "${aws_sqs_queue.queue.arn}" 253 events = ["s3:ObjectCreated:*"] 254 filter_prefix = "images/" 255 } 256 257 queue { 258 id = "video-upload-event" 259 queue_arn = "${aws_sqs_queue.queue.arn}" 260 events = ["s3:ObjectCreated:*"] 261 filter_prefix = "videos/" 262 } 263 } 264 ``` 265 266 For Terraform's [JSON syntax](https://www.terraform.io/docs/configuration/syntax.html), use an array instead of defining the `queue` key twice. 267 268 ```json 269 { 270 "bucket": "${aws_s3_bucket.bucket.id}", 271 "queue": [ 272 { 273 "id": "image-upload-event", 274 "queue_arn": "${aws_sqs_queue.queue.arn}", 275 "events": ["s3:ObjectCreated:*"], 276 "filter_prefix": "images/" 277 }, 278 { 279 "id": "video-upload-event", 280 "queue_arn": "${aws_sqs_queue.queue.arn}", 281 "events": ["s3:ObjectCreated:*"], 282 "filter_prefix": "videos/" 283 } 284 ] 285 } 286 ``` 287 288 ## Argument Reference 289 290 The following arguments are supported: 291 292 * `bucket` - (Required) The name of the bucket to put notification configuration. 293 * `topic` - (Optional) The notification configuration to SNS Topic (documented below). 294 * `queue` - (Optional) The notification configuration to SQS Queue (documented below). 295 * `lambda_function` - (Optional, Multiple) Used to configure notifications to a Lambda Function (documented below). 296 297 The `topic` notification configuration supports the following: 298 299 * `id` - (Optional) Specifies unique identifier for each of the notification configurations. 300 * `topic_arn` - (Required) Specifies Amazon SNS topic ARN. 301 * `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. 302 * `filter_prefix` - (Optional) Specifies object key name prefix. 303 * `filter_suffix` - (Optional) Specifies object key name suffix. 304 305 The `queue` notification configuration supports the following: 306 307 * `id` - (Optional) Specifies unique identifier for each of the notification configurations. 308 * `queue_arn` - (Required) Specifies Amazon SQS queue ARN. 309 * `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. 310 * `filter_prefix` - (Optional) Specifies object key name prefix. 311 * `filter_suffix` - (Optional) Specifies object key name suffix. 312 313 The `lambda_function` notification configuration supports the following: 314 315 * `id` - (Optional) Specifies unique identifier for each of the notification configurations. 316 * `lambda_function_arn` - (Required) Specifies Amazon Lambda function ARN. 317 * `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. 318 * `filter_prefix` - (Optional) Specifies object key name prefix. 319 * `filter_suffix` - (Optional) Specifies object key name suffix. 320 321 ## Import 322 323 S3 bucket notification can be imported using the `bucket`, e.g. 324 325 ``` 326 $ terraform import aws_s3_bucket_notification.bucket_notification bucket-name 327 ```