-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
134 lines (113 loc) · 4.67 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
// Define lambda for my-slack_event_handler
resource "aws_lambda_function" "lambda_function" {
function_name = var.lambda_app_name
description = var.lambda_app_description
runtime = var.lambda_runtime
handler = var.lambda_handler
filename = data.archive_file.zip.output_path
source_code_hash = data.archive_file.zip.output_base64sha256
architectures = var.lambda_architecture
memory_size = var.lambda_memory_size
role = aws_iam_role.lambda_function_execution_role.arn
tags = var.tags
environment {
variables = var.lambda_env_variables
}
}
//Provides a CloudWatch Log Group resource for the Lambda function
resource "aws_cloudwatch_log_group" "cloudwatch_log_group_lambda_function" {
count = (var.lambda_cloudwatch ? 1 : 0)
name = "/aws/lambda/${aws_lambda_function.lambda_function.function_name}"
retention_in_days = var.cloudwatch_log_retention_days
tags = var.tags
}
//Provides a CloudWatch Log Group resource for the API Gateway
resource "aws_cloudwatch_log_group" "cloudwatch_log_group_api_gateway" {
count = (length(aws_apigatewayv2_api.api_gateway) == 1 && var.api_gateway_cloudwatch ? 1 : 0)
name = "/aws/api_gw/${aws_apigatewayv2_api.api_gateway[0].name}"
retention_in_days = var.cloudwatch_log_retention_days
tags = var.tags
}
//Create lambda execution role
resource "aws_iam_role" "lambda_function_execution_role" {
name = var.lambda_execution_role_name
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
// Attaches a Managed IAM Policy to an IAM role
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_function_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
// Manages an Amazon API Gateway Version 2 API.
resource "aws_apigatewayv2_api" "api_gateway" {
count = (var.api_gateway ? 1 : 0)
name = var.lambda_app_name
protocol_type = var.api_gw_protocol_type
tags = var.tags
}
// Manages an Amazon API Gateway Version 2 stage
resource "aws_apigatewayv2_stage" "api_gateway_stage" {
count = (length(aws_apigatewayv2_api.api_gateway) == 1 ? 1 : 0)
api_id = aws_apigatewayv2_api.api_gateway[0].id
name = var.lambda_app_name
auto_deploy = true
tags = var.tags
lifecycle { ignore_changes = [access_log_settings] }
dynamic "access_log_settings" {
for_each = aws_cloudwatch_log_group.cloudwatch_log_group_api_gateway
content {
destination_arn = aws_cloudwatch_log_group.cloudwatch_log_group_api_gateway[0].arn
format = jsonencode({
requestId = "$context.requestId"
sourceIp = "$context.identity.sourceIp"
requestTime = "$context.requestTime"
protocol = "$context.protocol"
httpMethod = "$context.httpMethod"
resourcePath = "$context.resourcePath"
routeKey = "$context.routeKey"
status = "$context.status"
responseLength = "$context.responseLength"
integrationErrorMessage = "$context.integrationErrorMessage"
}
)
}
}
}
// Manages an Amazon API Gateway Version 2 integration
resource "aws_apigatewayv2_integration" "api_gateway_integration" {
count = (length(aws_apigatewayv2_api.api_gateway) == 1 ? 1 : 0)
api_id = aws_apigatewayv2_api.api_gateway[0].id
integration_uri = aws_lambda_function.lambda_function.invoke_arn
integration_type = var.api_gw_integration_type
integration_method = var.api_gw_integration_methode
}
resource "aws_apigatewayv2_route" "api_gateway_route" {
count = (length(aws_apigatewayv2_api.api_gateway) == 1 ? 1 : 0)
api_id = aws_apigatewayv2_api.api_gateway[0].id
route_key = var.api_gw_route_key
target = "integrations/${aws_apigatewayv2_integration.api_gateway_integration[0].id}"
}
// Gives an external source (like an EventBridge Rule, SNS, or S3) permission to access the Lambda function.
//resource "aws_lambda_permission" "my-slack_event_handler" {
resource "aws_lambda_permission" "api_gateway_lambda_permission" {
count = (length(aws_apigatewayv2_api.api_gateway) == 1 ? 1 : 0)
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_function.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.api_gateway[0].execution_arn}/*/*"
}