Skip to content

Commit

Permalink
feat: Support custom domain name
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Hammond committed Jun 11, 2021
1 parent 550c029 commit 7f8db2c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
29 changes: 25 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
locals {
resource_name_prefix = "${var.namespace}-${var.resource_tag_name}"
api_url = "${aws_api_gateway_deployment._.invoke_url}${aws_api_gateway_stage._.stage_name}"
api_name = "${local.resource_name_prefix}-${var.api_name}"
}

Expand All @@ -10,6 +9,10 @@ data "template_file" "_" {
vars = var.api_template_vars
}

data "aws_api_gateway_domain_name" "_" {
domain_name = var.api_domain_name
}

resource "aws_api_gateway_rest_api" "_" {
name = local.api_name
api_key_source = "HEADER"
Expand All @@ -24,8 +27,8 @@ resource "aws_api_gateway_deployment" "_" {
lifecycle {
create_before_destroy = true
}
# Triggers a re-deployment to the stage

# Triggers a re-deployment to the stage
triggers = {
redeployment = base64sha256(data.template_file._.template)
}
Expand Down Expand Up @@ -58,8 +61,26 @@ resource "aws_api_gateway_method_settings" "_" {
}
}

#
# Domain Setup
#
resource "aws_api_gateway_domain_name" "_" {
domain_name = var.api_domain_name
endpoint_configuration {
types = ["REGIONAL"]
}
regional_certificate_arn = var.acm_certificate_arn
security_policy = "TLS_1_2"
}

resource "aws_api_gateway_base_path_mapping" "_" {
api_id = aws_api_gateway_rest_api._.id
domain_name = aws_api_gateway_domain_name._.domain_name
stage_name = aws_api_gateway_stage._.stage_name
}

# -----------------------------------------------------------------------------
# CloudWatch: API Gateway
# CloudWatch: API Gateway
# -----------------------------------------------------------------------------
module "cloudwatch_alarms_apigateway" {
source = "./cloudwatch-alarms-apigateway"
Expand Down
8 changes: 6 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ output "rest_api_id" {
}

output "api_url" {
value = local.api_url
value = aws_api_gateway_deployment._.invoke_url
}

output "api_domain_name" {
value = data.aws_api_gateway_domain_name._.regional_domain_name
}

output "api_name" {
Expand All @@ -16,4 +20,4 @@ output "api_name" {

output "api_stage" {
value = aws_api_gateway_stage._.stage_name
}
}
18 changes: 15 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ variable "namespace" {

variable "region" {
description = "AWS region"
type = string
default = "us-east-1"
}

variable "resource_tag_name" {
Expand All @@ -27,7 +29,17 @@ variable "api_template" {

variable "api_template_vars" {
description = "Variables required in the OpenAPI template file"
type = map
type = map(string)
}

variable "api_domain_name" {
description = "Domain name of the API Gateway REST API for self-signed TLS certificate"
type = string
}

variable "acm_certificate_arn" {
description = "The ARN of the ACM certificate to use for the custom domain name"
type = string
}

variable "api_throttling_rate_limit" {
Expand Down Expand Up @@ -67,7 +79,7 @@ variable "xray_tracing_enabled" {
# -----------------------------------------------------------------------------
variable "resources" {
description = "Methods that have Cloudwatch alarms enabled"
type = map
type = map(string)
}

variable "latency_threshold_p95" {
Expand Down Expand Up @@ -113,4 +125,4 @@ variable "fiveRate_evaluationPeriods" {
description = "How many periods are evaluated before the alarm is triggered"
default = 5
type = number
}
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

terraform {
required_version = ">= 0.12"
required_version = ">= 0.14"
}

0 comments on commit 7f8db2c

Please sign in to comment.