-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_gw.tf
84 lines (71 loc) · 2.89 KB
/
api_gw.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
resource "aws_api_gateway_rest_api" "webhook" {
name = local.base_name
description = "HCP Packer webhook API for HCP org '${local.hcp_org_name}', project '${local.hcp_project_name}'."
endpoint_configuration {
types = ["EDGE"]
}
}
resource "aws_api_gateway_deployment" "webhook" {
rest_api_id = aws_api_gateway_rest_api.webhook.id
depends_on = [aws_api_gateway_integration.webhook]
lifecycle {
create_before_destroy = true
}
}
locals {
stage_name = "default"
}
resource "aws_api_gateway_stage" "webhook" {
deployment_id = aws_api_gateway_deployment.webhook.id
rest_api_id = aws_api_gateway_rest_api.webhook.id
stage_name = local.stage_name
}
resource "aws_api_gateway_resource" "webhook" {
rest_api_id = aws_api_gateway_rest_api.webhook.id
parent_id = aws_api_gateway_rest_api.webhook.root_resource_id
path_part = "handler"
}
resource "aws_api_gateway_method" "webhook" {
rest_api_id = aws_api_gateway_rest_api.webhook.id
resource_id = aws_api_gateway_resource.webhook.id
http_method = "POST"
authorization = "NONE"
request_parameters = {
"method.request.header.X-Hcp-Webhook-Signature" = true
}
}
# API Gateway execution logging requires a CloudWatch log role to be set up in your API Gateway settings.
# Because this is an account/region-level setting, this is not configured here.
# See https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html?icmpid=apigateway_console_help#set-up-access-logging-permissions
resource "aws_api_gateway_method_settings" "webhook" {
count = var.enable_api_gateway_logging ? 1 : 0
rest_api_id = aws_api_gateway_rest_api.webhook.id
stage_name = aws_api_gateway_stage.webhook.stage_name
method_path = "*/*"
settings {
logging_level = var.api_gateway_logging_level
metrics_enabled = true
data_trace_enabled = false
}
depends_on = [aws_cloudwatch_log_group.webhook_api_gateway]
}
resource "aws_cloudwatch_log_group" "webhook_api_gateway" {
count = var.enable_api_gateway_logging ? 1 : 0
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.webhook.id}/${local.stage_name}"
retention_in_days = var.log_retention_days
}
resource "aws_api_gateway_integration" "webhook" {
rest_api_id = aws_api_gateway_rest_api.webhook.id
resource_id = aws_api_gateway_resource.webhook.id
http_method = aws_api_gateway_method.webhook.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.webhook.invoke_arn
}
resource "aws_lambda_permission" "apigw_lambda" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.webhook.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.webhook.execution_arn}/*"
}