Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor initial infra code to support DEV instance resources #6

Merged
merged 53 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d181de1
added initial ecr tf scripts
Jul 23, 2024
e417962
Use env variables from root
Jul 24, 2024
24578a1
- Added ecs, ecr, vpc, elb code
Jul 25, 2024
3b744a8
hard code region for backend config
Jul 25, 2024
4ab505e
Include container module dependencies.
Jul 25, 2024
664bcb8
Fixed renamed resource
Jul 25, 2024
abebde8
Add networking module reference
Jul 25, 2024
81d0e86
Fixed passing of i/o variables
Jul 25, 2024
2780abb
Use sg.id to sgs in lb
Jul 25, 2024
180ca95
Use lb arn
Jul 25, 2024
f932506
- Added internet gateway
Jul 25, 2024
b848277
Fixed plan error
Jul 25, 2024
f494283
Fixed subnet id build error
Jul 25, 2024
1fd222d
Update lb tg to use target type to ip
Jul 25, 2024
d73d82a
Revert to single value only
Jul 25, 2024
3b75e39
- changed app name to jasper
Jul 26, 2024
208c454
Added force_delete in ECR repo resource
Jul 26, 2024
8377fc6
Update web container name and port
Jul 26, 2024
730da4f
Added port variable
Jul 26, 2024
c17801f
Changed port type to number
Jul 26, 2024
379cfe7
- Added more roles to ecs web task definition
Jul 29, 2024
1ca481b
Fixed ecs web task role policy
Jul 29, 2024
4aca9a3
Added ECS Web Task ARN to policy
Jul 29, 2024
ad3f988
- Renamed resources
Jul 29, 2024
7b7ddc4
Updated policy
Jul 29, 2024
6fdae6b
Added log groups and change port to 8080
Jul 29, 2024
acb436a
- Setup CW for ECS tasks
Jul 29, 2024
9c76f30
- Added sandbox.tfvars
Jul 31, 2024
7ad255c
Use default vpc
Jul 31, 2024
fa6198f
Removed unused variables
Jul 31, 2024
28a2f8f
- Refactor variables
Aug 1, 2024
b5b3fc2
Removed .gitkeep
Aug 1, 2024
4ecb27a
Use tfvars file in aws template tf
Aug 1, 2024
7f729ec
- Added dev workflow
Aug 1, 2024
3ebdf3d
Moved app related variables so that values are retrieved as an env va…
Aug 6, 2024
07fc4f0
Update tf state filename
Aug 6, 2024
2a60cb1
Rename job name
Aug 6, 2024
bcd3367
Update tf remote state lock name
Aug 6, 2024
1630c3e
Removed default=true filter so that it will get the current available…
Aug 6, 2024
2cb49d1
Refactor VPC to use custom VPC instead of default so that infra works…
Aug 7, 2024
f99b6d7
Merge branch 'master' into feature-infraPipeline
Aug 7, 2024
75c7e32
Merge branch 'master' to 'feature-infraPipeline'
Aug 7, 2024
6d64078
Refactor code to use provisioned VPC and SG.
Aug 7, 2024
26add7b
Add TF_VAR_vpc_id as an env var
Aug 7, 2024
b2ed914
Added rt, route and rt assoc.
Aug 7, 2024
3eaeb42
- Create and attach new SG
Aug 7, 2024
f6471ad
Added missing code changes
Aug 7, 2024
e31ea2e
Fixed assigned sec group
Aug 7, 2024
329d876
Specify retention days
Aug 7, 2024
d9e2117
Removed tfstate
Aug 8, 2024
de3f127
Removed code related to test env for now
Aug 8, 2024
b51ff03
Refactor infra code to use provisioned resources (e.g. VPC, Subnets) …
Aug 8, 2024
45e7644
Refactor infra code to use provisioned resources (e.g. VPC, Subnets) …
Aug 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/aws-template-terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
TF_VAR_app_name: ${{ vars.APP_NAME }}
TF_VAR_environment: ${{ vars.ENVIRONMENT_NAME }}
TF_VAR_kms_key_name: ${{ vars.KMS_KEY_NAME }}
TF_VAR_vpc_id: ${{ vars.VPC_ID }}
needs: [check_changes]
steps:
- name: Checkout repository
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-infra-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:

