-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
157 lines (135 loc) · 5.09 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
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
resource "aws_api_gateway_deployment" "bitbucket" {
depends_on = ["aws_api_gateway_integration_response.event"]
stage_description = "${var.deploy_version}"
stage_name = ""
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_api_gateway_integration" "event" {
count = "${length(var.endpoints)}"
credentials = "${aws_iam_role.api.arn}"
http_method = "${aws_api_gateway_method.event.*.http_method[count.index]}"
integration_http_method = "POST"
resource_id = "${aws_api_gateway_method.event.*.resource_id[count.index]}"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
type = "AWS"
uri = "arn:aws:apigateway:${data.aws_region.current.name}:sns:path//"
request_parameters = {
"integration.request.header.Content-Type" = "'application/x-www-form-urlencoded'"
}
request_templates = {
"application/json" = <<EOF
Action=Publish##
&Message=$util.urlEncode($input.body)##
&MessageAttributes.entry.1.Name=event##
&MessageAttributes.entry.1.Value.DataType=String##
&MessageAttributes.entry.1.Value.StringValue=$util.urlEncode($input.params('X-Event-Key'))##
&MessageAttributes.entry.2.Name=signature##
&MessageAttributes.entry.2.Value.DataType=String##
&MessageAttributes.entry.2.Value.StringValue=$util.urlEncode($input.params('X-Hub-Signature'))##
&TopicArn=${urlencode(aws_sns_topic.event.*.arn[count.index])}##
EOF
}
}
resource "aws_api_gateway_integration_response" "event" {
count = "${length(var.endpoints)}"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
resource_id = "${aws_api_gateway_method.event.*.resource_id[count.index]}"
http_method = "POST"
status_code = "${aws_api_gateway_method_response.event-200.*.status_code[count.index]}"
depends_on = ["aws_api_gateway_integration.event"]
}
resource "aws_api_gateway_method" "event" {
authorization = "NONE" # authorizers do not have access to the body
count = "${length(var.endpoints)}"
http_method = "POST"
request_validator_id = "${aws_api_gateway_request_validator.bitbucket.id}"
resource_id = "${aws_api_gateway_resource.event.*.id[count.index]}"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
request_parameters = {
"method.request.header.X-Event-Key" = true
}
}
resource "aws_api_gateway_method_response" "event-200" {
count = "${length(var.endpoints)}"
http_method = "${aws_api_gateway_method.event.*.http_method[count.index]}"
resource_id = "${aws_api_gateway_method.event.*.resource_id[count.index]}"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
status_code = 200
}
resource "aws_api_gateway_method_settings" "bitbucket" {
method_path = "*/*"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
stage_name = "${aws_api_gateway_stage.bitbucket.stage_name}"
settings {
metrics_enabled = true
logging_level = "INFO"
}
}
resource "aws_api_gateway_request_validator" "bitbucket" {
name = "validate"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
validate_request_parameters = true
}
resource "aws_api_gateway_resource" "event" {
count = "${length(var.endpoints)}"
parent_id = "${aws_api_gateway_rest_api.bitbucket.root_resource_id}"
path_part = "${var.endpoints[count.index]}"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
}
resource "aws_api_gateway_rest_api" "bitbucket" {
description = "bitbucket webhook consumer"
name = "${var.name}"
}
resource "aws_api_gateway_stage" "bitbucket" {
deployment_id = "${aws_api_gateway_deployment.bitbucket.id}"
rest_api_id = "${aws_api_gateway_rest_api.bitbucket.id}"
stage_name = "latest"
access_log_settings {
destination_arn = "arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/bitbucket/webhook/access"
format = "$context.identity.sourceIp $context.identity.caller $context.identity.user [$context.requestTime] $context.httpMethod $context.resourcePath $context.protocol $context.status $context.responseLength $context.requestId"
}
}
resource "aws_iam_role" "api" {
name = "${var.name}-api"
path = "/bitbucket/"
assume_role_policy = <<EOF
{
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Sid": ""
}
],
"Version": "2012-10-17"
}
EOF
}
resource "aws_iam_role_policy" "api-sns" {
name = "sns"
role = "${aws_iam_role.api.id}"
policy = <<EOF
{
"Statement": [
{
"Action": "sns:Publish",
"Effect": "Allow",
"Resource": ${jsonencode(aws_sns_topic.event.*.arn)}
}
],
"Version": "2012-10-17"
}
EOF
}
resource "aws_sns_topic" "event" {
count = "${length(var.endpoints)}"
name = "${var.name}-${var.endpoints[count.index]}"
}