github.com/ves/terraform@v0.8.0-beta2/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  ### Trigger multiple Lambda functions
   139  
   140  ```
   141  resource "aws_iam_role" "iam_for_lambda" {
   142      name = "iam_for_lambda"
   143      assume_role_policy = <<EOF
   144  {
   145    "Version": "2012-10-17",
   146    "Statement": [
   147      {
   148        "Action": "sts:AssumeRole",
   149        "Principal": {
   150          "Service": "lambda.amazonaws.com"
   151        },
   152        "Effect": "Allow"
   153      }
   154    ]
   155  }
   156  EOF
   157  }
   158  
   159  resource "aws_lambda_permission" "allow_bucket1" {
   160      statement_id = "AllowExecutionFromS3Bucket1"
   161      action = "lambda:InvokeFunction"
   162      function_name = "${aws_lambda_function.func1.arn}"
   163      principal = "s3.amazonaws.com"
   164      source_arn = "${aws_s3_bucket.bucket.arn}"
   165  }
   166  
   167  resource "aws_lambda_function" "func1" {
   168      filename = "your-function1.zip"
   169      function_name = "example_lambda_name1"
   170      role = "${aws_iam_role.iam_for_lambda.arn}"
   171      handler = "exports.example"
   172  }
   173  
   174  resource "aws_lambda_permission" "allow_bucket2" {
   175      statement_id = "AllowExecutionFromS3Bucket2"
   176      action = "lambda:InvokeFunction"
   177      function_name = "${aws_lambda_function.func2.arn}"
   178      principal = "s3.amazonaws.com"
   179      source_arn = "${aws_s3_bucket.bucket.arn}"
   180  }
   181  
   182  resource "aws_lambda_function" "func2" {
   183      filename = "your-function2.zip"
   184      function_name = "example_lambda_name2"
   185      role = "${aws_iam_role.iam_for_lambda.arn}"
   186      handler = "exports.example"
   187  }
   188  
   189  resource "aws_s3_bucket" "bucket" {
   190  	bucket = "your_bucket_name"
   191  }
   192  
   193  resource "aws_s3_bucket_notification" "bucket_notification" {
   194  	bucket = "${aws_s3_bucket.bucket.id}"
   195  	lambda_function {
   196  		lambda_function_arn = "${aws_lambda_function.func1.arn}"
   197  		events = ["s3:ObjectCreated:*"]
   198  		filter_prefix = "AWSLogs/"
   199  		filter_suffix = ".log"
   200  	}
   201  	lambda_function {
   202  		lambda_function_arn = "${aws_lambda_function.func2.arn}"
   203  		events = ["s3:ObjectCreated:*"]
   204  		filter_prefix = "OtherLogs/"
   205  		filter_suffix = ".log"
   206  	}
   207  }
   208  ```
   209  
   210  ### Add multiple notification configurations to SQS Queue
   211  
   212  ```
   213  resource "aws_sqs_queue" "queue" {
   214      name = "s3-event-notification-queue"
   215      policy = <<POLICY
   216  {
   217    "Version": "2012-10-17",
   218    "Statement": [
   219      {
   220        "Effect": "Allow",
   221        "Principal": "*",
   222        "Action": "sqs:SendMessage",
   223  	  "Resource": "arn:aws:sqs:*:*:s3-event-notification-queue",
   224        "Condition": {
   225          "ArnEquals": { "aws:SourceArn": "${aws_s3_bucket.bucket.arn}" }
   226        }
   227      }
   228    ]
   229  }
   230  POLICY
   231  }
   232  
   233  resource "aws_s3_bucket" "bucket" {
   234  	bucket = "your_bucket_name"
   235  }
   236  
   237  resource "aws_s3_bucket_notification" "bucket_notification" {
   238  	bucket = "${aws_s3_bucket.bucket.id}"
   239  	queue {
   240  		id = "image-upload-event"
   241  		queue_arn = "${aws_sqs_queue.queue.arn}"
   242  		events = ["s3:ObjectCreated:*"]
   243  		filter_prefix = "images/"
   244  	}
   245  	queue {
   246  		id = "video-upload-event"
   247  		queue_arn = "${aws_sqs_queue.queue.arn}"
   248  		events = ["s3:ObjectCreated:*"]
   249  		filter_prefix = "videos/"
   250  	}
   251  }
   252  ```
   253  
   254  For Terraform's [JSON syntax](https://www.terraform.io/docs/configuration/syntax.html), use an array instead of defining the `queue` key twice.
   255  
   256  ```
   257  {
   258  	"bucket": "${aws_s3_bucket.bucket.id}",
   259  	"queue": [
   260  		{
   261  			"id": "image-upload-event",
   262  			"queue_arn": "${aws_sqs_queue.queue.arn}",
   263  			"events": ["s3:ObjectCreated:*"],
   264  			"filter_prefix": "images/"
   265  		},
   266  		{
   267  			"id": "video-upload-event",
   268  			"queue_arn": "${aws_sqs_queue.queue.arn}",
   269  			"events": ["s3:ObjectCreated:*"],
   270  			"filter_prefix": "videos/"
   271  		}
   272  	]
   273  }
   274  ```
   275  
   276  ## Argument Reference
   277  
   278  The following arguments are supported:
   279  
   280  * `bucket` - (Required) The name of the bucket to put notification configuration.
   281  * `topic` - (Optional) The notification configuration to SNS Topic (documented below).
   282  * `queue` - (Optional) The notification configuration to SQS Queue (documented below).
   283  * `lambda_function` - (Optional, Multiple) Used to configure notifications to a Lambda Function (documented below).
   284  
   285  The `topic` notification configuration supports the following:
   286  
   287  * `id` - (Optional) Specifies unique identifier for each of the notification configurations.
   288  * `topic_arn` - (Required) Specifies Amazon SNS topic ARN.
   289  * `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.
   290  * `filter_prefix` - (Optional) Specifies object key name prefix.
   291  * `filter_suffix` - (Optional) Specifies object key name suffix.
   292  
   293  The `queue` notification configuration supports the following:
   294  
   295  * `id` - (Optional) Specifies unique identifier for each of the notification configurations.
   296  * `queue_arn` - (Required) Specifies Amazon SQS queue ARN.
   297  * `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.
   298  * `filter_prefix` - (Optional) Specifies object key name prefix.
   299  * `filter_suffix` - (Optional) Specifies object key name suffix.
   300  
   301  The `lambda_function` notification configuration supports the following:
   302  
   303  * `id` - (Optional) Specifies unique identifier for each of the notification configurations.
   304  * `lambda_function_arn` - (Required) Specifies Amazon Lambda function ARN.
   305  * `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.
   306  * `filter_prefix` - (Optional) Specifies object key name prefix.
   307  * `filter_suffix` - (Optional) Specifies object key name suffix.
   308  
   309  ## Import
   310  
   311  S3 bucket notification can be imported using the `bucket`, e.g.
   312  
   313  ```
   314  $ terraform import aws_s3_bucket_notification.bucket_notification bucket-name
   315  ```