Skip to content

Commit

Permalink
Optional VPC: Allow use of existing VPC and subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Dec 11, 2023
1 parent ebc7640 commit b01aab4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
18 changes: 11 additions & 7 deletions ecs-cluster/keycloak.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ data "aws_caller_identity" "current" {}
locals {
container-port = 8443
keycloak-hostname = var.keycloak-hostname == "" ? aws_lb.keycloak.dns_name : var.keycloak-hostname

vpc_id = var.vpc-id == "" ? module.vpc[0].vpc_id : var.vpc-id
public_subnets = var.public-subnets == [] ? module.vpc[0].public_subnets : var.public-subnets
private_subnets = var.private-subnets == [] ? module.vpc[0].private_subnets : var.private-subnets
}

resource "random_password" "db-password" {
Expand All @@ -18,7 +22,7 @@ resource "random_string" "initial-keycloak-password" {

resource "aws_security_group" "rds" {
name = "${var.name}-sg-rds"
vpc_id = module.vpc.vpc_id
vpc_id = local.vpc_id

ingress {
from_port = 5432
Expand All @@ -30,7 +34,7 @@ resource "aws_security_group" "rds" {

resource "aws_security_group" "alb" {
name = "${var.name}-sg-alb"
vpc_id = module.vpc.vpc_id
vpc_id = local.vpc_id

ingress {
protocol = "tcp"
Expand All @@ -56,7 +60,7 @@ resource "aws_security_group" "alb" {

resource "aws_security_group" "ecs-task-keycloak" {
name = "${var.name}-sg-task-keycloak"
vpc_id = module.vpc.vpc_id
vpc_id = local.vpc_id

ingress {
protocol = "tcp"
Expand All @@ -80,7 +84,7 @@ resource "aws_lb" "keycloak" {
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb.id]
subnets = module.vpc.public_subnets
subnets = local.public_subnets

enable_deletion_protection = true

Expand All @@ -91,7 +95,7 @@ resource "aws_alb_target_group" "keycloak" {
name = "${var.name}-tg"
port = 443
protocol = "HTTPS"
vpc_id = module.vpc.vpc_id
vpc_id = local.vpc_id
target_type = "ip"

health_check {
Expand Down Expand Up @@ -172,7 +176,7 @@ resource "aws_db_parameter_group" "keycloak" {

resource "aws_db_subnet_group" "keycloak" {
name = "${var.name}-keycloak"
subnet_ids = module.vpc.private_subnets
subnet_ids = local.private_subnets
}

resource "aws_db_instance" "keycloak" {
Expand Down Expand Up @@ -352,7 +356,7 @@ resource "aws_ecs_service" "keycloak" {
aws_security_group.rds.id,
aws_security_group.ecs-task-keycloak.id
]
subnets = module.vpc.private_subnets
subnets = local.private_subnets
# TODO: Setting this to False means the image can't be pulled. Why? It works in K8s.
# assign_public_ip = true
}
Expand Down
18 changes: 18 additions & 0 deletions ecs-cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ variable "lb-cidr-blocks-in" {
description = "CIDR blocks to allow access to the load balancer"
}

variable "vpc-id" {
type = string
default = ""
description = "VPC ID, if empty creates a new VPC"
}

variable "public-subnets" {
type = list(string)
default = []
description = "Public subnet IDs, must be defined if vpc-id is provided"
}

variable "private-subnets" {
type = list(string)
default = []
description = "Private subnet IDs, must be defined if vpc-id is provided"
}

variable "db-name" {
type = string
default = "keycloak"
Expand Down
8 changes: 8 additions & 0 deletions ecs-cluster/vpc.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ data "aws_availability_zones" "available" {}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.2.0"
count = var.vpc-id == "" ? 1 : 0

name = "${var.name}-vpc"
cidr = "10.199.0.0/16"
Expand All @@ -18,3 +19,10 @@ module "vpc" {
manage_default_network_acl = false
map_public_ip_on_launch = true
}

# Backwards compatibility with existing deployments
# https://developer.hashicorp.com/terraform/language/modules/develop/refactoring#enabling-count-or-for_each-for-a-resource
moved {
from = module.vpc
to = module.vpc[0]
}

0 comments on commit b01aab4

Please sign in to comment.