-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
179 lines (161 loc) · 4.11 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
// Environment variables
variable "aws_region" {
default = "us-west-2"
description = "AWS deployment region"
type = string
}
variable "env_name" {
default = "Event-Driven-Go"
description = "Terraform environment name"
type = string
}
// Build the lambda file
resource "null_resource" "build_lambda_exec" {
// run this when the main.go changes
triggers = {
source_code_hash = "${filebase64sha256("${path.module}/lambda/main.go")}"
}
provisioner "local-exec" {
command = "${path.module}/build.sh"
working_dir = path.module
}
}
// Lambda Zip file
data "archive_file" "s3_copy_lambda_function" {
source_dir = "${path.module}/lambda/"
output_path = "${path.module}/lambda.zip"
type = "zip"
}
provider "aws" {
profile = "default"
region = var.aws_region
}
// policy for the lambda
resource "aws_iam_policy" "lambda_policy" {
name = "iam_for_${var.env_name}_lambda"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:CopyObject",
"s3:HeadObject"
],
"Effect": "Allow",
"Resource" : [
"arn:aws:s3:::${aws_s3_bucket.my_producer.id}",
"arn:aws:s3:::${aws_s3_bucket.my_producer.id}/*"
]
},
{
"Action": [
"s3:ListBucket",
"s3:PutObject",
"s3:PutObjectAcl",
"s3:CopyObject",
"s3:HeadObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::${aws_s3_bucket.my_consumer.id}",
"arn:aws:s3:::${aws_s3_bucket.my_consumer.id}/*"
]
},
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
// AWS IAM Role for the lambda
resource "aws_iam_role" "s3_copy_function" {
name = "app-${var.env_name}-lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
// Attach the policy to the role
resource "aws_iam_role_policy_attachment" "terraform_lambda_iam_policy_basic_execution" {
role = aws_iam_role.s3_copy_function.id
policy_arn = aws_iam_policy.lambda_policy.arn
}
// Resources
resource "random_string" "unique_name" {
length = 8
special = false
upper = false
lower = true
number = false
}
// Bucket Producer of events to lambda
resource "aws_s3_bucket" "my_producer" {
bucket = "${random_string.unique_name.id}-my-producer"
}
// Bucket Consumer of data from lambda
resource "aws_s3_bucket" "my_consumer" {
bucket = "${random_string.unique_name.id}-my-consumer"
acl = "private"
}
// allow bucket to notify lambda of events
resource "aws_lambda_permission" "allow_bucket" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.s3_copy_lambda_function.arn
principal = "s3.amazonaws.com"
source_arn = aws_s3_bucket.my_producer.arn // which bucket is going to call our lambda
}
// Lambda function
resource "aws_lambda_function" "s3_copy_lambda_function" {
filename = "lambda.zip"
function_name = "example_lambda_name"
role = aws_iam_role.s3_copy_function.arn
handler = "s3_lambda"
runtime = "go1.x"
// Set environment variables for the lambda code
environment {
variables = {
DST_BUCKET = aws_s3_bucket.my_consumer.id
REGION = "${var.aws_region}"
}
}
}
resource "aws_s3_bucket_notification" "bucket_notification" {
// bucket that sends the notification
bucket = aws_s3_bucket.my_producer.id
// The lambda function that will be notified
lambda_function {
lambda_function_arn = aws_lambda_function.s3_copy_lambda_function.arn
events = ["s3:ObjectCreated:*"]
}
depends_on = [aws_lambda_permission.allow_bucket]
}