diff --git a/_examples/complete/example.tf b/_examples/complete/http-api-gateway/example.tf similarity index 67% rename from _examples/complete/example.tf rename to _examples/complete/http-api-gateway/example.tf index 8d67613..94026e0 100644 --- a/_examples/complete/example.tf +++ b/_examples/complete/http-api-gateway/example.tf @@ -1,16 +1,23 @@ ####---------------------------------------------------------------------------------- -## Provider block added, Use the Amazon Web Services (AWS) provider to interact with the many resources supported by AWS. +## PROVIDER ####---------------------------------------------------------------------------------- provider "aws" { - region = "eu-west-1" + region = local.region } +####---------------------------------------------------------------------------------- +## LOCALS +####---------------------------------------------------------------------------------- + locals { - name = "api" - environment = "test" + name = "api" + environment = "test" + region = "us-east-1" + domain_name = "clouddrove.ca" + hosted_zone_id = "Z015XXXXXXXXXXXXXXIEP" } ####---------------------------------------------------------------------------------- -## This terraform module is designed to generate consistent label names and tags for resources. +## ACM ####---------------------------------------------------------------------------------- module "acm" { source = "clouddrove/acm/aws" @@ -19,14 +26,14 @@ module "acm" { name = local.name environment = local.environment enable_aws_certificate = true - domain_name = "clouddrove.ca" - subject_alternative_names = ["*.clouddrove.ca"] + domain_name = local.domain_name + subject_alternative_names = ["*.${local.domain_name}"] validation_method = "DNS" enable_dns_validation = false } ####---------------------------------------------------------------------------------- -## This terraform module is designed to generate consistent label names and tags for resources. +## LAMBDA ####---------------------------------------------------------------------------------- module "lambda" { source = "clouddrove/lambda/aws" @@ -34,9 +41,9 @@ module "lambda" { name = local.name environment = local.environment - enabled = true + enable = true timeout = 60 - filename = "./lambda_packages" + filename = "../lambda_packages/index.zip" handler = "index.lambda_handler" runtime = "python3.8" iam_actions = [ @@ -47,38 +54,39 @@ module "lambda" { names = [ "python_layer" ] - layer_filenames = ["./lambda-test.zip"] compatible_runtimes = [ ["python3.8"] ] statement_ids = [ - "AllowExecutionFromCloudWatch" + "AllowExecutionFromApiGateway" ] actions = [ "lambda:InvokeFunction" ] principals = [ - "events.amazonaws.com" + "apigateway.amazonaws.com" ] - source_arns = [module.api_gateway.api_arn] variables = { foo = "bar" } } ####---------------------------------------------------------------------------------- -## This terraform module is designed to generate consistent label names and tags for resources. +## API GATEWAY ####---------------------------------------------------------------------------------- module "api_gateway" { - source = "./../../" + source = "../../../" name = local.name environment = local.environment - domain_name = "clouddrove.ca" + domain_name = "api.${local.domain_name}" domain_name_certificate_arn = module.acm.arn - integration_uri = module.lambda.arn - zone_id = "1234059QJ345674343" + integration_uri = module.lambda.invoke_arn + zone_id = local.hosted_zone_id + auto_deploy = true + stage_name = "$default" create_vpc_link_enabled = false + create_http_api = true cors_configuration = { allow_credentials = true allow_methods = ["GET", "OPTIONS", "POST"] @@ -88,16 +96,16 @@ module "api_gateway" { "ANY /" = { lambda_arn = module.lambda.arn payload_format_version = "2.0" - timeout_milliseconds = 12000 + timeout_milliseconds = 30000 } "GET /some-route-with-authorizer" = { lambda_arn = module.lambda.arn - payload_format_version = "2.0" + payload_format_version = "1.0" authorizer_key = "cognito" } "POST /start-step-function" = { lambda_arn = module.lambda.arn - payload_format_version = "2.0" + payload_format_version = "1.0" authorizer_key = "cognito" } } diff --git a/_examples/complete/outputs.tf b/_examples/complete/http-api-gateway/outputs.tf similarity index 57% rename from _examples/complete/outputs.tf rename to _examples/complete/http-api-gateway/outputs.tf index eef9623..7025048 100644 --- a/_examples/complete/outputs.tf +++ b/_examples/complete/http-api-gateway/outputs.tf @@ -1,19 +1,19 @@ output "api_id" { - value = join("", module.api_gateway[*].api_id) + value = module.api_gateway.api_id description = "The API identifier." } output "api_arn" { - value = join("", module.api_gateway[*].api_arn) + value = module.api_gateway.api_arn description = "The API arn." } output "api_endpoint" { - value = join("", module.api_gateway[*].api_endpoint) + value = module.api_gateway.api_endpoint description = "The URI of the API, of the form {api-id}.execute-api.{region}.amazonaws.com." } output "invoke_url" { - value = join("", module.api_gateway[*].invoke_url) + value = module.api_gateway.invoke_url description = "URL to invoke the API pointing to the stage" -} \ No newline at end of file +} diff --git a/_examples/complete/versions.tf b/_examples/complete/http-api-gateway/version.tf similarity index 100% rename from _examples/complete/versions.tf rename to _examples/complete/http-api-gateway/version.tf diff --git a/_examples/complete/lambda-test.zip b/_examples/complete/lambda-test.zip deleted file mode 100644 index 8589034..0000000 Binary files a/_examples/complete/lambda-test.zip and /dev/null differ diff --git a/_examples/complete/lambda_packages/index.py b/_examples/complete/lambda_packages/index.py index 6e5bd09..a0f3277 100644 --- a/_examples/complete/lambda_packages/index.py +++ b/_examples/complete/lambda_packages/index.py @@ -1,14 +1,7 @@ -import os import json - def lambda_handler(event, context): - json_region = os.environ['AWS_REGION'] - return { - "statusCode": 200, - "headers": { - "Content-Type": "application/json" - }, - "body": json.dumps({ - "Region ": json_region - }) - } + print('Lambda function with Python!|') + return { + 'statusCode': 200, + 'body': json.dumps('Hello from Lambda!') + } diff --git a/_examples/complete/lambda_packages/index.zip b/_examples/complete/lambda_packages/index.zip new file mode 100644 index 0000000..f7f278d Binary files /dev/null and b/_examples/complete/lambda_packages/index.zip differ diff --git a/_examples/complete/private-rest-api-gateway/example.tf b/_examples/complete/private-rest-api-gateway/example.tf new file mode 100644 index 0000000..d07b532 --- /dev/null +++ b/_examples/complete/private-rest-api-gateway/example.tf @@ -0,0 +1,225 @@ +####---------------------------------------------------------------------------------- +## PROVIDER +####---------------------------------------------------------------------------------- + +provider "aws" { + region = local.region +} +####---------------------------------------------------------------------------------- +## LOCALS +####---------------------------------------------------------------------------------- + +locals { + name = "api" + environment = "test" + region = "us-east-1" + domain_name = "clouddrove.ca" + hosted_zone_id = "Z015XXXXXXXXXXXXXXIEP" +} +####---------------------------------------------------------------------------------- +## ACM +####---------------------------------------------------------------------------------- + +module "acm" { + source = "clouddrove/acm/aws" + version = "1.4.1" + + name = local.name + environment = local.environment + enable_aws_certificate = true + domain_name = local.domain_name + subject_alternative_names = ["*.${local.domain_name}"] + validation_method = "DNS" + enable_dns_validation = false +} + +####---------------------------------------------------------------------------------- +## LAMBDA +####---------------------------------------------------------------------------------- + +module "lambda" { + source = "clouddrove/lambda/aws" + version = "1.3.1" + + name = local.name + environment = local.environment + enable = true + timeout = 60 + filename = "../lambda_packages/index.zip" + handler = "index.lambda_handler" + runtime = "python3.8" + iam_actions = [ + "logs:CreateLogStream", + "logs:CreateLogGroup", + "logs:PutLogEvents" + ] + names = [ + "python_layer" + ] + compatible_runtimes = [ + ["python3.8"] + ] + statement_ids = [ + "AllowExecutionFromApiGateway" + ] + actions = [ + "lambda:InvokeFunction" + ] + principals = [ + "apigateway.amazonaws.com" + ] + variables = { + foo = "bar" + } +} + + +####---------------------------------------------------------------------------------- +## VPC +####---------------------------------------------------------------------------------- + +module "vpc" { + source = "clouddrove/vpc/aws" + version = "2.0.0" + + name = "${local.name}-rest-api-private" + environment = local.environment + enable = true + cidr_block = "10.0.0.0/16" + +} + +####---------------------------------------------------------------------------------- +## SUBNETS +####---------------------------------------------------------------------------------- +#tfsec:ignore:aws-ec2-no-excessive-port-access +#tfsec:ignore:aws-ec2-no-public-ingress-acl +module "subnets" { + source = "clouddrove/subnet/aws" + version = "2.0.1" + + name = "${local.name}-rest-api-private" + environment = local.environment + + nat_gateway_enabled = true + single_nat_gateway = true + availability_zones = ["${local.region}a", "${local.region}b", "${local.region}c"] + vpc_id = module.vpc.vpc_id + type = "public-private" + igw_id = module.vpc.igw_id + cidr_block = module.vpc.vpc_cidr_block + ipv6_cidr_block = module.vpc.ipv6_cidr_block + enable_ipv6 = true + private_inbound_acl_rules = [ + { + rule_number = 100 + rule_action = "allow" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_block = module.vpc.vpc_cidr_block + } + ] + private_outbound_acl_rules = [ + { + rule_number = 100 + rule_action = "allow" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_block = module.vpc.vpc_cidr_block + } + ] + public_inbound_acl_rules = [ + { + rule_number = 100 + rule_action = "allow" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_block = "0.0.0.0/0" + } + ] + public_outbound_acl_rules = [ + { + rule_number = 100 + rule_action = "allow" + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_block = "0.0.0.0/0" + } + ] + +} + +####---------------------------------------------------------------------------------- +## SECURITY GROUP +####---------------------------------------------------------------------------------- + +module "security_group" { + source = "clouddrove/security-group/aws" + version = "2.0.0" + + name = "${local.name}-rest-api-private" + environment = local.environment + + vpc_id = module.vpc.vpc_id + new_sg_ingress_rules_with_cidr_blocks = [ + { + rule_count = 1 + from_port = 0 + protocol = "-1" + to_port = 0 + cidr_blocks = [module.vpc.vpc_cidr_block] + description = "Allow all traffic from ${local.environment} VPC." + } + ] + new_sg_egress_rules_with_cidr_blocks = [ + { + rule_count = 1 + from_port = 0 + protocol = "-1" + to_port = 0 + cidr_blocks = [module.vpc.vpc_cidr_block] + description = "Allow all outbound traffic." + } + ] +} + + +####---------------------------------------------------------------------------------- +## REST API PRIVATE +####---------------------------------------------------------------------------------- + +module "rest_api_private" { + source = "../../../" + + name = "${local.name}-rest-api-private" + environment = local.environment + enabled = true + create_rest_api = true + rest_api_endpoint_type = "PRIVATE" + rest_api_description = "Private REST API for ${module.lambda.name} lambda function" + integration_uri = module.lambda.invoke_arn + rest_api_stage_name = "default" + auto_deploy = true + rest_api_base_path = "test" + domain_name = "api.${local.domain_name}" + zone_id = local.hosted_zone_id + + # -- VPC Endpoint configuration + vpc_id = module.vpc.vpc_id + subnet_ids = module.subnets.private_subnet_id + security_group_ids = [module.security_group.security_group_id] + service_name = "com.amazonaws.${local.region}.execute-api" + vpc_endpoint_type = "Interface" + private_dns_enabled = true + domain_name_certificate_arn = module.acm.arn + + #---access log---- + enable_access_logs = true + retention_in_days = 7 +} + + diff --git a/_examples/complete/private-rest-api-gateway/outputs.tf b/_examples/complete/private-rest-api-gateway/outputs.tf new file mode 100644 index 0000000..289a67a --- /dev/null +++ b/_examples/complete/private-rest-api-gateway/outputs.tf @@ -0,0 +1,21 @@ +##------------------------------------------------------------- +# REST API PRIVATE +##------------------------------------------------------------- + +output "private_rest_api_id" { + value = module.rest_api_private.rest_api_id + description = " The ID of the REST API" + +} + +output "private_rest_api_arn" { + value = module.rest_api_private.rest_api_arn + description = "The Rest api arn" + +} + +output "private_rest_api_invoke_url" { + value = module.rest_api_private.rest_api_invoke_url + description = "The URL to invoke the API pointing to the stage" + +} \ No newline at end of file diff --git a/_examples/complete/private-rest-api-gateway/versions.tf b/_examples/complete/private-rest-api-gateway/versions.tf new file mode 100644 index 0000000..3f60246 --- /dev/null +++ b/_examples/complete/private-rest-api-gateway/versions.tf @@ -0,0 +1,11 @@ +# Terraform version +terraform { + required_version = ">= 1.6.1" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 5.20.0" + } + } +} \ No newline at end of file diff --git a/_examples/complete/rest-api-gateway/example.tf b/_examples/complete/rest-api-gateway/example.tf new file mode 100644 index 0000000..89ba1ab --- /dev/null +++ b/_examples/complete/rest-api-gateway/example.tf @@ -0,0 +1,115 @@ +####---------------------------------------------------------------------------------- +## PROVIDER +####---------------------------------------------------------------------------------- +provider "aws" { + region = local.region +} + +####---------------------------------------------------------------------------------- +## LOCALS +####---------------------------------------------------------------------------------- + +locals { + name = "api" + environment = "test" + region = "us-east-1" + domain_name = "clouddrove.ca" + hosted_zone_id = "Z015XXXXXXXXXXXXXXIEP" +} +####---------------------------------------------------------------------------------- +## ACM +####---------------------------------------------------------------------------------- +module "acm" { + source = "clouddrove/acm/aws" + version = "1.4.1" + + name = local.name + environment = local.environment + enable_aws_certificate = true + domain_name = local.domain_name + subject_alternative_names = ["*.${local.domain_name}"] + validation_method = "DNS" + enable_dns_validation = false +} + +####---------------------------------------------------------------------------------- +## LAMBDA +####---------------------------------------------------------------------------------- +module "lambda" { + source = "clouddrove/lambda/aws" + version = "1.3.1" + + name = local.name + environment = local.environment + enable = true + timeout = 60 + filename = "../lambda_packages/index.zip" + handler = "index.lambda_handler" + runtime = "python3.8" + iam_actions = [ + "logs:CreateLogStream", + "logs:CreateLogGroup", + "logs:PutLogEvents" + ] + names = [ + "python_layer" + ] + compatible_runtimes = [ + ["python3.8"] + ] + statement_ids = [ + "AllowExecutionFromApiGateway" + ] + actions = [ + "lambda:InvokeFunction" + ] + principals = [ + "apigateway.amazonaws.com" + ] + variables = { + foo = "bar" + } +} + + +####---------------------------------------------------------------------------------- +## REST API +####---------------------------------------------------------------------------------- + +module "rest_api" { + source = "../../../" + + name = "${local.name}-rest-api" + environment = local.environment + create_rest_api = true + domain_name_certificate_arn = module.acm.arn + domain_name = "api.${local.domain_name}" + zone_id = local.hosted_zone_id + rest_api_description = "REST API for ${module.lambda.name} lambda function" + rest_api_endpoint_type = "REGIONAL" + integration_uri = module.lambda.invoke_arn + rest_api_stage_name = "default" + api_resources = { + users = { + path_part = "users" + http_method = "ANY" + uri = module.lambda.invoke_arn + + }, + cards = { + path_part = "cards" + http_method = "ANY" + uri = module.lambda.invoke_arn + } + } + + #---access log---- + enable_access_logs = true + retention_in_days = 7 +} + + + + + + diff --git a/_examples/complete/rest-api-gateway/outputs.tf b/_examples/complete/rest-api-gateway/outputs.tf new file mode 100644 index 0000000..a0b81f7 --- /dev/null +++ b/_examples/complete/rest-api-gateway/outputs.tf @@ -0,0 +1,22 @@ +##------------------------------------------------------------- +# REST API +##------------------------------------------------------------- + +output "rest_api_id" { + value = module.rest_api.rest_api_id + description = " The ID of the REST API" + +} + +output "rest_api_arn" { + value = module.rest_api.rest_api_arn + description = "The Rest api arn" +} + +output "rest_api_invoke_url" { + value = module.rest_api.rest_api_invoke_url + description = "The URL to invoke the API pointing to the stage" + +} + + diff --git a/_examples/complete/rest-api-gateway/versions.tf b/_examples/complete/rest-api-gateway/versions.tf new file mode 100644 index 0000000..3f60246 --- /dev/null +++ b/_examples/complete/rest-api-gateway/versions.tf @@ -0,0 +1,11 @@ +# Terraform version +terraform { + required_version = ">= 1.6.1" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 5.20.0" + } + } +} \ No newline at end of file diff --git a/_examples/complete/test-api.zip b/_examples/complete/test-api.zip deleted file mode 100644 index 8589034..0000000 Binary files a/_examples/complete/test-api.zip and /dev/null differ diff --git a/_examples/vpc_link_api/example.tf b/_examples/vpc_link_api/example.tf index 7f77c2e..f2884a4 100644 --- a/_examples/vpc_link_api/example.tf +++ b/_examples/vpc_link_api/example.tf @@ -157,7 +157,7 @@ module "lambda" { name = local.name environment = local.environment - enabled = true + enable = true timeout = 60 filename = "./lambda_packages" handler = "index.lambda_handler" diff --git a/main.tf b/main.tf index c659ff9..e24f286 100644 --- a/main.tf +++ b/main.tf @@ -16,9 +16,9 @@ module "labels" { ## Below resource will Manages an Amazon API Gateway Version 2 API. ##---------------------------------------------------------------------------------- resource "aws_apigatewayv2_api" "default" { - count = var.enabled && var.create_api_gateway_enabled ? 1 : 0 + count = var.enabled && var.create_http_api ? 1 : 0 - name = format("%s", module.labels.id) + name = module.labels.id description = var.api_description protocol_type = var.protocol_type version = var.api_version @@ -39,19 +39,14 @@ resource "aws_apigatewayv2_api" "default" { max_age = try(cors_configuration.value.max_age, null) } } - tags = merge( - module.labels.tags, - { - "Name" = format("%s", module.labels.id) - } - ) + tags = module.labels.tags } ##---------------------------------------------------------------------------------- ## Below resource will Manages an Amazon API Gateway Version 2 domain name. ##---------------------------------------------------------------------------------- resource "aws_apigatewayv2_domain_name" "default" { - count = var.enabled && var.create_api_domain_name_enabled ? 1 : 0 + count = var.enabled && var.create_api_domain_name_enabled && (var.create_http_api || var.create_rest_api) ? 1 : 0 domain_name = var.domain_name domain_name_configuration { @@ -60,6 +55,7 @@ resource "aws_apigatewayv2_domain_name" "default" { endpoint_type = "REGIONAL" security_policy = "TLS_1_2" } + dynamic "mutual_tls_authentication" { for_each = var.mutual_tls_authentication content { @@ -67,19 +63,15 @@ resource "aws_apigatewayv2_domain_name" "default" { truststore_version = lookup(mutual_tls_authentication.value.truststore_version, null) } } - tags = merge( - module.labels.tags, - { - "Name" = format("%s-domain", module.labels.id) - } - ) + + tags = module.labels.tags } ##---------------------------------------------------------------------------------- ## Below Provides a Route53 record resource. ##---------------------------------------------------------------------------------- resource "aws_route53_record" "default" { - count = var.enabled ? 1 : 0 + count = var.enabled && (var.create_http_api || var.create_rest_api) && var.rest_api_endpoint_type != "PRIVATE" ? 1 : 0 name = join("", aws_apigatewayv2_domain_name.default[*].domain_name) type = "A" @@ -96,11 +88,12 @@ resource "aws_route53_record" "default" { ##---------------------------------------------------------------------------------- #tfsec:ignore:aws-api-gateway-enable-access-logging resource "aws_apigatewayv2_stage" "default" { - count = var.enabled && var.create_default_stage_enabled ? 1 : 0 + count = var.enabled && var.create_default_stage_enabled && var.create_http_api ? 1 : 0 api_id = aws_apigatewayv2_api.default[0].id name = var.stage_name != null ? var.stage_name : format("%s-stage", module.labels.id) auto_deploy = var.auto_deploy + dynamic "access_log_settings" { for_each = var.access_log_settings content { @@ -108,6 +101,7 @@ resource "aws_apigatewayv2_stage" "default" { format = var.default_stage_access_log_format } } + dynamic "default_route_settings" { for_each = var.default_route_settings @@ -120,6 +114,7 @@ resource "aws_apigatewayv2_stage" "default" { throttling_rate_limit = lookup(default_route_settings.value.throttling_rate_limit, null) } } + dynamic "route_settings" { for_each = var.route_settings content { @@ -131,19 +126,15 @@ resource "aws_apigatewayv2_stage" "default" { throttling_rate_limit = lookup(route_settings.value, "throttling_rate_limit", null) } } - tags = merge( - module.labels.tags, - { - "Name" = format("%s-stage", module.labels.id) - } - ) + + tags = module.labels.tags } ##---------------------------------------------------------------------------------- ## Below resource will Manages an Amazon API Gateway Version 2 API mapping. ##---------------------------------------------------------------------------------- resource "aws_apigatewayv2_api_mapping" "default" { - count = var.enabled && var.apigatewayv2_api_mapping_enabled ? 1 : 0 + count = var.enabled && var.apigatewayv2_api_mapping_enabled && var.create_http_api ? 1 : 0 api_id = join("", aws_apigatewayv2_api.default[*].id) domain_name = join("", aws_apigatewayv2_domain_name.default[*].id) @@ -154,7 +145,7 @@ resource "aws_apigatewayv2_api_mapping" "default" { ## Below resource will Manages an Amazon API Gateway Version 2 route. ##---------------------------------------------------------------------------------- resource "aws_apigatewayv2_route" "default" { - for_each = var.enabled && var.create_routes_and_integrations_enabled ? var.integrations : {} + for_each = var.enabled && var.create_routes_and_integrations_enabled && var.create_http_api ? var.integrations : {} api_id = aws_apigatewayv2_api.default[0].id route_key = each.key @@ -166,29 +157,31 @@ resource "aws_apigatewayv2_route" "default" { model_selection_expression = try(each.value.model_selection_expression, null) operation_name = try(each.value.operation_name, null) route_response_selection_expression = try(each.value.route_response_selection_expression, null) - target = "integrations/${join("", aws_apigatewayv2_integration.default[*].id)}" + target = "integrations/${(aws_apigatewayv2_integration.default[each.key].id)}" } ##---------------------------------------------------------------------------------- ## Below resource will Manages an Amazon API Gateway Version 2 integration. ##---------------------------------------------------------------------------------- + resource "aws_apigatewayv2_integration" "default" { - count = var.enabled && var.create_routes_and_integrations_enabled ? 1 : 0 + for_each = var.enabled && var.create_routes_and_integrations_enabled && var.create_http_api ? var.integrations : {} - api_id = join("", aws_apigatewayv2_api.default[*].id) - integration_type = var.integration_type - connection_type = var.connection_type - description = var.integration_description - integration_method = var.integration_method - integration_uri = var.integration_uri - passthrough_behavior = var.passthrough_behavior + api_id = join("", aws_apigatewayv2_api.default[*].id) + integration_type = var.integration_type + connection_type = var.connection_type + description = var.integration_description + integration_method = var.integration_method + integration_uri = var.integration_uri + passthrough_behavior = var.passthrough_behavior + payload_format_version = try(each.value.payload_format_version, null) } ##---------------------------------------------------------------------------------- ## Below resource will Manages an Amazon API Gateway Version 2 authorizer. ##---------------------------------------------------------------------------------- resource "aws_apigatewayv2_authorizer" "default" { - for_each = var.enabled && var.create_routes_and_integrations_enabled ? var.authorizers : {} + for_each = var.enabled && var.create_routes_and_integrations_enabled && var.create_http_api ? var.authorizers : {} api_id = aws_apigatewayv2_api.default[0].id authorizer_type = lookup(each.value.authorizer_type, null) @@ -205,24 +198,20 @@ resource "aws_apigatewayv2_authorizer" "default" { ## Below resource will Manages an Amazon API Gateway Version 2 VPC Link. ##---------------------------------------------------------------------------------- resource "aws_apigatewayv2_vpc_link" "default" { - for_each = var.enabled && var.create_vpc_link_enabled ? var.vpc_links : {} + for_each = var.enabled && var.create_vpc_link_enabled && var.create_http_api ? var.vpc_links : {} name = format("%s", module.labels.id) security_group_ids = var.security_group_ids subnet_ids = var.subnet_ids - tags = merge( - module.labels.tags, - { - "Name" = format("%s-vpc-link", module.labels.id) - } - ) + tags = module.labels.tags + } ##---------------------------------------------------------------------------------- ## Below resource will Manages an Amazon API Gateway Version 2 authorizer. ##---------------------------------------------------------------------------------- resource "aws_apigatewayv2_authorizer" "some_authorizer" { - count = var.enabled && var.create_routes_and_integrations_enabled ? 1 : 0 + count = var.enabled && var.create_routes_and_integrations_enabled && var.create_http_api ? 1 : 0 api_id = aws_apigatewayv2_api.default[0].id authorizer_type = var.authorizer_type @@ -240,3 +229,384 @@ resource "aws_apigatewayv2_authorizer" "some_authorizer" { resource "aws_cognito_user_pool" "default" { name = module.labels.id } + +##---------------------------------------------------------------------------------- +## Below resource will Provides a REST API resource. +##---------------------------------------------------------------------------------- +resource "aws_api_gateway_rest_api" "rest_api" { + count = var.enabled && var.create_rest_api ? 1 : 0 + + name = module.labels.id + description = var.rest_api_description + tags = module.labels.tags + + endpoint_configuration { + types = [var.rest_api_endpoint_type] + vpc_endpoint_ids = var.rest_api_endpoint_type == "PRIVATE" ? (var.create_vpc_endpoint ? [aws_vpc_endpoint.rest_api_private[0].id] : var.vpc_endpoint_id) : null + } +} + +##-------------------------------------------------------------------------------- +# Resource Policy for [aws_api_gateway_rest_api.rest_api] +##-------------------------------------------------------------------------------- +resource "aws_api_gateway_rest_api_policy" "rest_api_resource_policy" { + count = var.enabled && var.create_rest_api && var.rest_api_endpoint_type == "PRIVATE" ? 1 : 0 + + rest_api_id = aws_api_gateway_rest_api.rest_api[0].id + policy = var.rest_api_resource_policy != "" ? var.rest_api_resource_policy : <