Skip to content

Commit 7d71393

Browse files
committed
Initial commit
1 parent 642c360 commit 7d71393

File tree

9 files changed

+460
-58
lines changed

9 files changed

+460
-58
lines changed

.github/settings.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# Upstream changes from _extends are only recognized when modifications are made to this file in the default branch.
22
_extends: .github
33
repository:
4-
name: template
5-
description: Template for Terraform Components
4+
name: aws-api-gateway-rest-api
5+
description: This component is responsible for deploying an API Gateway REST API
66
homepage: https://cloudposse.com/accelerate
77
topics: terraform, terraform-component
8-
9-
10-
11-

README.yaml

Lines changed: 136 additions & 48 deletions
Large diffs are not rendered by default.

src/main.tf

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,77 @@
11
locals {
22
enabled = module.this.enabled
3+
4+
sub_domain = var.name
5+
root_domain = coalesce(module.acm.outputs.domain_name, join(".", [
6+
module.this.environment, module.dns_delegated.outputs.default_domain_name
7+
]), module.dns_delegated.outputs.default_domain_name)
8+
domain_name = join(".", [local.sub_domain, local.root_domain])
9+
}
10+
11+
module "api_gateway_rest_api" {
12+
source = "cloudposse/api-gateway/aws"
13+
version = "0.3.1"
14+
15+
enabled = local.enabled
16+
17+
openapi_config = var.openapi_config
18+
endpoint_type = var.endpoint_type
19+
logging_level = var.logging_level
20+
metrics_enabled = var.metrics_enabled
21+
xray_tracing_enabled = var.xray_tracing_enabled
22+
access_log_format = var.access_log_format
23+
rest_api_policy = var.rest_api_policy
24+
private_link_target_arns = module.nlb[*].nlb_arn
25+
26+
context = module.this.context
27+
}
28+
29+
data "aws_acm_certificate" "issued" {
30+
count = local.enabled ? 1 : 0
31+
domain = local.root_domain
32+
statuses = ["ISSUED"]
333
}
434

35+
data "aws_route53_zone" "this" {
36+
count = local.enabled ? 1 : 0
37+
name = module.dns_delegated.outputs.default_domain_name
38+
private_zone = false
39+
}
40+
41+
resource "aws_api_gateway_domain_name" "this" {
42+
count = local.enabled ? 1 : 0
43+
domain_name = local.domain_name
44+
regional_certificate_arn = data.aws_acm_certificate.issued[0].arn
45+
46+
endpoint_configuration {
47+
types = ["REGIONAL"]
48+
}
49+
50+
tags = module.this.tags
51+
}
52+
53+
resource "aws_api_gateway_base_path_mapping" "this" {
54+
count = local.enabled ? 1 : 0
55+
api_id = module.api_gateway_rest_api.id
56+
domain_name = aws_api_gateway_domain_name.this[0].domain_name
57+
stage_name = module.this.stage
558

59+
depends_on = [
60+
aws_api_gateway_domain_name.this,
61+
module.api_gateway_rest_api
62+
]
663

64+
}
765

66+
resource "aws_route53_record" "this" {
67+
count = local.enabled ? 1 : 0
68+
name = aws_api_gateway_domain_name.this[0].domain_name
69+
type = "A"
70+
zone_id = data.aws_route53_zone.this[0].id
871

72+
alias {
73+
evaluate_target_health = true
74+
name = aws_api_gateway_domain_name.this[0].regional_domain_name
75+
zone_id = aws_api_gateway_domain_name.this[0].regional_zone_id
76+
}
77+
}

src/nlb.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module "nlb" {
2+
source = "cloudposse/nlb/aws"
3+
version = "0.12.0"
4+
count = var.enable_private_link_nlb ? 1 : 0
5+
6+
enabled = local.enabled
7+
8+
vpc_id = module.vpc.outputs.vpc.id
9+
subnet_ids = module.vpc.outputs.private_subnet_ids
10+
internal = true
11+
tcp_enabled = true
12+
cross_zone_load_balancing_enabled = true
13+
ip_address_type = "ipv4"
14+
deletion_protection_enabled = var.enable_private_link_nlb_deletion_protection
15+
tcp_port = 443
16+
target_group_port = 443
17+
target_group_target_type = "alb"
18+
health_check_protocol = "HTTPS"
19+
nlb_access_logs_s3_bucket_force_destroy = true
20+
deregistration_delay = var.deregistration_delay
21+
22+
context = module.this.context
23+
}
24+
25+
## You can use a target attachment like below to point the nlb at an ecs alb.
26+
#resource "aws_lb_target_group_attachment" "alb" {
27+
# target_group_arn = one(module.nlb[*].default_target_group_arn)
28+
# target_id = module.ecs.outputs.alb_arn
29+
# port = 443
30+
#}

