generated from cloudposse/terraform-example-module
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.tf
96 lines (78 loc) · 2.9 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
locals {
enabled = module.this.enabled
create_rest_api_policy = local.enabled && var.rest_api_policy != null
create_log_group = local.enabled && var.logging_level != "OFF"
log_group_arn = local.create_log_group ? module.cloudwatch_log_group.log_group_arn : null
vpc_link_enabled = local.enabled && length(var.private_link_target_arns) > 0
}
resource "aws_api_gateway_rest_api" "this" {
count = local.enabled ? 1 : 0
name = module.this.id
body = jsonencode(var.openapi_config)
tags = module.this.tags
endpoint_configuration {
types = [var.endpoint_type]
vpc_endpoint_ids = var.vpc_endpoints
}
}
resource "aws_api_gateway_rest_api_policy" "this" {
count = local.create_rest_api_policy ? 1 : 0
rest_api_id = aws_api_gateway_rest_api.this[0].id
policy = var.rest_api_policy
}
module "cloudwatch_log_group" {
source = "cloudposse/cloudwatch-logs/aws"
version = "0.6.8"
enabled = local.create_log_group
iam_tags_enabled = var.iam_tags_enabled
permissions_boundary = var.permissions_boundary
context = module.this.context
}
resource "aws_api_gateway_deployment" "this" {
count = local.enabled ? 1 : 0
rest_api_id = aws_api_gateway_rest_api.this[0].id
triggers = {
redeployment = sha1(jsonencode(aws_api_gateway_rest_api.this[0].body))
}
lifecycle {
create_before_destroy = true
}
depends_on = [aws_api_gateway_rest_api_policy.this]
}
resource "aws_api_gateway_stage" "this" {
count = local.enabled ? 1 : 0
deployment_id = aws_api_gateway_deployment.this[0].id
rest_api_id = aws_api_gateway_rest_api.this[0].id
stage_name = var.stage_name != "" ? var.stage_name : module.this.stage
xray_tracing_enabled = var.xray_tracing_enabled
tags = module.this.tags
variables = {
vpc_link_id = local.vpc_link_enabled ? aws_api_gateway_vpc_link.this[0].id : null
}
dynamic "access_log_settings" {
for_each = local.create_log_group ? [1] : []
content {
destination_arn = local.log_group_arn
format = replace(var.access_log_format, "\n", "")
}
}
}
# Set the logging, metrics and tracing levels for all methods
resource "aws_api_gateway_method_settings" "all" {
count = local.enabled ? 1 : 0
rest_api_id = aws_api_gateway_rest_api.this[0].id
stage_name = aws_api_gateway_stage.this[0].stage_name
method_path = "*/*"
settings {
metrics_enabled = var.metrics_enabled
logging_level = var.logging_level
data_trace_enabled = var.data_trace_enabled
}
}
# Optionally create a VPC Link to allow the API Gateway to communicate with private resources (e.g. ALB)
resource "aws_api_gateway_vpc_link" "this" {
count = local.vpc_link_enabled ? 1 : 0
name = module.this.id
description = "VPC Link for ${module.this.id}"
target_arns = var.private_link_target_arns
}