From 9a197cf3eaec9d391bf77e3c0bb45c5d2b270bb3 Mon Sep 17 00:00:00 2001 From: TAKANO Mitsuhiro Date: Tue, 27 Feb 2024 22:39:46 +0900 Subject: [PATCH] ECS: Use `FARGATE_SPOT` capacity provider --- db.tf | 3 +++ fargate.tf | 21 ++++++++++++--------- variables.tf | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/db.tf b/db.tf index ec9bffe..61cf7b8 100644 --- a/db.tf +++ b/db.tf @@ -31,6 +31,9 @@ resource "aws_rds_cluster" "this" { } resource "aws_rds_cluster_instance" "cluster_instances" { + count = 3 + availability_zone = module.vpc.azs[count.index % length(module.vpc.azs)] + engine = "aurora-mysql" engine_version = "8.0.mysql_aurora.3.04.1" cluster_identifier = "${var.prefix}-${var.environment}" diff --git a/fargate.tf b/fargate.tf index 99abb3e..346959a 100644 --- a/fargate.tf +++ b/fargate.tf @@ -106,11 +106,11 @@ resource "aws_ecs_cluster" "this" { } resource "aws_ecs_cluster_capacity_providers" "cluster" { - cluster_name = "${var.prefix}-${var.environment}" - - capacity_providers = ["FARGATE_SPOT", "FARGATE"] - + cluster_name = "${var.prefix}-${var.environment}" + capacity_providers = ["FARGATE_SPOT"] default_capacity_provider_strategy { + base = 0 + weight = 10 capacity_provider = "FARGATE_SPOT" } } @@ -148,14 +148,15 @@ resource "aws_ecs_service" "this" { desired_count = var.desired_count launch_type = "FARGATE" enable_execute_command = true + network_configuration { security_groups = [ aws_security_group.alb.id, aws_security_group.db.id, aws_security_group.efs.id, aws_security_group.elasticache.id - ] - subnets = module.vpc.private_subnets + ] + subnets = module.vpc.private_subnets } load_balancer { @@ -165,12 +166,14 @@ resource "aws_ecs_service" "this" { } lifecycle { - ignore_changes = [desired_count] + ignore_changes = [ + task_definition, + desired_count, + capacity_provider_strategy + ] } - } - resource "aws_ecs_task_definition" "this" { family = "${var.prefix}-${var.environment}" execution_role_arn = aws_iam_role.task_execution_role.arn diff --git a/variables.tf b/variables.tf index ec0e311..cec8bc8 100644 --- a/variables.tf +++ b/variables.tf @@ -27,7 +27,7 @@ variable "error_ttl" { variable "desired_count" { description = "The number of instances of fargate tasks to keep running" - default = "2" + default = "3" } variable "log_retention_in_days" { description = "The number of days to retain cloudwatch log"