src/outputs.tf

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1-
output "mock" {
2-
description = "Mock output example for the Cloud Posse Terraform component template"
3-
value = local.enabled ? "hello ${basename(abspath(path.module))}" : ""
1+
output "id" {
2+
description = "The ID of the REST API"
3+
value = module.this.enabled ? module.api_gateway_rest_api.id : null
4+
}
5+
6+
output "root_resource_id" {
7+
description = "The resource ID of the REST API's root"
8+
value = module.this.enabled ? module.api_gateway_rest_api.root_resource_id : null
9+
}
10+
11+
output "created_date" {
12+
description = "The date the REST API was created"
13+
value = module.this.enabled ? module.api_gateway_rest_api.created_date : null
14+
}
15+
16+
output "execution_arn" {
17+
description = <<EOF
18+
The execution ARN part to be used in lambda_permission's source_arn when allowing API Gateway to invoke a Lambda
19+
function, e.g., arn:aws:execute-api:eu-west-2:123456789012:z4675bid1j, which can be concatenated with allowed stage,
20+
method and resource path.The ARN of the Lambda function that will be executed.
21+
EOF
22+
value = module.this.enabled ? module.api_gateway_rest_api.execution_arn : null
23+
}
24+
25+
output "arn" {
26+
description = "The ARN of the REST API"
27+
value = module.this.enabled ? module.api_gateway_rest_api.arn : null
28+
}
29+
30+
output "invoke_url" {
31+
description = "The URL to invoke the REST API"
32+
value = module.this.enabled ? module.api_gateway_rest_api.invoke_url : null
433
}

src/providers.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
provider "aws" {
2+
region = var.region
3+
4+
# Profile is deprecated in favor of terraform_role_arn. When profiles are not in use, terraform_profile_name is null.
5+
profile = module.iam_roles.terraform_profile_name
6+
7+
dynamic "assume_role" {
8+
# module.iam_roles.terraform_role_arn may be null, in which case do not assume a role.
9+
for_each = compact([module.iam_roles.terraform_role_arn])
10+
content {
11+
role_arn = assume_role.value
12+
}
13+
}
14+
}
15+
16+
module "iam_roles" {
17+
source = "../account-map/modules/iam-roles"
18+
context = module.this.context
19+
}

src/remote-state.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module "dns_delegated" {
2+
source = "cloudposse/stack-config/yaml//modules/remote-state"
3+
version = "1.5.0"
4+
5+
component = "dns-delegated"
6+
environment = module.iam_roles.global_environment_name
7+
8+
context = module.this.context
9+
}
10+
11+
module "acm" {
12+
source = "cloudposse/stack-config/yaml//modules/remote-state"
13+
version = "1.5.0"
14+
15+
component = "acm"
16+
ignore_errors = true
17+
18+
defaults = {
19+
domain_name = ""
20+
}
21+
22+
context = module.this.context
23+
}
24+
25+
module "vpc" {
26+
source = "cloudposse/stack-config/yaml//modules/remote-state"
27+
version = "1.5.0"
28+
29+
component = "vpc"
30+
31+
context = module.this.context
32+
}

src/variables.tf

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
variable "region" {
2+
type = string
3+
description = "AWS Region"
4+
}
5+
6+
# See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html for additional
7+
# configuration information.
8+
variable "openapi_config" {
9+
description = "The OpenAPI specification for the API"
10+
type = any
11+
default = {}
12+
}
13+
14+
variable "endpoint_type" {
15+
type = string
16+
description = "The type of the endpoint. One of - PUBLIC, PRIVATE, REGIONAL"
17+
default = "REGIONAL"
18+
19+
validation {
20+
condition = contains(["EDGE", "REGIONAL", "PRIVATE"], var.endpoint_type)
21+
error_message = "Valid values for var: endpoint_type are (EDGE, REGIONAL, PRIVATE)."
22+
}
23+
}
24+
25+
variable "logging_level" {
26+
type = string
27+
description = "The logging level of the API. One of - OFF, INFO, ERROR"
28+
default = "INFO"
29+
30+
validation {
31+
condition = contains(["OFF", "INFO", "ERROR"], var.logging_level)
32+
error_message = "Valid values for var: logging_level are (OFF, INFO, ERROR)."
33+
}
34+
}
35+
36+
variable "metrics_enabled" {
37+
description = "A flag to indicate whether to enable metrics collection."
38+
type = bool
39+
default = true
40+
}
41+
42+
variable "xray_tracing_enabled" {
43+
description = "A flag to indicate whether to enable X-Ray tracing."
44+
type = bool
45+
default = true
46+
}
47+
48+
# See https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html for additional information
49+
# on how to configure logging.
50+
variable "access_log_format" {
51+
description = "The format of the access log file."
52+
type = string
53+
default = <<EOF
54+
{
55+
"requestTime": "$context.requestTime",
56+
"requestId": "$context.requestId",
57+
"httpMethod": "$context.httpMethod",
58+
"path": "$context.path",
59+
"resourcePath": "$context.resourcePath",
60+
"status": $context.status,
61+
"responseLatency": $context.responseLatency,
62+
"xrayTraceId": "$context.xrayTraceId",
63+
"integrationRequestId": "$context.integration.requestId",
64+
"functionResponseStatus": "$context.integration.status",
65+
"integrationLatency": "$context.integration.latency",
66+
"integrationServiceStatus": "$context.integration.integrationStatus",
67+
"authorizeResultStatus": "$context.authorize.status",
68+
"authorizerServiceStatus": "$context.authorizer.status",
69+
"authorizerLatency": "$context.authorizer.latency",
70+
"authorizerRequestId": "$context.authorizer.requestId",
71+
"ip": "$context.identity.sourceIp",
72+
"userAgent": "$context.identity.userAgent",
73+
"principalId": "$context.authorizer.principalId",
74+
"cognitoUser": "$context.identity.cognitoIdentityId",
75+
"user": "$context.identity.user"
76+
}
77+
EOF
78+
}
79+
80+
# See https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-resource-policies.html for additional
81+
# information on how to configure resource policies.
82+
#
83+
# Example:
84+
# {
85+
# "Version": "2012-10-17",
86+
# "Statement": [
87+
# {
88+
# "Effect": "Allow",
89+
# "Principal": "*",
90+
# "Action": "execute-api:Invoke",
91+
# "Resource": "arn:aws:execute-api:us-east-1:000000000000:*"
92+
# },
93+
# {
94+
# "Effect": "Deny",
95+
# "Principal": "*",
96+
# "Action": "execute-api:Invoke",
97+
# "Resource": "arn:aws:execute-api:region:account-id:*",
98+
# "Condition": {
99+
# "NotIpAddress": {
100+
# "aws:SourceIp": "123.4.5.6/24"
101+
# }
102+
# }
103+
# }
104+
# ]
105+
#}
106+
variable "rest_api_policy" {
107+
description = "The IAM policy document for the API."
108+
type = string
109+
default = null
110+
}
111+
112+
variable "fully_qualified_domain_name" {
113+
description = "The fully qualified domain name of the API."
114+
type = string
115+
default = null
116+
}
117+
118+
variable "enable_private_link_nlb_deletion_protection" {
119+
description = "A flag to indicate whether to enable private link deletion protection."
120+
type = bool
121+
default = false
122+
}
123+
124+
variable "deregistration_delay" {
125+
type = number
126+
default = 15
127+
description = "The amount of time to wait in seconds before changing the state of a deregistering target to unused"
128+
}
129+
130+
variable "enable_private_link_nlb" {
131+
description = "A flag to indicate whether to enable private link."
132+
type = bool
133+
default = false
134+
}

src/versions.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
terraform {
22
required_version = ">= 1.0.0"
33

4-
required_providers {}
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.0"
8+
}
9+
}
510
}

0 commit comments

Comments
 (0)