Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.

Commit 2c1ed28

Browse files
authored
added submodule for S3 events (#43)
1 parent e20be39 commit 2c1ed28

File tree

10 files changed

+115
-1
lines changed

10 files changed

+115
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The following [event sources](https://docs.aws.amazon.com/lambda/latest/dg/invok
88

99
- `cloudwatch-scheduled-event`: configures a [CloudWatch Event Rule](https://www.terraform.io/docs/providers/aws/r/cloudwatch_event_rule.html) to trigger the Lambda on a regular, scheduled basis
1010
- `dynamodb`: configures an [Event Source Mapping](https://www.terraform.io/docs/providers/aws/r/lambda_event_source_mapping.html) to trigger the Lambda by DynamoDb events
11+
- `s3`: configures permission to trigger the Lambda by S3
1112
- `sns`: to trigger Lambda by [SNS Topic Subscription](https://www.terraform.io/docs/providers/aws/r/sns_topic_subscription.html)
1213

1314
Furthermore this module supports:
@@ -73,6 +74,7 @@ module "lambda" {
7374

7475
- [example-with-cloudwatch-scheduled-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-cloudwatch-scheduled-event)
7576
- [example-with-dynamodb-event-source](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-dynamodb-event)
77+
- [example-with-s3-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-s3-event)
7678
- [example-with-sns-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-sns-event)
7779
- [example-with-vpc](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-with-vpc)
7880
- [example-without-event](https://github.com/spring-media/terraform-aws-lambda/tree/master/examples/example-without-event)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Example with S3 event
2+
3+
Creates an AWS Lambda function triggered by a S3 [event](https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html).
4+
5+
## requirements
6+
7+
- [Terraform 0.12+](https://www.terraform.io/)
8+
- authentication configuration for the [aws provider](https://www.terraform.io/docs/providers/aws/)
9+
10+
## usage
11+
12+
```
13+
terraform init
14+
terraform plan
15+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
resource "aws_s3_bucket_notification" "bucket_notification" {
6+
bucket = "bucketname"
7+
8+
lambda_function {
9+
lambda_function_arn = module.lambda.arn
10+
events = ["s3:ObjectCreated:*"]
11+
}
12+
}
13+
14+
module "lambda" {
15+
source = "../../"
16+
description = "Example AWS Lambda using go with S3 trigger"
17+
filename = "${path.module}/test_function.zip"
18+
function_name = "tf-example-go-s3"
19+
handler = "example-lambda-func"
20+
runtime = "go1.x"
21+
22+
event = {
23+
type = "s3"
24+
s3_bucket_arn = "arn:aws:s3:::bucketname"
25+
s3_bucket_id = "bucketname"
26+
}
27+
28+
tags = {
29+
key = "value"
30+
}
31+
32+
environment = {
33+
variables = {
34+
key = "value"
35+
}
36+
}
37+
}
38+

examples/example-with-s3-event/test_function.zip

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ module "event-sns" {
4040
topic_arn = lookup(var.event, "topic_arn", "")
4141
}
4242

43+
module "event-s3" {
44+
source = "./modules/event/s3"
45+
enable = lookup(var.event, "type", "") == "s3" ? true : false
46+
47+
lambda_function_arn = module.lambda.arn
48+
s3_bucket_arn = lookup(var.event, "s3_bucket_arn", "")
49+
s3_bucket_id = lookup(var.event, "s3_bucket_id", "")
50+
}
51+
4352
resource "aws_cloudwatch_log_group" "lambda" {
4453
name = "/aws/lambda/${module.lambda.function_name}"
4554
retention_in_days = var.log_retention_in_days

modules/event/s3/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "aws_lambda_permission" "allow_bucket" {
2+
count = var.enable ? 1 : 0
3+
action = "lambda:InvokeFunction"
4+
function_name = var.lambda_function_arn
5+
principal = "s3.amazonaws.com"
6+
statement_id = "AllowExecutionFromS3Bucket"
7+
source_arn = var.s3_bucket_arn
8+
}

modules/event/s3/variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# REQUIRED PARAMETERS
3+
# You must provide a value for each of these parameters.
4+
# ---------------------------------------------------------------------------------------------------------------------
5+
6+
variable "lambda_function_arn" {
7+
description = "The Amazon Resource Name (ARN) identifying the Lambda Function triggered by S3"
8+
}
9+
10+
variable "s3_bucket_arn" {
11+
description = "The ARN of the bucket."
12+
}
13+
14+
variable "s3_bucket_id" {
15+
description = "The name of the bucket."
16+
}
17+
18+
# ---------------------------------------------------------------------------------------------------------------------
19+
# OPTIONAL PARAMETERS
20+
# These parameters have reasonable defaults.
21+
# ---------------------------------------------------------------------------------------------------------------------
22+
23+
variable "enable" {
24+
description = "Conditionally enables this module (and all it's ressources)."
25+
type = bool
26+
default = false
27+
}
28+
29+
variable "lambda_function_notification" {
30+
description = "(multiple) Used to configure notifications to a Lambda Function. See https://www.terraform.io/docs/providers/aws/r/s3_bucket_notification.html#lambda_function for allowed values."
31+
type = list(map(string))
32+
default = []
33+
}
34+

modules/event/s3/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ variable "environment" {
3232
}
3333

3434
variable "event" {
35-
description = "Event source configuration which triggers the Lambda function. Supported events: Scheduled Events, DynamoDb."
35+
description = "Event source configuration which triggers the Lambda function. Supported events: cloudwatch-scheduled-event, dynamodb, s3, sns"
3636
type = map(string)
3737
default = {}
3838
}

0 commit comments

Comments
 (0)