Skip to content

Commit 667aabc

Browse files
committed
Add option to support custom routes and integrations
1 parent 0a455b7 commit 667aabc

File tree

9 files changed

+144
-3
lines changed

9 files changed

+144
-3
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
examples/*/.terraform*
2+
examples/*/terraform.tfstate

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ resources before the certificate is ready. Re-applying after the initial
2828
failure should fix the problem.
2929

3030
The module will create a proxy gateway at the requested location, with a
31-
valid TLS certificate. Logs are streamed to CloudWatch.
31+
valid TLS certificate. Logs are streamed to CloudWatch.
32+
33+
## Custom Integration
34+
35+
To integrate with something other than an HTTP service, pass the variable
36+
`auto_create_route=false` and omit `target_url`. Then you can define your
37+
own `aws_apigatewayv2_route` and `aws_apigatewayv2_integration` objects.
38+
To attach them to the API Gateway, use the module's `api_id` output.

examples/proxy_to_alb/main.tf

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module "proxy" {
2+
source = "../.."
3+
api_display_name = "example-alb-proxy"
4+
api_description = "Transparent proxy to a simple AWS Application Load Balancer"
5+
api_mapping_key = ""
6+
full_domain_name = var.domain_name
7+
hosted_zone_id = var.route53_hosted_zone_id
8+
auto_create_route = false
9+
}
10+
11+
resource "aws_apigatewayv2_route" "custom_route" {
12+
api_id = module.proxy.api_id
13+
route_key = "$default"
14+
target = format("integrations/%s", aws_apigatewayv2_integration.custom_integration.id)
15+
}
16+
17+
resource "aws_apigatewayv2_integration" "custom_integration" {
18+
api_id = module.proxy.api_id
19+
integration_type = "HTTP_PROXY"
20+
integration_method = "ANY"
21+
integration_uri = aws_lb_listener.hello.arn
22+
connection_type = "VPC_LINK"
23+
connection_id = aws_apigatewayv2_vpc_link.target.id
24+
}

examples/proxy_to_alb/target.tf

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
resource "aws_lb" "target" {
2+
name = "test-lb"
3+
internal = true
4+
load_balancer_type = "application"
5+
subnets = [ for s in aws_subnet.subnet : s.id ]
6+
security_groups = [ aws_security_group.target.id ]
7+
}
8+
9+
resource "aws_lb_listener" "hello" {
10+
load_balancer_arn = aws_lb.target.arn
11+
port = "80"
12+
protocol = "HTTP"
13+
14+
default_action {
15+
type = "fixed-response"
16+
17+
fixed_response {
18+
content_type = "text/plain"
19+
message_body = "Hello, World!"
20+
status_code = "200"
21+
}
22+
}
23+
}
24+
25+
resource "aws_security_group" "target" {
26+
name = "test-load-balancer"
27+
vpc_id = aws_default_vpc.vpc.id
28+
}
29+
30+
resource "aws_vpc_security_group_ingress_rule" "ingress_to_target" {
31+
security_group_id = aws_security_group.target.id
32+
ip_protocol = "tcp"
33+
cidr_ipv4 = "0.0.0.0/0"
34+
from_port = 80
35+
to_port = 80
36+
}

examples/proxy_to_alb/variables.tf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable domain_name {
2+
type = string
3+
description = "Domain name where the proxy should be hosted"
4+
}
5+
6+
variable route53_hosted_zone_id {
7+
type = string
8+
description = "Your Route53 zone"
9+
}

examples/proxy_to_alb/vpc.tf

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
resource "aws_default_vpc" "vpc" {}
2+
3+
data "aws_availability_zones" "available" {
4+
state = "available"
5+
}
6+
7+
resource "aws_route_table" "private" {
8+
vpc_id = aws_default_vpc.vpc.id
9+
route = []
10+
}
11+
12+
resource "aws_subnet" "subnet" {
13+
for_each = { for i, name in data.aws_availability_zones.available.names: i => name }
14+
vpc_id = aws_default_vpc.vpc.id
15+
availability_zone = each.value
16+
cidr_block = cidrsubnet(aws_default_vpc.vpc.cidr_block, 4, 8 + each.key)
17+
map_public_ip_on_launch = false
18+
}
19+
20+
resource "aws_route_table_association" "private" {
21+
for_each = aws_subnet.subnet
22+
subnet_id = each.value.id
23+
route_table_id = aws_route_table.private.id
24+
}
25+
26+
resource "aws_apigatewayv2_vpc_link" "target" {
27+
name = "test-vpc-link"
28+
security_group_ids = [ aws_security_group.vpc_link.id ]
29+
subnet_ids = [ for s in aws_subnet.subnet : s.id ]
30+
}
31+
32+
resource "aws_security_group" "vpc_link" {
33+
name = "test-vpc-link"
34+
vpc_id = aws_default_vpc.vpc.id
35+
}
36+
37+
resource "aws_vpc_security_group_egress_rule" "vpc_link_to_load_balancer" {
38+
security_group_id = aws_security_group.vpc_link.id
39+
ip_protocol = "tcp"
40+
from_port = 80
41+
to_port = 80
42+
referenced_security_group_id = aws_security_group.target.id
43+
}
44+
45+
resource "aws_vpc_security_group_ingress_rule" "vpc_link_from_internet" {
46+
security_group_id = aws_security_group.vpc_link.id
47+
ip_protocol = "tcp"
48+
from_port = 80
49+
to_port = 80
50+
cidr_ipv4 = "0.0.0.0/0"
51+
}

main.tf

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ resource "aws_apigatewayv2_deployment" "proxy" {
6464
}
6565

6666
resource "aws_apigatewayv2_route" "proxy" {
67+
count = var.auto_create_route ? 1 : 0
6768
api_id = aws_apigatewayv2_api.proxy.id
6869
route_key = "$default"
69-
target = format("integrations/%s", aws_apigatewayv2_integration.proxy.id)
70+
target = format("integrations/%s", var.auto_create_route ? aws_apigatewayv2_integration.proxy[0].id : "")
7071
}
7172

7273
resource "aws_apigatewayv2_integration" "proxy" {
74+
count = var.auto_create_route ? 1 : 0
7375
api_id = aws_apigatewayv2_api.proxy.id
7476
integration_type = "HTTP_PROXY"
7577
integration_method = "ANY"

outputs.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "api_id" {
2+
value = aws_apigatewayv2_api.proxy.id
3+
}

variables.tf

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ variable "hosted_zone_id" {
2323
description = "ID of the Route53 Hosted Zone that routes to the API Gateway"
2424
}
2525

26+
variable "auto_create_route" {
27+
type = bool
28+
description = "Automatically create the proxy route and integration"
29+
default = true
30+
}
31+
2632
variable "target_url" {
2733
type = string
28-
description = "URL of the site to proxy"
34+
description = "URL of the site to proxy. Required if auto_create_route = true; ignored otherwise"
35+
default = ""
2936
}

0 commit comments

Comments
 (0)