Skip to content

Commit 387342e

Browse files
committed
NRL-1385 Make VPC private
1 parent 1f73f2c commit 387342e

File tree

10 files changed

+120
-21
lines changed

10 files changed

+120
-21
lines changed
Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
module "vpc" {
2-
source = "../modules/vpc"
3-
vpc_cidr_block = var.vpc_cidr_block
4-
enable_dns_hostnames = var.enable_dns_hostnames
5-
vpc_public_subnets_cidr_block = var.vpc_public_subnets_cidr_block
6-
aws_azs = var.aws_azs
7-
name_prefix = "nhsd-nrlf--dev"
2+
source = "../modules/vpc"
3+
vpc_cidr_block = var.vpc_cidr_block
4+
enable_dns_hostnames = var.enable_dns_hostnames
5+
vpc_public_subnets_cidr_block = var.vpc_public_subnets_cidr_block
6+
vpc_private_subnets_cidr_block = var.vpc_private_subnets_cidr_block
7+
aws_azs = var.aws_azs
8+
name_prefix = "nhsd-nrlf--dev"
89
}
910

10-
1111
module "ec2" {
1212
source = "../modules/ec2"
13+
use_custom_ami = true
1314
instance_type = var.instance_type
1415
name_prefix = "nhsd-nrlf--dev"
1516
target_bucket_arn = module.dev-glue.target_bucket_arn
@@ -21,3 +22,31 @@ module "ec2" {
2122
subnet_id = module.vpc.subnet_id
2223
security_groups = module.vpc.security_group
2324
}
25+
26+
module "powerbi_gw_instance" {
27+
source = "../modules/ec2"
28+
use_custom_ami = true
29+
instance_type = var.instance_type
30+
name_prefix = "nhsd-nrlf--dev-powerbi-gw"
31+
target_bucket_arn = module.dev-glue.target_bucket_arn
32+
glue_kms_key_arn = module.dev-glue.aws_kms_key_arn
33+
athena_kms_key_arn = module.dev-athena.kms_key_arn
34+
athena_bucket_arn = module.dev-athena.bucket_arn
35+
36+
subnet_id = module.vpc.private_subnet_id
37+
security_groups = [module.vpc.powerbi_gw_security_group_id]
38+
}
39+
40+
module "powerbi_gw_instance_v2" {
41+
source = "../modules/ec2"
42+
use_custom_ami = false
43+
instance_type = var.instance_type
44+
name_prefix = "nhsd-nrlf--dev-powerbi-gw-v2"
45+
target_bucket_arn = module.dev-glue.target_bucket_arn
46+
glue_kms_key_arn = module.dev-glue.aws_kms_key_arn
47+
athena_kms_key_arn = module.dev-athena.kms_key_arn
48+
athena_bucket_arn = module.dev-athena.bucket_arn
49+
50+
subnet_id = module.vpc.private_subnet_id
51+
security_groups = [module.vpc.powerbi_gw_security_group_id]
52+
}

terraform/account-wide-infrastructure/dev/vars.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,20 @@ variable "vpc_public_subnets_cidr_block" {
3838
default = "10.0.0.0/24"
3939
}
4040

41+
variable "vpc_private_subnets_cidr_block" {
42+
type = string
43+
description = "CIDR Block for Private Subnets in VPC"
44+
default = "10.0.1.0/24"
45+
}
46+
4147
variable "instance_type" {
4248
type = string
4349
description = "Type for EC2 Instance"
4450
default = "t2.micro"
4551
}
52+
53+
variable "use_custom_ami" {
54+
type = bool
55+
description = "Use custom image"
56+
default = false
57+
}

terraform/account-wide-infrastructure/modules/ec2/ec2.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_instance" "web" {
22
# associate_public_ip_address =
33
iam_instance_profile = aws_iam_instance_profile.powerbi_profile.name
4-
ami = data.aws_ami.PowerBI_Gateway.id
4+
ami = local.selected_ami_id
55
instance_type = var.instance_type
66
key_name = aws_key_pair.ec2_key_pair.key_name
77
subnet_id = var.subnet_id
@@ -15,18 +15,16 @@ resource "aws_instance" "web" {
1515

1616
}
1717

18-
# Key pair for RDP access
1918
resource "tls_private_key" "instance_key_pair" {
2019
algorithm = "RSA"
2120
}
2221

2322
resource "aws_key_pair" "ec2_key_pair" {
24-
key_name = "PowerBI-GateWay-Key"
23+
key_name = "${var.name_prefix}_PowerBI-GateWay-Key"
2524
public_key = tls_private_key.instance_key_pair.public_key_openssh
2625
}
2726

28-
# Saving Key Pair for ssh login for Client if needed
29-
resource "local_file" "ssh_key" {
27+
resource "local_file" "ssh_key_priv" {
3028
filename = "${path.module}/keys/${aws_key_pair.ec2_key_pair.key_name}.pem"
3129
content = tls_private_key.instance_key_pair.private_key_pem
3230
}

terraform/account-wide-infrastructure/modules/ec2/iam.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,17 @@ resource "aws_iam_policy" "ec2_service" {
9494
policy = data.aws_iam_policy_document.ec2_service.json
9595
}
9696

97-
resource "aws_iam_role_policy_attachment" "ec2_service" {
97+
resource "aws_iam_role_policy_attachment" "ec2_role_policy" {
9898
role = aws_iam_role.ec2_service_role.name
9999
policy_arn = aws_iam_policy.ec2_service.arn
100100
}
101101

102+
resource "aws_iam_role_policy_attachment" "ec2_role_policy_ssm" {
103+
role = aws_iam_role.ec2_service_role.name
104+
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
105+
}
106+
102107
resource "aws_iam_instance_profile" "powerbi_profile" {
103-
name = "powerbi_profile"
108+
name = "${var.name_prefix}-powerbi_instance_profile"
104109
role = aws_iam_role.ec2_service_role.name
105110
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
selected_ami_id = var.use_custom_ami ? data.aws_ami.PowerBI_Gateway.id : data.aws_ami.windows-2019.id
3+
}

terraform/account-wide-infrastructure/modules/ec2/scripts/user_data.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22

33
<powershell>
4+
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1
5+
46
Install-WindowsFeature -name Web-Server -IncludeManagementTools
57

68
$instanceId = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id -UseBasicParsing).content

terraform/account-wide-infrastructure/modules/ec2/vars.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ variable "glue_kms_key_arn" {}
66
variable "athena_kms_key_arn" {}
77
variable "target_bucket_arn" {}
88
variable "athena_bucket_arn" {}
9+
variable "use_custom_ami" {}

terraform/account-wide-infrastructure/modules/vpc/outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ output "subnet_id" {
22
value = aws_subnet.public_subnet.id
33
}
44

5+
output "private_subnet_id" {
6+
value = aws_subnet.private_subnet.id
7+
}
8+
59
output "security_group" {
610
value = [aws_security_group.sg.id]
711
}
12+
13+
output "powerbi_gw_security_group_id" {
14+
value = aws_security_group.powerbi_gw_sg.id
15+
}

terraform/account-wide-infrastructure/modules/vpc/vars.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ variable "aws_azs" {}
22
variable "enable_dns_hostnames" {}
33
variable "vpc_cidr_block" {}
44
variable "vpc_public_subnets_cidr_block" {}
5+
variable "vpc_private_subnets_cidr_block" {}
56
variable "name_prefix" {}

terraform/account-wide-infrastructure/modules/vpc/vpc.tf

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Create the VPC
21
resource "aws_vpc" "app_vpc" {
32
cidr_block = var.vpc_cidr_block
43
enable_dns_hostnames = var.enable_dns_hostnames
@@ -8,7 +7,6 @@ resource "aws_vpc" "app_vpc" {
87
}
98
}
109

11-
# Create the internet gateway
1210
resource "aws_internet_gateway" "igw" {
1311
vpc_id = aws_vpc.app_vpc.id
1412

@@ -17,7 +15,6 @@ resource "aws_internet_gateway" "igw" {
1715
}
1816
}
1917

20-
# Create the public subnet
2118
resource "aws_subnet" "public_subnet" {
2219
vpc_id = aws_vpc.app_vpc.id
2320
cidr_block = var.vpc_public_subnets_cidr_block
@@ -27,29 +24,59 @@ resource "aws_subnet" "public_subnet" {
2724
tags = {
2825
Name = "${var.name_prefix}-pubsubnet"
2926
}
27+
}
28+
29+
resource "aws_subnet" "private_subnet" {
30+
vpc_id = aws_vpc.app_vpc.id
31+
cidr_block = var.vpc_private_subnets_cidr_block
32+
availability_zone = var.aws_azs
3033

34+
tags = {
35+
Name = "${var.name_prefix}-privsubnet"
36+
}
3137
}
3238

33-
# Create the route table
3439
resource "aws_route_table" "public_rt" {
3540
vpc_id = aws_vpc.app_vpc.id
3641

3742
route {
3843
cidr_block = "0.0.0.0/0"
3944
gateway_id = aws_internet_gateway.igw.id
4045
}
46+
}
47+
48+
resource "aws_route_table" "private_rt" {
49+
vpc_id = aws_vpc.app_vpc.id
50+
51+
route {
52+
cidr_block = "0.0.0.0/0"
53+
nat_gateway_id = aws_nat_gateway.nat.id
54+
}
55+
}
56+
57+
resource "aws_eip" "natgw-ip" {
58+
domain = "vpc"
59+
}
60+
61+
resource "aws_nat_gateway" "nat" {
62+
allocation_id = aws_eip.natgw-ip.id
63+
subnet_id = aws_subnet.public_subnet.id
4164

65+
tags = {
66+
Name = "${var.name_prefix}-nat"
67+
}
4268
}
4369

44-
# Assign the public route table to the public subnet
4570
resource "aws_route_table_association" "public_rt_asso" {
4671
subnet_id = aws_subnet.public_subnet.id
4772
route_table_id = aws_route_table.public_rt.id
4873
}
4974

75+
resource "aws_route_table_association" "private_rt_asso" {
76+
subnet_id = aws_subnet.private_subnet.id
77+
route_table_id = aws_route_table.private_rt.id
78+
}
5079

51-
52-
# Create the security group
5380
resource "aws_security_group" "sg" {
5481
name = "allow_ssh_http"
5582
description = "Allow ssh http inbound traffic"
@@ -79,5 +106,18 @@ resource "aws_security_group" "sg" {
79106
cidr_blocks = ["0.0.0.0/0"]
80107
ipv6_cidr_blocks = ["::/0"]
81108
}
109+
}
110+
111+
resource "aws_security_group" "powerbi_gw_sg" {
112+
name = "powerbi-gw-sg"
113+
description = "Only allow egress traffic"
114+
vpc_id = aws_vpc.app_vpc.id
82115

116+
egress {
117+
from_port = 0
118+
to_port = 0
119+
protocol = "-1"
120+
cidr_blocks = ["0.0.0.0/0"]
121+
ipv6_cidr_blocks = ["::/0"]
122+
}
83123
}

0 commit comments

Comments
 (0)