-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
156 lines (128 loc) · 3.77 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
terraform {
required_providers {
random = {
source = "hashicorp/random"
version = "~> 3.1.0"
}
archive = {
source = "hashicorp/archive"
version = "~> 2.2.0"
}
}
required_version = "~> 1.0"
}
provider "aws" {
region = var.aws_region
}
//To Zip the folder
data "archive_file" "lambda_hello_world" {
type = "zip"
source_dir = "${path.module}/hello-world"
output_path = "${path.module}/hello-world.zip"
}
locals {
my_function_source = "./hello-world.zip"
}
//For S3
resource "aws_s3_bucket" "builds" {
bucket = var.bucketname
acl = "private"
}
resource "aws_s3_bucket_object" "my_function" {
bucket = aws_s3_bucket.builds.id
key = "${filemd5(local.my_function_source)}.zip"
source = data.archive_file.lambda_hello_world.output_path
}
//For lambda 1
module "lambda_function_existing_package_s3" {
source = "terraform-aws-modules/lambda/aws"
function_name = var.functionname1
description = "My Hello world lambda function"
handler = "hello.handler"
runtime = "nodejs12.x"
publish = true
create_package = false
s3_existing_package = {
bucket = aws_s3_bucket.builds.id
key = aws_s3_bucket_object.my_function.id
}
allowed_triggers = {
AllowExecutionFromAPIGateway = {
service = "apigateway"
source_arn = "${module.api_gateway.apigatewayv2_api_execution_arn}/*/*"
}
}
}
//For lambda 2
module "lambda_function_existing_package_s3_2" {
source = "terraform-aws-modules/lambda/aws"
function_name = var.functionname2
description = "My Learn world lambda function"
handler = "hello.handler2"
runtime = "nodejs12.x"
publish = true
create_package = false
s3_existing_package = {
bucket = aws_s3_bucket.builds.id
key = aws_s3_bucket_object.my_function.id
}
allowed_triggers = {
AllowExecutionFromAPIGateway = {
service = "apigateway"
source_arn = "${module.api_gateway.apigatewayv2_api_execution_arn}/*/*"
}
}
}
// For API Gateway
module "api_gateway" {
source = "terraform-aws-modules/apigateway-v2/aws"
name = var.gatewayname
description = "My HTTP API Gateway"
protocol_type = "HTTP"
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 = ["*"]
}
create_api_domain_name = false
# Access logs
default_stage_access_log_destination_arn = aws_cloudwatch_log_group.api_gw.arn
default_stage_access_log_format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
//OPENAPI v3 definition documents
body = jsonencode({
openapi = "3.0.1"
info = {
title = "ulesson"
version = "1.0"
}
paths = {
"/hello" = {
get = {
x-amazon-apigateway-integration = {
httpMethod = "GET"
payloadFormatVersion = "1.0"
type = "AWS_PROXY"
uri = module.lambda_function_existing_package_s3.lambda_function_arn
}
}
}
"/learn" = {
get = {
x-amazon-apigateway-integration = {
httpMethod = "GET"
payloadFormatVersion = "1.0"
type = "AWS_PROXY"
uri = module.lambda_function_existing_package_s3_2.lambda_function_arn
}
}
}
}
})
tags = {
Name = "http-apigateway"
}
}
resource "aws_cloudwatch_log_group" "api_gw" {
name = "/aws/lambda/${module.api_gateway.apigatewayv2_api_id}"
retention_in_days = 30
}