Skip to content

Commit b8b1824

Browse files
authored
Merge pull request #35 from sanG-github/release/0.3.0
Release - 0.3.0
2 parents ec36e4c + 5d02fed commit b8b1824

34 files changed

+1117
-24
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"task_desired_count": 3,
3+
"web_container_cpu": 256,
4+
"web_container_memory": 512,
5+
"deployment_maximum_percent": 200,
6+
"deployment_minimum_healthy_percent": 50,
7+
"max_capacity": 10,
8+
"max_cpu_threshold": 80
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"task_desired_count": 3,
3+
"web_container_cpu": 256,
4+
"web_container_memory": 512,
5+
"deployment_maximum_percent": 200,
6+
"deployment_minimum_healthy_percent": 50,
7+
"max_capacity": 10,
8+
"max_cpu_threshold": 80
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"AVAILABLE_LOCALES": "en",
3+
"DEFAULT_LOCALE": "en"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"AVAILABLE_LOCALES": "en",
3+
"DEFAULT_LOCALE": "en"
4+
}

core/locals.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,65 @@ locals {
55
# The owner of the infrastructure, used to tag the resources, e.g. `acme-web`
66
owner = "sanghuynh20000"
77

8+
# The repository name of the ECR to retrieve the image from
9+
ecr_repo_name = "devops-ic-ecr"
10+
811
# AWS region
912
region = "ap-southeast-1"
13+
14+
# The application exposed port
15+
app_port = 3000
16+
17+
# The health check path of the Application
18+
health_check_path = "/health"
19+
20+
# The ECS configuration for the current environment
21+
current_ecs_config = local.ecs_config[var.environment]
22+
23+
# ECS configurations for each environment
24+
ecs_config = {
25+
staging = jsondecode(file("assets/ecs_configs/staging.json"))
26+
production = jsondecode(file("assets/ecs_configs/production.json"))
27+
}
28+
29+
# ENV variables for the current environment
30+
current_environment_variables = local.environment_variables[var.environment]
31+
32+
# ENV variables for each environment
33+
environment_variables = {
34+
staging = [for k, v in jsondecode(file("assets/environment_variables/staging.json")) : { name = k, value = v }]
35+
production = [for k, v in jsondecode(file("assets/environment_variables/production.json")) : { name = k, value = v }]
36+
}
37+
38+
current_rds_config = local.rds_config[var.environment]
39+
40+
rds_config = {
41+
staging = {
42+
instance_type = "db.t3.micro"
43+
port = 5432
44+
autoscaling_min_capacity = 0
45+
autoscaling_max_capacity = 3
46+
}
47+
48+
production = {
49+
instance_type = "db.t3.micro"
50+
port = 5432
51+
autoscaling_min_capacity = 1
52+
autoscaling_max_capacity = 3
53+
}
54+
}
55+
56+
current_elasticache_config = local.elasticache_config[var.environment]
57+
58+
elasticache_config = {
59+
staging = {
60+
node_type = "cache.t2.micro"
61+
port = 6379
62+
}
63+
64+
production = {
65+
node_type = "cache.t2.micro"
66+
port = 6379
67+
}
68+
}
1069
}

core/main.tf

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,98 @@ terraform {
88
}
99
}
1010

11+
module "vpc" {
12+
source = "../modules/vpc"
13+
}
14+
15+
module "s3" {
16+
source = "../modules/s3"
17+
}
18+
19+
module "security_group" {
20+
source = "../modules/security_group"
21+
22+
vpc_id = module.vpc.vpc_id
23+
app_port = local.app_port
24+
rds_port = local.current_rds_config.port
25+
elasticache_port = local.current_elasticache_config.port
26+
private_subnets_cidr_blocks = module.vpc.private_subnets_cidr_blocks
27+
}
28+
29+
module "alb" {
30+
source = "../modules/alb"
31+
32+
vpc_id = module.vpc.vpc_id
33+
app_port = local.app_port
34+
subnet_ids = module.vpc.public_subnet_ids
35+
security_group_ids = module.security_group.alb_security_group_ids
36+
health_check_path = local.health_check_path
37+
}
38+
1139
module "cloudwatch" {
1240
source = "../modules/cloudwatch"
1341

14-
kms_key_id = module.secrets_manager.secret_cloudwatch_log_key_arn
42+
kms_key_id = module.secrets_manager.secret_key_arn
1543
}
1644

