Skip to content

Commit ae07d8c

Browse files
authored
Merge pull request #22 from optiop/feature/private-subnet
Use private subnet for containers
2 parents 9ba05a8 + 87f7018 commit ae07d8c

File tree

9 files changed

+33
-8
lines changed

9 files changed

+33
-8
lines changed

ops/ecs/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module "cluster" {
88
name = "ecs-cluster"
99
vpc_security_group_id = module.vpc.security_group_id
1010
vpc_public_subnets = module.vpc.public_subnets
11+
vpc_private_subnets = module.vpc.private_subnets
1112

1213
depends_on = [module.vpc]
1314
}
@@ -18,6 +19,7 @@ module "grafana" {
1819
cluster_id = module.cluster.cluster_id
1920
vpc_id = module.vpc.vpc_id
2021
vpc_public_subnets = module.vpc.public_subnets
22+
vpc_private_subnets = module.vpc.private_subnets
2123
security_group_id = module.vpc.security_group_id
2224
namespace_id = module.vpc.namespace_id
2325
secret_manager_name = var.secret_manager_name
@@ -30,7 +32,7 @@ module "postgres" {
3032
repository_name = "postgres-grafana-on-ecs-postgres-repo"
3133
cluster_id = module.cluster.cluster_id
3234
vpc_id = module.vpc.vpc_id
33-
vpc_public_subnets = module.vpc.public_subnets
35+
vpc_private_subnets = module.vpc.private_subnets
3436
security_group_id = module.vpc.security_group_id
3537
namespace_id = module.vpc.namespace_id
3638
secret_manager_name = var.secret_manager_name

ops/ecs/modules/cluster/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ resource "aws_ecs_cluster" "cluster" {
1111
resource "aws_launch_configuration" "ecs_cfg" {
1212
name = "ecs-instance"
1313
image_id = "ami-06581a55723db5feb"
14-
instance_type = "t2.small"
14+
instance_type = var.instance_type
1515

1616
iam_instance_profile = aws_iam_instance_profile.ecsInstanceRole.name
1717

@@ -28,7 +28,7 @@ resource "aws_launch_configuration" "ecs_cfg" {
2828
resource "aws_autoscaling_group" "ecs_instance_asg" {
2929
launch_configuration = aws_launch_configuration.ecs_cfg.name
3030

31-
vpc_zone_identifier = var.vpc_public_subnets
31+
vpc_zone_identifier = concat(var.vpc_public_subnets, var.vpc_private_subnets)
3232
min_size = 2
3333
max_size = 2
3434
desired_capacity = 2

ops/ecs/modules/cluster/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ variable "name" {
33
description = "The name of the cluster"
44
}
55

6+
variable "instance_type" {
7+
type = string
8+
description = "The instance type for the ECS instances"
9+
default = "t2.small"
10+
}
11+
612
variable "vpc_security_group_id" {
713
type = string
814
description = "The security group id for the ECS instances"
@@ -11,4 +17,9 @@ variable "vpc_security_group_id" {
1117
variable "vpc_public_subnets" {
1218
type = list(string)
1319
description = "The public subnets for the ECS instances"
20+
}
21+
22+
variable "vpc_private_subnets" {
23+
type = list(string)
24+
description = "The private subnets for the ECS instances"
1425
}

ops/ecs/modules/grafana/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ resource "aws_ecs_service" "service" {
9393

9494
network_configuration {
9595
security_groups = [var.security_group_id]
96-
subnets = var.vpc_public_subnets
96+
subnets = var.vpc_private_subnets
9797
}
9898

9999
load_balancer {

ops/ecs/modules/grafana/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ variable "vpc_id" {
1919
description = "The ID of the VPC to which the container should be deployed."
2020
}
2121

22+
variable "vpc_private_subnets" {
23+
type = list(string)
24+
description = "The private subnets to which the container should be deployed."
25+
}
26+
2227
variable "vpc_public_subnets" {
2328
type = list(string)
24-
description = "The subnets to which the container should be deployed."
29+
description = "The public subnets to which the container should be deployed."
2530
}
2631

2732
variable "security_group_id" {

ops/ecs/modules/postgres/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ resource "aws_ecs_service" "service" {
8787

8888
network_configuration {
8989
security_groups = [var.security_group_id]
90-
subnets = var.vpc_public_subnets
90+
subnets = var.vpc_private_subnets
9191
}
9292

9393
service_registries {

ops/ecs/modules/postgres/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ variable "vpc_id" {
1313
description = "The ID of the VPC to which the container should be deployed."
1414
}
1515

16-
variable "vpc_public_subnets" {
16+
variable "vpc_private_subnets" {
1717
type = list(string)
1818
description = "The public subnets to which the container should be deployed."
1919
}

ops/ecs/modules/vpc/output.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ output "public_subnets" {
66
value = module.vpc.public_subnets
77
}
88

9+
output "private_subnets" {
10+
value = module.vpc.private_subnets
11+
}
12+
913
output "security_group_id" {
1014
value = aws_security_group.sg.id
1115
}

ops/ecs/modules/vpc/vpc.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ module "vpc" {
1414
cidr = "10.0.0.0/16"
1515
azs = slice(data.aws_availability_zones.available.names, 0, 2)
1616

17-
public_subnets = ["10.0.4.0/24", "10.0.5.0/24"]
17+
public_subnets = ["10.0.4.0/24", "10.0.5.0/24"]
18+
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
1819

20+
enable_nat_gateway = true
21+
single_nat_gateway = true
1922
enable_dns_hostnames = true
2023
}
2124

0 commit comments

Comments
 (0)