jobs:
infrastructure_deploy_snd:
infrastructure_deploy_dev:
uses: ./.github/workflows/aws-template-terraform.yml
with:
CONTEXT_FOLDER: ./infrastructure/cloud/environments/dev
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/cloud/environments/dev/backend.tfvars
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bucket = "terraform-remote-state-dev"
dynamodb_table = "terraform-remote-state-lock"
bucket = "terraform-remote-state-b5e4f5-dev"
dynamodb_table = "terraform-remote-state-lock-b5e4f5"
key = "terraform.tfstate"
region = "ca-central-1"
5 changes: 4 additions & 1 deletion infrastructure/cloud/environments/dev/dev.tfvars
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
region = "ca-central-1"
test_s3_bucket_name = "test-s3-bucket"
test_s3_bucket_name = "jasper-test-s3-bucket-dev"
web_subnet_names = ["Web_Dev_aza_net", "Web_Dev_azb_net"]
# api_subnet_names = ["App_Dev_aza_net", "App_Dev_azb_net"]
# db_subnet_names = ["Data_Dev_aza_net", "Data_Dev_azb_net"]
20 changes: 20 additions & 0 deletions infrastructure/cloud/environments/dev/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,23 @@ variable "environment" {
description = "The AWS environment to deploy to"
type = string
}

variable "vpc_id" {
description = "The provisioned VPC ID"
type = string
}

variable "web_subnet_names" {
description = "List of Subnets for Web"
type = list(string)
}

# variable "api_subnet_names" {
# description = "List of Subnets for API"
# type = list(string)
# }

# variable "db_subnet_names" {
# description = "List of Subnets for Database"
# type = list(string)
# }
33 changes: 19 additions & 14 deletions infrastructure/cloud/environments/dev/webapp.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,28 @@ module "storage" {
}

module "networking" {
source = "../../modules/networking"
environment = var.environment
app_name = var.app_name
region = var.region
subnet_ids = module.networking.subnet_ids
source = "../../modules/networking"
environment = var.environment
app_name = var.app_name
region = var.region
vpc_id = var.vpc_id
web_subnet_names = var.web_subnet_names
# api_subnet_names = var.api_subnet_names
# db_subnet_names = var.db_subnet_names
}

module "container" {
source = "../../modules/container"
environment = var.environment
app_name = var.app_name
region = var.region
ecs_execution_role_arn = module.security.ecs_execution_role_arn
subnet_ids = module.networking.subnet_ids
sg_id = module.networking.sg_id
lb_tg_arn = module.networking.lb_tg_arn
ecs_web_log_group_name = module.monitoring.ecs_web_log_group_name
source = "../../modules/container"
environment = var.environment
app_name = var.app_name
region = var.region
ecs_execution_role_arn = module.security.ecs_execution_role_arn
subnet_ids = module.networking.web_subnets_ids
sg_id = module.networking.ecs_sg_id
lb_tg_arn = module.networking.lb_tg_arn
ecs_web_td_log_group_name = module.monitoring.ecs_web_td_log_group_name
depends_on = [module.monitoring]

}

