-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambdas.tf
146 lines (128 loc) · 4.16 KB
/
lambdas.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
resource "aws_apigatewayv2_api" "webhook" {
name = "gateway-github-monitoring-dashboard"
protocol_type = "HTTP"
tags = local.tags
}
resource "aws_apigatewayv2_route" "webhook" {
api_id = aws_apigatewayv2_api.webhook.id
route_key = "POST /webhook"
target = "integrations/${aws_apigatewayv2_integration.webhook.id}"
}
resource "aws_apigatewayv2_stage" "webhook" {
lifecycle {
ignore_changes = [
// see bug https://github.com/terraform-providers/terraform-provider-aws/issues/12893
default_route_settings,
// not terraform managed
deployment_id
]
}
api_id = aws_apigatewayv2_api.webhook.id
name = "$default"
auto_deploy = true
tags = local.tags
}
resource "aws_apigatewayv2_integration" "webhook" {
lifecycle {
ignore_changes = [
// not terraform managed
passthrough_behavior
]
}
api_id = aws_apigatewayv2_api.webhook.id
integration_type = "AWS_PROXY"
connection_type = "INTERNET"
description = "GitHub App webhook for receiving events."
integration_method = "POST"
integration_uri = aws_lambda_function.webhook.invoke_arn
request_parameters = {
"overwrite:header.x-github-organization" = "$request.body.organization.login"
"overwrite:header.x-github-repository" = "$request.body.repository.name"
}
}
resource "aws_lambda_function" "webhook" {
filename = "${path.module}/lambdas/webhook/webhook.zip"
source_code_hash = filebase64sha256("${path.module}/lambdas/webhook/webhook.zip")
function_name = "webhook-github-monitoring-dashboard"
role = aws_iam_role.webhook_lambda.arn
handler = "index.githubWebhook"
runtime = "nodejs16.x"
timeout = 10
architectures = ["x86_64"]
environment {
variables = {
LOG_LEVEL = var.log_level
LOG_TYPE = var.log_type
ORGANIZATION_ALLOW_LIST = jsonencode(var.organization_allow_list)
}
}
tags = local.tags
}
resource "aws_cloudwatch_log_group" "webhook" {
name = "/aws/lambda/${aws_lambda_function.webhook.function_name}"
retention_in_days = var.logging_retention_in_days
tags = local.tags
}
resource "aws_lambda_permission" "webhook" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.webhook.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.webhook.execution_arn}/*/*/webhook"
}
data "aws_iam_policy_document" "lambda_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "webhook_lambda" {
name = "role-github-monitoring-dashboard"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
path = "/github-monitoring-dashboard/"
tags = local.tags
}
resource "aws_iam_role_policy" "webhook_logging" {
name = "logging-policy-github-monitoring-dashboard"
role = aws_iam_role.webhook_lambda.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "${aws_cloudwatch_log_group.webhook.arn}*"
}
]
})
}
resource "aws_iam_role_policy" "webhook_ssm" {
name = "ssm-policy-github-monitoring-dashboard"
role = aws_iam_role.webhook_lambda.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ssm:GetParameter"
]
Resource = [
"${aws_ssm_parameter.github_app_id.arn}",
"${aws_ssm_parameter.github_app_key_base64.arn}",
"${aws_ssm_parameter.github_app_webhook_secret.arn}",
"${aws_ssm_parameter.rds_address.arn}",
"${aws_ssm_parameter.rds_port.arn}",
"${aws_ssm_parameter.rds_username.arn}",
"${aws_ssm_parameter.rds_password.arn}"
]
}
]
})
}