Skip to content

Commit

Permalink
terraform code added
Browse files Browse the repository at this point in the history
  • Loading branch information
ybmadhu committed Feb 19, 2022
1 parent 1da3190 commit f8eea0d
Show file tree
Hide file tree
Showing 16 changed files with 1,160 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
- name: terraform init
run: cd devops && terraform init

36 changes: 36 additions & 0 deletions devops/alb.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
resource "aws_alb" "main" {
name = "jmsth-load-balancer"
subnets = aws_subnet.public.*.id
security_groups = [aws_security_group.lb.id]
}

resource "aws_alb_target_group" "app" {
name = "jmsth-target-group"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
target_type = "ip"

health_check {
healthy_threshold = "3"
interval = "30"
protocol = "HTTP"
matcher = "200"
timeout = "3"
path = var.health_check_path
unhealthy_threshold = "2"
}
}

# Redirect all traffic from the ALB to the target group
resource "aws_alb_listener" "front_end" {
load_balancer_arn = aws_alb.main.id
port = var.app_port
protocol = "HTTP"

default_action {
target_group_arn = aws_alb_target_group.app.id
type = "forward"
}
}

87 changes: 87 additions & 0 deletions devops/auto_scaling.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
resource "aws_appautoscaling_target" "target" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = 2
max_capacity = 4
}

# Automatically scale capacity up by one
resource "aws_appautoscaling_policy" "up" {
name = "cb_scale_up"
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"

step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}

depends_on = [aws_appautoscaling_target.target]
}

# Automatically scale capacity down by one
resource "aws_appautoscaling_policy" "down" {
name = "cb_scale_down"
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"

step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}

depends_on = [aws_appautoscaling_target.target]
}

# CloudWatch alarm that triggers the autoscaling up policy
resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
alarm_name = "cb_cpu_utilization_high"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "85"

dimensions = {
ClusterName = aws_ecs_cluster.main.name
ServiceName = aws_ecs_service.main.name
}

alarm_actions = [aws_appautoscaling_policy.up.arn]
}

# CloudWatch alarm that triggers the autoscaling down policy
resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
alarm_name = "cb_cpu_utilization_low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "10"

dimensions = {
ClusterName = aws_ecs_cluster.main.name
ServiceName = aws_ecs_service.main.name
}

alarm_actions = [aws_appautoscaling_policy.down.arn]
}
11 changes: 11 additions & 0 deletions devops/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#terraform {
# backend "s3" {
# bucket = "jms-terraform-backend"
# key = "jmsth_jenkins.tfstate"
# region = "ap-south-1"
# encrypt = true
# dynamodb_table = "terraform-state-lock-dynamo"
# }
# }

50 changes: 50 additions & 0 deletions devops/ecs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
resource "aws_ecs_cluster" "main" {
name = "jms-cluster"
}
data "aws_ecr_repository" "springapp" {
name = "spring"
}
data "template_file" "cb_app" {
template = file("./templates/ecs/cb_app.json.tpl")

vars = {
app_image = data.aws_ecr_repository.springapp.repository_url
app_port = var.app_port
fargate_cpu = var.fargate_cpu
fargate_memory = var.fargate_memory
aws_region = var.aws_region
tag = var.tag
}
}

resource "aws_ecs_task_definition" "app" {
family = "jms-app-task"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.fargate_cpu
memory = var.fargate_memory
container_definitions = data.template_file.cb_app.rendered
}

resource "aws_ecs_service" "main" {
name = "jms-service1"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.app.arn
desired_count = var.app_count
launch_type = "FARGATE"

network_configuration {
security_groups = [aws_security_group.ecs_tasks.id]
subnets = aws_subnet.private.*.id
assign_public_ip = true
}

load_balancer {
target_group_arn = aws_alb_target_group.app.id
container_name = "jms-app"
container_port = var.app_port
}

depends_on = [aws_alb_listener.front_end, aws_iam_role_policy_attachment.ecs_task_execution_role]
}
Binary file added devops/graph.dot
Binary file not shown.
Loading

0 comments on commit f8eea0d

Please sign in to comment.