1745
module "secrets_manager" {
1846
source = "../modules/secrets_manager"
1947

2048
secrets = {
49+
database_url = module.rds.db_url
50+
redis_url = module.elasticache.redis_primary_endpoint
2151
secret_key_base = var.secret_key_base
2252
}
2353
}
2454

25-
module "vpc" {
26-
source = "../modules/vpc"
55+
module "ecs" {
56+
source = "../modules/ecs"
57+
58+
region = local.region
59+
app_port = local.app_port
60+
ecr_repo_name = local.ecr_repo_name
61+
health_check_path = local.health_check_path
62+
subnets = module.vpc.private_subnet_ids
63+
security_groups = module.security_group.ecs_security_group_ids
64+
alb_target_group_arn = module.alb.alb_target_group_arn
65+
aws_cloudwatch_log_group_name = module.cloudwatch.aws_cloudwatch_log_group_name
66+
deployment_maximum_percent = local.current_ecs_config.deployment_maximum_percent
67+
deployment_minimum_healthy_percent = local.current_ecs_config.deployment_minimum_healthy_percent
68+
web_container_cpu = local.current_ecs_config.web_container_cpu
69+
web_container_memory = local.current_ecs_config.web_container_memory
70+
task_desired_count = local.current_ecs_config.task_desired_count
71+
max_capacity = local.current_ecs_config.max_capacity
72+
max_cpu_threshold = local.current_ecs_config.max_cpu_threshold
73+
74+
environment_variables = local.current_environment_variables
75+
secrets_variables = module.secrets_manager.secrets_variables
76+
secret_arns = module.secrets_manager.secret_arns
2777
}
2878

29-
module "s3" {
30-
source = "../modules/s3"
79+
module "rds" {
80+
source = "../modules/rds"
81+
82+
vpc_security_group_ids = module.security_group.rds_security_group_ids
83+
vpc_id = module.vpc.vpc_id
84+
subnet_ids = module.vpc.private_subnet_ids
85+
86+
database_name = var.environment
87+
username = var.rds_username
88+
password = var.rds_password
89+
90+
instance_type = local.current_rds_config.instance_type
91+
port = local.current_rds_config.port
92+
autoscaling_min_capacity = local.current_rds_config.autoscaling_min_capacity
93+
autoscaling_max_capacity = local.current_rds_config.autoscaling_max_capacity
94+
}
95+
96+
module "elasticache" {
97+
source = "../modules/elasticache"
98+
99+
node_type = local.current_elasticache_config.node_type
100+
port = local.current_elasticache_config.port
101+
subnet_ids = module.vpc.private_subnet_ids
102+
security_group_ids = module.security_group.elasticache_security_group_ids
103+
auth_token = var.redis_auth_token
104+
kms_key_id = module.secrets_manager.secret_key_arn
31105
}

core/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,22 @@ variable "environment" {
77
variable "secret_key_base" {
88
description = "The secret key base for the application"
99
type = string
10+
sensitive = true
11+
}
12+
13+
variable "rds_username" {
14+
description = "RDS username"
15+
type = string
16+
sensitive = true
17+
}
18+
19+
variable "rds_password" {
20+
description = "RDS password"
21+
type = string
22+
sensitive = true
23+
}
24+
25+
variable "redis_auth_token" {
26+
description = "The auth token for the Redis cluster"
27+
type = string
1028
}

modules/alb/locals.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
locals {
2+
# aws_lb
3+
namespace = "devops-ic-alb"
4+
access_logs_bucket = "${local.namespace}-log-bucket"
5+
application_target_group_name = "${local.namespace}-application-tg"
6+
load_balancer_type = "application"
7+
8+
# aws_lb_target_group
9+
application_target_group_protocol = "HTTP"
10+
application_target_type = "ip"
11+
application_target_deregistration_delay = 100
12+
13+
# Health check timeout must be smaller than the interval
14+
health_check_timeout = 20
15+
health_check_interval = 30
16+
health_check_healthy_threshold = 3
17+
health_check_unhealthy_threshold = 3
18+
health_check_protocol = "HTTP"
19+
health_check_matcher = "200-299"
20+
21+
# stickiness
22+
enable_stickiness = false
23+
stickiness_type = "lb_cookie"
24+
25+
# aws_lb_listener
26+
aws_lb_listener_port = 80
27+
aws_lb_listener_protocol = "HTTP"
28+
}

modules/alb/main.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
resource "aws_lb" "this" {
2+
#checkov:skip=CKV2_AWS_20: HTTP protocol is used for redirecting to HTTPS
3+
#checkov:skip=CKV2_AWS_28: There is no need to protect public ALB by WAF
4+
name = local.namespace
5+
load_balancer_type = local.load_balancer_type
6+
subnets = var.subnet_ids
7+
security_groups = var.security_group_ids
8+
9+
enable_deletion_protection = true
10+
drop_invalid_header_fields = true
11+
12+
# tfsec:ignore:aws-elb-alb-not-public
13+
internal = false
14+
15+
access_logs {
16+
bucket = local.access_logs_bucket
17+
enabled = true
18+
}
19+
}
20+
21+
resource "aws_lb_target_group" "application_target_group" {
22+
port = var.app_port
23+
vpc_id = var.vpc_id
24+
name = local.application_target_group_name
25+
target_type = local.application_target_type
26+
deregistration_delay = local.application_target_deregistration_delay
27+
protocol = local.application_target_group_protocol
28+
29+
health_check {
30+
path = var.health_check_path
31+
port = var.app_port
32+
timeout = local.health_check_timeout
33+
interval = local.health_check_interval
34+
healthy_threshold = local.health_check_healthy_threshold
35+
unhealthy_threshold = local.health_check_unhealthy_threshold
36+
protocol = local.health_check_protocol
37+
matcher = local.health_check_matcher
38+
}
39+
40+
dynamic "stickiness" {
41+
for_each = local.enable_stickiness ? [1] : []
42+
43+
content {
44+
enabled = local.enable_stickiness
45+
type = local.stickiness_type
46+
}
47+
}
48+
}
49+
50+
# tfsec:ignore:aws-elb-http-not-used
51+
resource "aws_lb_listener" "app_http" {
52+
load_balancer_arn = aws_lb.this.arn
53+
port = local.aws_lb_listener_port
54+
55+
#checkov:skip=CKV_AWS_2: HTTP protocol is used for redirecting to HTTPS
56+
#checkov:skip=CKV_AWS_103: HTTP protocol is used for redirecting to HTTPS, so there is no TLS certificate
57+
protocol = local.aws_lb_listener_protocol
58+
59+
default_action {
60+
type = "forward"
61+
target_group_arn = aws_lb_target_group.application_target_group.arn
62+
}
63+
}

modules/alb/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "alb_name" {
2+
description = "Application LB name"
3+
value = aws_lb.this.name
4+
}
5+
6+
output "alb_dns_name" {
7+
description = "Application LB DNS name"
8+
value = aws_lb.this.dns_name
9+
}
10+
11+
output "alb_zone_id" {
12+
description = "Application LB Zone ID"
13+
value = aws_lb.this.zone_id
14+
}
15+
16+
output "alb_target_group_arn" {
17+
description = "ALB target group ARN"
18+
value = aws_lb_target_group.application_target_group.arn
19+
}

0 commit comments

Comments
 (0)