Skip to content

Commit 6bf5adc

Browse files
authored
Merge pull request #3633 from nhsuk/allow_outgoing_connections
Enable PDS lookups from data-replication stack
2 parents 710d092 + a3c7f14 commit 6bf5adc

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

.github/workflows/data-replication-pipeline.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ on:
3131
description: ARN of the DB snapshot to use (optional)
3232
required: false
3333
type: string
34+
egress_cidr:
35+
description: CIDR blocks to allow egress traffic.
36+
type: string
37+
required: true
38+
default: "[]"
3439

3540
env:
3641
aws_role: ${{ inputs.environment == 'production'
@@ -193,6 +198,7 @@ jobs:
193198
terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade
194199
terraform plan -var="image_digest=${{ env.DOCKER_DIGEST }}" -var="db_secret_arn=${{ env.DB_SECRET_ARN }}" \
195200
-var="imported_snapshot=${{ env.SNAPSHOT_ARN }}" -var-file="env/${{ inputs.environment }}.tfvars" \
201+
-var='allowed_egress_cidr_blocks=${{ inputs.egress_cidr }}' \
196202
-out ${{ runner.temp }}/tfplan | tee ${{ runner.temp }}/tf_stdout
197203
- name: Upload artifact
198204
uses: actions/upload-artifact@v4

terraform/data_replication/network.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ resource "aws_vpc" "vpc" {
22
cidr_block = "10.0.0.0/16"
33
enable_dns_hostnames = true
44
enable_dns_support = true
5+
tags = {
6+
Name = "data-replication-vpc-${var.environment}"
7+
}
58
}
69

710
resource "aws_subnet" "subnet_a" {
@@ -18,6 +21,9 @@ resource "aws_subnet" "subnet_b" {
1821

1922
resource "aws_route_table" "private" {
2023
vpc_id = aws_vpc.vpc.id
24+
tags = {
25+
Name = "data-replication-private-rt-${var.environment}"
26+
}
2127
}
2228

2329
resource "aws_route_table_association" "private" {
@@ -26,6 +32,63 @@ resource "aws_route_table_association" "private" {
2632
subnet_id = local.subnet_list[count.index]
2733
}
2834

35+
resource "aws_subnet" "public_subnet" {
36+
vpc_id = aws_vpc.vpc.id
37+
cidr_block = "10.0.3.0/24"
38+
availability_zone = "${var.region}a"
39+
}
40+
41+
resource "aws_internet_gateway" "internet_gateway" {
42+
count = local.shared_egress_infrastructure_count
43+
vpc_id = aws_vpc.vpc.id
44+
tags = {
45+
Name = "data-replication-igw-${var.environment}"
46+
}
47+
}
48+
49+
resource "aws_eip" "nat_ip" {
50+
count = local.shared_egress_infrastructure_count
51+
domain = "vpc"
52+
depends_on = [aws_internet_gateway.internet_gateway]
53+
}
54+
55+
resource "aws_nat_gateway" "nat_gateway" {
56+
count = local.shared_egress_infrastructure_count
57+
subnet_id = aws_subnet.public_subnet.id
58+
allocation_id = aws_eip.nat_ip[0].id
59+
connectivity_type = "public"
60+
depends_on = [aws_internet_gateway.internet_gateway]
61+
tags = {
62+
Name = "data-replication-nat-gateway-${var.environment}"
63+
}
64+
}
65+
66+
resource "aws_route" "private_to_public" {
67+
count = length(var.allowed_egress_cidr_blocks)
68+
route_table_id = aws_route_table.private.id
69+
destination_cidr_block = var.allowed_egress_cidr_blocks[count.index]
70+
nat_gateway_id = aws_nat_gateway.nat_gateway[0].id
71+
}
72+
73+
resource "aws_route" "public_to_igw" {
74+
count = length(var.allowed_egress_cidr_blocks)
75+
route_table_id = aws_route_table.public.id
76+
destination_cidr_block = var.allowed_egress_cidr_blocks[count.index]
77+
gateway_id = aws_internet_gateway.internet_gateway[0].id
78+
}
79+
80+
resource "aws_route_table" "public" {
81+
vpc_id = aws_vpc.vpc.id
82+
tags = {
83+
Name = "data-replication-public-rt-${var.environment}"
84+
}
85+
}
86+
87+
resource "aws_route_table_association" "public" {
88+
route_table_id = aws_route_table.public.id
89+
subnet_id = aws_subnet.public_subnet.id
90+
}
91+
2992
locals {
3093
vpc_endpoints = tomap(
3194
{

terraform/data_replication/variables.tf

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ variable "rails_master_key_path" {
8282
}
8383

8484
locals {
85-
name_prefix = "mavis-${var.environment}-data-replication"
86-
subnet_list = [aws_subnet.subnet_a.id, aws_subnet.subnet_b.id]
85+
name_prefix = "mavis-${var.environment}-data-replication"
86+
subnet_list = [aws_subnet.subnet_a.id, aws_subnet.subnet_b.id]
87+
shared_egress_infrastructure_count = min(length(var.allowed_egress_cidr_blocks), 1)
88+
8789
task_envs = [
8890
{
8991
name = "DB_HOST"
@@ -125,3 +127,9 @@ locals {
125127
}
126128
]
127129
}
130+
131+
variable "allowed_egress_cidr_blocks" {
132+
type = list(string)
133+
description = "CIDR blocks for the allowed outbound traffic from the data replication service."
134+
default = []
135+
}

0 commit comments

Comments
 (0)