Skip to content

Commit 97eba3d

Browse files
Working postgress connection
- Security group to handle incoming/outgoing connections to grafana - Grafana now has connection to VPC allowing DB access - To reliably reference data-replication resources new tags have been included
1 parent 7709a8d commit 97eba3d

10 files changed

Lines changed: 122 additions & 24 deletions

File tree

terraform/app/modules/ecs_service/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ resource "aws_security_group" "this" {
1717
}
1818
}
1919

20-
resource "aws_security_group_rule" "egress_all" {
20+
resource "aws_security_group_rule" "egress" {
21+
count = min(length(var.default_egress_cidr_blocks), 1)
2122
type = "egress"
2223
from_port = 0
2324
to_port = 0
2425
protocol = "-1"
25-
cidr_blocks = ["0.0.0.0/0"]
26+
cidr_blocks = var.default_egress_cidr_blocks
2627
security_group_id = aws_security_group.this.id
2728
}
2829

terraform/app/modules/ecs_service/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ variable "server_type_name" {
1717
nullable = true
1818
}
1919

20+
variable "default_egress_cidr_blocks" {
21+
type = list(string)
22+
description = "The default CIDR blocks for egress rules from the service. Defaults to allow all outbound traffic."
23+
default = ["0.0.0.0/0"]
24+
nullable = false
25+
}
26+
2027
variable "minimum_replica_count" {
2128
type = number
2229
description = "Minimum amount of allowed replicas for the service. Also the replica count when creating th service."

terraform/data_replication/ecs.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ module "db_access_service" {
3939
region = var.region
4040
health_check_command = ["CMD-SHELL", "echo 'alive' || exit 1"]
4141
}
42-
depends_on = [aws_rds_cluster_instance.instance]
42+
default_egress_cidr_blocks = var.allowed_egress_cidr_blocks
43+
depends_on = [aws_rds_cluster_instance.instance]
4344
}

terraform/data_replication/network.tf

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ resource "aws_subnet" "subnet_a" {
1111
vpc_id = aws_vpc.vpc.id
1212
cidr_block = "10.0.1.0/24"
1313
availability_zone = "${var.region}a"
14+
tags = {
15+
Private = true
16+
}
1417
}
1518

1619
resource "aws_subnet" "subnet_b" {
1720
vpc_id = aws_vpc.vpc.id
1821
cidr_block = "10.0.2.0/24"
1922
availability_zone = "${var.region}b"
23+
tags = {
24+
Private = true
25+
}
2026
}
2127

2228
resource "aws_route_table" "private" {
@@ -36,45 +42,43 @@ resource "aws_subnet" "public_subnet" {
3642
vpc_id = aws_vpc.vpc.id
3743
cidr_block = "10.0.3.0/24"
3844
availability_zone = "${var.region}a"
45+
tags = {
46+
Private = false
47+
}
3948
}
4049

41-
resource "aws_internet_gateway" "internet_gateway" {
42-
count = local.shared_egress_infrastructure_count
50+
resource "aws_internet_gateway" "this" {
4351
vpc_id = aws_vpc.vpc.id
4452
tags = {
4553
Name = "data-replication-igw-${var.environment}"
4654
}
4755
}
4856

49-
resource "aws_eip" "nat_ip" {
50-
count = local.shared_egress_infrastructure_count
57+
resource "aws_eip" "this" {
5158
domain = "vpc"
52-
depends_on = [aws_internet_gateway.internet_gateway]
59+
depends_on = [aws_internet_gateway.this]
5360
}
5461

55-
resource "aws_nat_gateway" "nat_gateway" {
56-
count = local.shared_egress_infrastructure_count
62+
resource "aws_nat_gateway" "this" {
5763
subnet_id = aws_subnet.public_subnet.id
58-
allocation_id = aws_eip.nat_ip[0].id
64+
allocation_id = aws_eip.this.id
5965
connectivity_type = "public"
60-
depends_on = [aws_internet_gateway.internet_gateway]
66+
depends_on = [aws_internet_gateway.this]
6167
tags = {
6268
Name = "data-replication-nat-gateway-${var.environment}"
6369
}
6470
}
6571

6672
resource "aws_route" "private_to_public" {
67-
count = length(var.allowed_egress_cidr_blocks)
6873
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
74+
nat_gateway_id = aws_nat_gateway.this.id
75+
destination_cidr_block = "0.0.0.0/0"
7176
}
7277

7378
resource "aws_route" "public_to_igw" {
74-
count = length(var.allowed_egress_cidr_blocks)
7579
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
80+
gateway_id = aws_internet_gateway.this.id
81+
destination_cidr_block = "0.0.0.0/0"
7882
}
7983

8084
resource "aws_route_table" "public" {

terraform/data_replication/rds.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ resource "aws_db_subnet_group" "dbsg" {
55

66
resource "aws_security_group" "rds" {
77
vpc_id = aws_vpc.vpc.id
8+
tags = {
9+
Name = "${local.name_prefix}-rds-sg"
10+
}
11+
}
12+
13+
resource "aws_security_group_rule" "ecs_to_grafana" {
14+
type = "egress"
15+
from_port = 5432
16+
to_port = 5432
17+
protocol = "tcp"
18+
security_group_id = module.db_access_service.security_group_id
19+
source_security_group_id = aws_security_group.rds.id
820
}
921

1022
resource "aws_security_group_rule" "rds_inbound" {
@@ -16,6 +28,16 @@ resource "aws_security_group_rule" "rds_inbound" {
1628
source_security_group_id = module.db_access_service.security_group_id
1729
}
1830

31+
resource "aws_security_group_rule" "rds_inbound_grafana" {
32+
type = "ingress"
33+
from_port = 5432
34+
to_port = 5432
35+
protocol = "tcp"
36+
security_group_id = aws_security_group.rds.id
37+
cidr_blocks = [aws_vpc.vpc.cidr_block]
38+
description = "Allow Grafana workspace access to PostgreSQL"
39+
}
40+
1941
resource "aws_rds_cluster" "cluster" {
2042
cluster_identifier = "${local.name_prefix}-rds-${formatdate("hh-mm-ss", timestamp())}"
2143
engine = "aurora-postgresql"

terraform/data_replication/ssm_parameters.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ resource "aws_secretsmanager_secret" "ro_db_password" {
1414
Name = "${local.name_prefix}-ro-db-password"
1515
}
1616
lifecycle {
17-
ignore_changes = [name]
18-
replace_triggered_by = [aws_rds_cluster.cluster]
17+
ignore_changes = [name]
1918
}
2019
}
2120

terraform/data_replication/variables.tf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ 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]
87-
shared_egress_infrastructure_count = min(length(var.allowed_egress_cidr_blocks), 1)
85+
name_prefix = "mavis-${var.environment}-data-replication"
86+
subnet_list = [aws_subnet.subnet_a.id, aws_subnet.subnet_b.id]
8887

8988
task_envs = [
9089
{

terraform/monitoring/aws/variables.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ locals {
3434
AWS-Mavis-ReadOnly = "16b29214-60a1-7008-ff52-0ccd29b7e2d4"
3535
}
3636
}
37-
bucket_name = "nhse-mavis-grafana-${var.environment}"
37+
bucket_name = "nhse-mavis-grafana-${var.environment}"
38+
prefix_environment = var.environment == "development" ? "qa" : var.environment
39+
data_replication_prefix = "mavis-${local.prefix_environment}-data-replication"
3840
}

terraform/monitoring/aws/workspace.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ resource "aws_grafana_workspace" "this" {
1414
}
1515
})
1616
data_sources = ["CLOUDWATCH"]
17+
vpc_configuration {
18+
security_group_ids = [aws_security_group.grafana_workspace.id]
19+
subnet_ids = data.aws_subnets.data_replication.ids
20+
}
1721
}
1822

1923
resource "aws_grafana_role_association" "role" {
@@ -28,3 +32,60 @@ resource "aws_grafana_workspace_service_account" "grafana_provider" {
2832
grafana_role = "ADMIN"
2933
workspace_id = aws_grafana_workspace.this.id
3034
}
35+
36+
locals {
37+
}
38+
39+
resource "aws_security_group" "grafana_workspace" {
40+
name_prefix = "grafana-workspace-${var.environment}"
41+
description = "Security group for Grafana workspace"
42+
vpc_id = data.aws_vpcs.data_replication.ids[0]
43+
tags = {
44+
Name = "grafana-workspace-sg-${var.environment}"
45+
}
46+
}
47+
48+
resource "aws_security_group_rule" "egress_rds" {
49+
type = "egress"
50+
from_port = 5432
51+
to_port = 5432
52+
protocol = "tcp"
53+
security_group_id = aws_security_group.grafana_workspace.id
54+
cidr_blocks = ["10.0.0.0/16"]
55+
}
56+
57+
resource "aws_security_group_rule" "egress_443" {
58+
type = "egress"
59+
from_port = 443
60+
to_port = 443
61+
protocol = "tcp"
62+
security_group_id = aws_security_group.grafana_workspace.id
63+
cidr_blocks = ["0.0.0.0/0"]
64+
}
65+
66+
resource "aws_security_group_rule" "ingress_443" {
67+
type = "ingress"
68+
from_port = 443
69+
to_port = 443
70+
protocol = "tcp"
71+
security_group_id = aws_security_group.grafana_workspace.id
72+
cidr_blocks = ["0.0.0.0/0"]
73+
}
74+
75+
data "aws_vpcs" "data_replication" {
76+
filter {
77+
name = "tag:Environment"
78+
values = [local.data_replication_prefix]
79+
}
80+
}
81+
82+
data "aws_subnets" "data_replication" {
83+
filter {
84+
name = "vpc-id"
85+
values = data.aws_vpcs.data_replication.ids
86+
}
87+
filter {
88+
name = "tag:Private"
89+
values = [true]
90+
}
91+
}

terraform/monitoring/grafana/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ resource "grafana_data_source" "cloudwatch" {
2929
})
3030
uid = "cloudwatch"
3131
}
32+
33+
# resouce "grafana_data_source" "postgres" {}

0 commit comments

Comments
 (0)