module "monitoring" {
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/cloud/environments/sandbox/sandbox.tfvars
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
region = "ca-central-1"
test_s3_bucket_name = "test-s3-bucket"
test_s3_bucket_name = "jasper-test-s3-bucket-snd"
3 changes: 1 addition & 2 deletions infrastructure/cloud/environments/sandbox/webapp.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module "networking" {
environment = var.environment
app_name = var.app_name
region = var.region
subnet_ids = module.networking.subnet_ids
}

module "container" {
Expand All @@ -28,7 +27,7 @@ module "container" {
app_name = var.app_name
region = var.region
ecs_execution_role_arn = module.security.ecs_execution_role_arn
subnet_ids = module.networking.subnet_ids
subnet_ids = [module.networking.private_subnets_web[0], module.networking.private_subnets_web[1]]
sg_id = module.networking.sg_id
lb_tg_arn = module.networking.lb_tg_arn
ecs_web_log_group_name = module.monitoring.ecs_web_log_group_name
Expand Down
10 changes: 5 additions & 5 deletions infrastructure/cloud/modules/container/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ resource "aws_ecs_task_definition" "ecs_web_task_definition" {
{
containerPort = 8080
}
],
]
logConfiguration = {
logDriver = "awslogs",
logDriver = "awslogs"
options = {
awslogs-group = var.ecs_web_log_group_name,
awslogs-region = var.region,
awslogs-stream-prefix = "ecs"
"awslogs-group" = var.ecs_web_td_log_group_name
"awslogs-region" = var.region
"awslogs-stream-prefix" = "ecs"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions infrastructure/cloud/modules/container/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ variable "ecs_execution_role_arn" {
}

variable "subnet_ids" {
description = "Public Subnet IDs"
description = "Subnet IDs in which ECS will deploy the tasks"
type = list(string)
}

Expand All @@ -33,7 +33,7 @@ variable "lb_tg_arn" {
type = string
}

variable "ecs_web_log_group_name" {
description = "ECS Web Log Group Name in CloudWatch"
variable "ecs_web_td_log_group_name" {
description = "ECS Web Task Definition Log Group Name in CloudWatch"
type = string
}
6 changes: 3 additions & 3 deletions infrastructure/cloud/modules/monitoring/logs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "aws_cloudwatch_log_group" "ecs_web_log_group" {
name = "${var.app_name}-ecs-web-log-group-${var.environment}"
retention_in_days = 30
resource "aws_cloudwatch_log_group" "ecs_web_td_log_group" {
name = "${var.app_name}-ecs-web-td-log-group-${var.environment}"
retention_in_days = 90
}
4 changes: 2 additions & 2 deletions infrastructure/cloud/modules/monitoring/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
output "ecs_web_log_group_name" {
value = aws_cloudwatch_log_group.ecs_web_log_group.name
output "ecs_web_td_log_group_name" {
value = aws_cloudwatch_log_group.ecs_web_td_log_group.name
}
48 changes: 48 additions & 0 deletions infrastructure/cloud/modules/networking/alb.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
resource "aws_lb" "lb" {
name = "${var.app_name}-lb-${var.environment}"
subnets = local.web_subnets
security_groups = [aws_security_group.sg.id]
internal = true
load_balancer_type = "application"
enable_http2 = true

tags = {
Name = "${var.app_name}-lb-${var.environment}"
}
}

resource "aws_lb_target_group" "lb_target_group" {
name = "${var.app_name}-lb-tg-${var.environment}"
port = 8080
protocol = "HTTP"
vpc_id = data.aws_vpc.vpc.id
target_type = "ip"
deregistration_delay = 5

health_check {
enabled = true
interval = 15
path = "/"
port = 8080
protocol = "HTTP"
timeout = 10
healthy_threshold = 2
unhealthy_threshold = 3
matcher = "200"
}

lifecycle {
create_before_destroy = true
}
}

resource "aws_lb_listener" "lb_listener" {
load_balancer_arn = aws_lb.lb.arn
port = 80
protocol = "HTTP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.lb_target_group.arn
}
}
43 changes: 0 additions & 43 deletions infrastructure/cloud/modules/networking/elb.tf

This file was deleted.

8 changes: 6 additions & 2 deletions infrastructure/cloud/modules/networking/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ output "lb_tg_arn" {
value = aws_lb_target_group.lb_target_group.arn
}

output "subnet_ids" {
value = data.aws_subnets.default_public.ids
output "ecs_sg_id" {
value = aws_security_group.ecs_sg.id
}

output "web_subnets_ids" {
value = local.web_subnets
}
47 changes: 26 additions & 21 deletions infrastructure/cloud/modules/networking/securitygroup.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Load Balancer Security Group
# Load Balancer Security Group
resource "aws_security_group" "sg" {
name = "${var.app_name}-lb-sg-${var.environment}"
vpc_id = data.aws_vpc.default.id
vpc_id = data.aws_vpc.vpc.id

ingress {
from_port = 80
Expand All @@ -23,25 +23,30 @@ resource "aws_security_group" "sg" {
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "${var.app_name}_sg_${var.environment}"
}
}


# # ECS Security Group
# resource "aws_security_group" "ecs_sg" {
# name = "${var.app_name}-ecs-sg-${var.environment}"
# vpc_id = aws_vpc.vpc.id

# ingress {
# from_port = 80
# to_port = 80
# protocol = "tcp"
# security_groups = [aws_security_group.sg.id]
# }

# egress {
# from_port = 0
# to_port = 0
# protocol = "-1"
# cidr_blocks = ["0.0.0.0/0"]
# }
# }
# ECS Security Group
resource "aws_security_group" "ecs_sg" {
name = "${var.app_name}-ecs-sg-${var.environment}"
vpc_id = data.aws_vpc.vpc.id

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = null
security_groups = [aws_security_group.sg.id]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
18 changes: 16 additions & 2 deletions infrastructure/cloud/modules/networking/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@ variable "region" {
type = string
}

variable "subnet_ids" {
description = "The default VPC subnet ids"
variable "vpc_id" {
description = "The provisioned VPC ID"
type = string
}

variable "web_subnet_names" {
description = "List of Subnets for Web"
type = list(string)
}

# variable "api_subnet_names" {
# description = "List of Subnets for API"
# type = list(string)
# }

# variable "db_subnet_names" {
# description = "List of Subnets for Database"
# type = list(string)
# }
Loading
Loading