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
# HTTP/WebSocket API =========================================================

resource "aws_apigatewayv2_api" "this" {
  name          = var.name
  protocol_type = var.protocol_type
  description   = var.description

  dynamic "cors_configuration" {
    for_each = var.cors_configuration != null ? [var.cors_configuration] : []
    content {
      allow_origins     = cors_configuration.value.allow_origins
      allow_methods     = cors_configuration.value.allow_methods
      allow_headers     = cors_configuration.value.allow_headers
      expose_headers    = cors_configuration.value.expose_headers
      max_age           = cors_configuration.value.max_age
      allow_credentials = cors_configuration.value.allow_credentials
    }
  }

  tags = local.tags
}

# Integrations ===============================================================

resource "aws_apigatewayv2_integration" "this" {
  for_each = var.integrations

  api_id                 = aws_apigatewayv2_api.this.id
  integration_type       = each.value.integration_type
  integration_uri        = each.value.integration_uri
  integration_method     = each.value.integration_method
  payload_format_version = each.value.payload_format_version
  timeout_milliseconds   = each.value.timeout_milliseconds
}

# Routes =====================================================================

resource "aws_apigatewayv2_route" "this" {
  for_each = var.routes

  api_id             = aws_apigatewayv2_api.this.id
  route_key          = each.value.route_key
  authorization_type = each.value.authorization_type
  authorizer_id      = each.value.authorizer_id
  target             = each.value.integration_key != null ? "integrations/${aws_apigatewayv2_integration.this[each.value.integration_key].id}" : null
}

# Stages =====================================================================

resource "aws_apigatewayv2_stage" "this" {
  for_each = var.stages

  api_id      = aws_apigatewayv2_api.this.id
  name        = each.value.name
  auto_deploy = each.value.auto_deploy

  dynamic "access_log_settings" {
    for_each = each.value.access_log_arn != null ? [each.value] : []
    content {
      destination_arn = access_log_settings.value.access_log_arn
      format          = access_log_settings.value.access_log_format
    }
  }

  dynamic "default_route_settings" {
    for_each = each.value.default_route_settings != null ? [each.value.default_route_settings] : []
    content {
      throttling_burst_limit = default_route_settings.value.throttling_burst_limit
      throttling_rate_limit  = default_route_settings.value.throttling_rate_limit
    }
  }

  tags = local.tags
}

# Custom domain ==============================================================

resource "aws_apigatewayv2_domain_name" "this" {
  for_each = var.custom_domain != null ? { domain = var.custom_domain } : {}

  domain_name = each.value.domain_name

  domain_name_configuration {
    certificate_arn = each.value.acm_certificate_arn
    endpoint_type   = each.value.endpoint_type
    security_policy = "TLS_1_2"
  }

  tags = local.tags
}

resource "aws_apigatewayv2_api_mapping" "this" {
  for_each = var.custom_domain != null ? { mapping = var.custom_domain } : {}

  api_id          = aws_apigatewayv2_api.this.id
  domain_name     = aws_apigatewayv2_domain_name.this["domain"].id
  stage           = aws_apigatewayv2_stage.this[each.value.stage_key].id
  api_mapping_key = each.value.api_mapping_key
}