-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.tf
158 lines (123 loc) · 4.78 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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# S3 COMPLETE EXAMPLE
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ----------------------------------------------------------------------------------------------------------------------
# PROVIDER CONFIGURATION
# ----------------------------------------------------------------------------------------------------------------------
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
archive = "~> 1.3"
}
}
# ----------------------------------------------------------------------------------------------------------------------
# CREATE A ZIP ARCHIVE FROM SOURCES
# ----------------------------------------------------------------------------------------------------------------------
data "archive_file" "lambda" {
type = "zip"
source_file = "${path.module}/python/main.py"
output_path = "${path.module}/python/main.py.zip"
}
# ----------------------------------------------------------------------------------------------------------------------
# FETCH DEFAULT VPC DATA AND PASS TO THE LAMBDA FUNCTIONS VPC CONFIG
# ----------------------------------------------------------------------------------------------------------------------
resource "aws_default_vpc" "default" {
}
data "aws_subnet_ids" "default" {
vpc_id = aws_default_vpc.default.id
}
resource "aws_default_security_group" "default" {
vpc_id = aws_default_vpc.default.id
}
# ----------------------------------------------------------------------------------------------------------------------
# CREATE A S3 BUCKET AND UPLOAD THE ARCHIVE AS AN OBJECT
# ----------------------------------------------------------------------------------------------------------------------
module "s3_bucket" {
source = "mineiros-io/s3-bucket/aws"
version = "~> 0.5.0"
bucket_prefix = "mineiros-s3-example-"
force_destroy = true
}
resource "aws_s3_bucket_object" "function" {
bucket = module.s3_bucket.id
key = "main.py.zip"
source = data.archive_file.lambda.output_path
}
# ----------------------------------------------------------------------------------------------------------------------
# CREATE THE LAMBDA FUNCTION
# ----------------------------------------------------------------------------------------------------------------------
module "lambda" {
source = "mineiros-io/lambda-function/aws"
version = "~> 0.5.0"
function_name = "mineiros-s3-lambda-example"
description = "This is a simple Lambda Function that has its deployment package located in a S3 bucket."
runtime = "python3.8"
handler = "main.lambda_handler"
role_arn = module.iam_role.role.arn
publish = true
s3_bucket = aws_s3_bucket_object.function.bucket
s3_key = aws_s3_bucket_object.function.key
timeout = 3
memory_size = 128
vpc_subnet_ids = data.aws_subnet_ids.default.ids
vpc_security_group_ids = [aws_default_security_group.default.id]
aliases = {
latest = {
description = "The latest deployed version."
}
}
permissions = [
{
statement_id = "AllowExecutionFromSNS"
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.lambda.arn
}
]
module_tags = {
Environment = "Test"
}
}
# ----------------------------------------------------------------------------------------------------------------------
# CREATE A SNS TOPIC AND SUBSCRIBE THE LAMBDA FUNCTION
# ----------------------------------------------------------------------------------------------------------------------
resource "aws_sns_topic" "lambda" {
name = "call-lambda-maybe"
}
resource "aws_sns_topic_subscription" "lambda" {
topic_arn = aws_sns_topic.lambda.arn
protocol = "lambda"
endpoint = module.lambda.function.arn
}
# ----------------------------------------------------------------------------------------------------------------------
# CREATE AN IAM ROLE THAT WILL BE ATTACHED TO THE LAMBDA FUNCTION
# The Lambda Function needs access to the VPC if you'd to grant it permission
# to a VPC.
# ----------------------------------------------------------------------------------------------------------------------
module "iam_role" {
source = "mineiros-io/iam-role/aws"
version = "~> 0.6.0"
name = "mineiros-s3-lambda-example"
assume_role_principals = [
{
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
]
policy_statements = [
{
sid = "LambdaVPCAccess"
actions = [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DetachNetworkInterface",
"ec2:DeleteNetworkInterface",
]
resources = ["*"]
}
]
tags = {
Environment = "Test"
}
}