# 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 }