-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
main.tf
226 lines (178 loc) · 5.28 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
provider "aws" {
region = local.region
}
data "aws_availability_zones" "available" {}
locals {
name = "ex-${basename(path.cwd)}"
region = "eu-west-1"
vpc_cidr = "10.0.0.0/16"
azs = slice(data.aws_availability_zones.available.names, 0, 3)
tags = {
Example = local.name
GithubRepo = "terraform-aws-apigateway-v2"
GithubOrg = "terraform-aws-modules"
}
}
################################################################################
# API Gateway Module
################################################################################
module "api_gateway" {
source = "../../"
# API
cors_configuration = {
allow_headers = ["content-type", "x-amz-date", "authorization", "x-api-key", "x-amz-security-token", "x-amz-user-agent"]
allow_methods = ["*"]
allow_origins = ["*"]
}
description = "HTTP API Gateway with VPC links"
name = local.name
# Custom Domain
create_domain_name = false
# Routes & Integration(s)
routes = {
"ANY /" = {
integration = {
uri = module.lambda_function.lambda_function_arn
payload_format_version = "2.0"
timeout_milliseconds = 12000
}
}
"GET /alb-internal-route" = {
integration = {
connection_type = "VPC_LINK"
uri = module.alb.listeners["default"].arn
type = "HTTP_PROXY"
method = "ANY"
vpc_link_key = "my-vpc"
}
}
"$default" = {
integration = {
uri = module.lambda_function.lambda_function_arn
}
}
}
# VPC Link
vpc_links = {
my-vpc = {
name = local.name
security_group_ids = [module.api_gateway_security_group.security_group_id]
subnet_ids = module.vpc.public_subnets
}
}
tags = local.tags
}
################################################################################
# Supporting Resources
################################################################################
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = local.name
cidr = local.vpc_cidr
azs = local.azs
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)]
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 48)]
enable_nat_gateway = true
single_nat_gateway = true
tags = local.tags
}
module "api_gateway_security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.0"
name = local.name
description = "API Gateway group for example usage"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_rules = ["http-80-tcp"]
egress_rules = ["all-all"]
tags = local.tags
}
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 9.0"
name = local.name
vpc_id = module.vpc.vpc_id
subnets = module.vpc.public_subnets
# Disable for example
enable_deletion_protection = false
security_group_ingress_rules = {
all_http = {
from_port = 80
to_port = 80
ip_protocol = "tcp"
description = "HTTP web traffic"
cidr_ipv4 = "0.0.0.0/0"
}
all_https = {
from_port = 443
to_port = 443
ip_protocol = "tcp"
description = "HTTPS web traffic"
cidr_ipv4 = "0.0.0.0/0"
}
}
listeners = {
default = {
port = 80
protocol = "HTTP"
fixed_response = {
content_type = "text/plain"
message_body = "Hello, World!"
status_code = "200"
}
}
}
tags = local.tags
}
locals {
package_url = "https://raw.githubusercontent.com/terraform-aws-modules/terraform-aws-lambda/master/examples/fixtures/python-function.zip"
downloaded = "downloaded_package_${md5(local.package_url)}.zip"
}
resource "null_resource" "download_package" {
triggers = {
downloaded = local.downloaded
}
provisioner "local-exec" {
command = "curl -L -o ${local.downloaded} ${local.package_url}"
}
}
module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
version = "~> 7.0"
function_name = local.name
description = "My awesome lambda function"
handler = "index.lambda_handler"
runtime = "python3.12"
architectures = ["arm64"]
publish = true
create_package = false
local_existing_package = local.downloaded
cloudwatch_logs_retention_in_days = 7
attach_network_policy = true
vpc_subnet_ids = module.vpc.private_subnets
vpc_security_group_ids = [module.lambda_security_group.security_group_id]
allowed_triggers = {
AllowExecutionFromAPIGateway = {
service = "apigateway"
source_arn = "${module.api_gateway.api_execution_arn}/*/*"
}
}
tags = local.tags
}
module "lambda_security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.0"
name = "${local.name}-lambda"
description = "Lambda security group for example usage"
vpc_id = module.vpc.vpc_id
computed_ingress_with_source_security_group_id = [
{
rule = "http-80-tcp"
source_security_group_id = module.api_gateway_security_group.security_group_id
}
]
number_of_computed_ingress_with_source_security_group_id = 1
egress_rules = ["all-all"]
tags = local.tags
}