Skip to content

Commit 7a4076c

Browse files
committed
implementation of Application Load Balancer that can be extended to include SSL from AWS ACM
1 parent f1b9853 commit 7a4076c

8 files changed

+123
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.terraform/
2+
.terraform.lock.hcl
3+
.terraform.tfstate.lock.info
24
terraform.tfstate.backup

api_ssl_endpoint.tf

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* This file sets up all resources necessary to accept inbound
3+
* HTTPS requests to the DLE API and direct them to the DLE
4+
* instance itself.
5+
*
6+
* It uses an AWS Application Load Balancer to terminate SSL
7+
* requests and then forward HTTP traffic to the DLE instance.
8+
*/
9+
10+
# Create the Load Balancer for only the main DLE API
11+
resource "aws_lb" "dle_api_lb" {
12+
name = "dle-api-lb"
13+
load_balancer_type = "application"
14+
security_groups = [aws_security_group.dle_api_sg.id]
15+
16+
subnets = data.aws_subnet_ids.dle_vpc_subnets.ids
17+
tags = "${local.common_tags}"
18+
}
19+
20+
# Setup an HTTPS listener that will terminate SSL
21+
resource "aws_lb_listener" "dle_api_ssl_listener" {
22+
load_balancer_arn = aws_lb.dle_api_lb.arn
23+
24+
port = 80
25+
protocol = "HTTP"
26+
27+
# FIXME -- Need to have Domain and Certificate in ACM
28+
# port = 443
29+
# protocol = "HTTPS"
30+
# ssl_policy = "ELBSecurityPolicy-2016-08"
31+
# certificate_arn = aws_acm_certificate.this.arn
32+
33+
default_action {
34+
type = "forward"
35+
target_group_arn = aws_lb_target_group.api_target_group.arn
36+
}
37+
38+
tags = "${local.common_tags}"
39+
}
40+
41+
# The target group defines how the Load Balancer should
42+
# forward incoming requests and also sets up a health
43+
# check on the target instance.
44+
resource "aws_lb_target_group" "api_target_group" {
45+
port = 80
46+
protocol = "HTTP"
47+
vpc_id = aws_default_vpc.dle_vpc.id
48+
49+
health_check {
50+
path = "/healthz"
51+
healthy_threshold = 2
52+
interval = 30
53+
protocol = "HTTP"
54+
unhealthy_threshold = 2
55+
}
56+
57+
depends_on = [
58+
aws_lb.dle_api_lb
59+
]
60+
61+
lifecycle {
62+
create_before_destroy = true
63+
}
64+
65+
tags = "${local.common_tags}"
66+
}
67+
68+
# This makes the connection between the Target Group and the
69+
# actual DLE EC2 Instance itself.
70+
resource "aws_lb_target_group_attachment" "dle_instance_attachment" {
71+
target_group_arn = aws_lb_target_group.api_target_group.arn
72+
target_id = aws_instance.aws_ec2.id
73+
port = 80
74+
}

instance.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_instance" "aws_ec2" {
22
ami = "${data.aws_ami.ami.id}"
33
instance_type = "${var.instance_type}"
4-
security_groups = ["${aws_security_group.sg.name}"]
4+
security_groups = ["${aws_security_group.dle_instance_sg.name}"]
55
key_name = "${var.keypair}"
66
tags = "${local.common_tags}"
77
}

main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ terraform {
1111
provider "aws" {
1212
region = "${var.aws_region}"
1313
}
14+
1415
locals {
1516
common_tags = {
1617
Name = "${var.tag_name}"

security.tf

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
resource "aws_security_group" "sg" {
1+
resource "aws_security_group" "dle_instance_sg" {
22
ingress {
33
cidr_blocks = "${var.allow_ssh_from_cidrs}"
44

@@ -16,3 +16,22 @@ resource "aws_security_group" "sg" {
1616

1717
tags = "${local.common_tags}"
1818
}
19+
20+
resource "aws_security_group" "dle_api_sg" {
21+
ingress {
22+
cidr_blocks = "${var.allow_api_from_cidrs}"
23+
24+
from_port = 443
25+
to_port = 443
26+
protocol = "tcp"
27+
}
28+
29+
egress {
30+
from_port = 0
31+
to_port = 0
32+
protocol = "-1"
33+
cidr_blocks = ["0.0.0.0/0"]
34+
}
35+
36+
tags = "${local.common_tags}"
37+
}

terraform.tfstate

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": 4,
3-
"terraform_version": "0.13.2",
4-
"serial": 14,
3+
"terraform_version": "0.15.3",
4+
"serial": 49,
55
"lineage": "60a04a52-2c24-ec3b-9ab1-dd2c08731279",
66
"outputs": {},
77
"resources": []

variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ variable "allow_ssh_from_cidrs" {
2727
default = ["0.0.0.0/0"]
2828
}
2929

30+
variable "allow_api_from_cidrs" {
31+
description = "List of CIDRs allowed to connect to API"
32+
default = ["0.0.0.0/0"]
33+
}
34+
3035
variable "tag_name" {
3136
description = "Value of the tags Name to apply to all resources"
3237
default = "DBLABserver"

vpc.tf

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Establish & Configure the AWS VPC that the DLE
3+
* and other Postgres.ai products will reside within
4+
*/
5+
6+
# Note that for now, we are simply adopting the default
7+
# VPC. Future work should establish independent and dedicated
8+
# VPC for Database Lab Engine.
9+
10+
resource "aws_default_vpc" "dle_vpc" {
11+
tags = {
12+
Name = "Default VPC"
13+
}
14+
}
15+
16+
data "aws_subnet_ids" "dle_vpc_subnets" {
17+
vpc_id = aws_default_vpc.dle_vpc.id
18+
}

0 commit comments

Comments
 (0)