-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterraform.tf
148 lines (128 loc) · 3.87 KB
/
terraform.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
variable "bucket" {}
variable "key" {}
variable "access_key" {}
variable "secret_key" {}
variable "region" {}
variable "github_app_id" {}
variable "sites" {}
# terraform init -backend-config=terraform.tfvars
terraform {
required_version = ">= 0.12"
backend "s3" {}
}
locals {
app = var.key
}
data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "dist"
output_path = "dist.zip"
}
provider "aws" {
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
data "aws_caller_identity" "current" {}
resource "random_string" "webhook_secret" {
length = 16
}
resource "aws_api_gateway_rest_api" "api" {
name = "${local.app}-api"
}
resource "aws_api_gateway_resource" "webhook" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_rest_api.api.root_resource_id
path_part = "webhook"
}
resource "aws_api_gateway_method" "webhook_post" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.webhook.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "webhook_integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.webhook.id
http_method = aws_api_gateway_method.webhook_post.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.functions["webhook"].invoke_arn
}
resource "aws_api_gateway_deployment" "api" {
depends_on = [aws_api_gateway_integration.webhook_integration]
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "production"
lifecycle {
create_before_destroy = true
}
}
resource "aws_lambda_function" "functions" {
for_each = {
webhook = "handler"
manual = "manual"
}
function_name = "${local.app}-${each.key}"
filename = data.archive_file.lambda_zip.output_path
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
runtime = "nodejs12.x"
layers = [
# https://github.com/lambci/git-lambda-layer
"arn:aws:lambda:us-east-1:553035198032:layer:git-lambda2:7",
# https://github.com/shelfio/chrome-aws-lambda-layer
"arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:21",
]
memory_size = 1024
handler = "index.${each.value}"
environment {
variables = {
GITHUB_APP_ID = var.github_app_id
GITHUB_PRIVATE_KEY = file("github-private-key.pem")
WEBHOOK_SECRET = random_string.webhook_secret.result
SITES = jsonencode(var.sites)
}
}
role = aws_iam_role.lambda.arn
timeout = 90
depends_on = [aws_iam_role_policy.lambda]
tags = {
Terraform = local.app
}
}
resource "aws_lambda_permission" "invoke_webhook" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.functions["webhook"].function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/${aws_api_gateway_method.webhook_post.http_method}${aws_api_gateway_resource.webhook.path}"
}
data "aws_iam_policy_document" "assume_role_lambda" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda" {
name = "${local.app}-lambda-role"
assume_role_policy = data.aws_iam_policy_document.assume_role_lambda.json
}
data "aws_iam_policy_document" "lambda" {
statement {
actions = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
resources = ["arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:*"]
}
}
resource "aws_iam_role_policy" "lambda" {
name = "${local.app}-lambda-policy"
role = aws_iam_role.lambda.id
policy = data.aws_iam_policy_document.lambda.json
}
resource "aws_cloudwatch_log_group" "logs" {
name = "/aws/lambda/${aws_lambda_function.functions["webhook"].function_name}"
retention_in_days = 1
tags = {
Terraform = local.app
}
}