forked from mkyong/spring3-mvc-maven-xml-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
# } | ||
# } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Oops, something went wrong.