From d352dfc521af999b9497a51c8b524b4868980081 Mon Sep 17 00:00:00 2001 From: Anusha Hegde Date: Fri, 12 Apr 2024 10:11:48 +0200 Subject: [PATCH] Create main.tf --- config-files/terraform/main.tf | 94 ++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 config-files/terraform/main.tf diff --git a/config-files/terraform/main.tf b/config-files/terraform/main.tf new file mode 100644 index 0000000..6859ab0 --- /dev/null +++ b/config-files/terraform/main.tf @@ -0,0 +1,94 @@ +# Create an ECS cluster +resource "aws_ecs_cluster" "ecs_cluster" { + name = "my-test-ecs-cluster" +} + +resource "aws_ecs_capacity_provider" "ecs_capacity_provider" { + name = "test1" + + auto_scaling_group_provider { + auto_scaling_group_arn = aws_autoscaling_group.ecs_asg.arn + + managed_scaling { + maximum_scaling_step_size = 1000 + minimum_scaling_step_size = 1 + status = "ENABLED" + target_capacity = 3 + } + } +} + +resource "aws_ecs_cluster_capacity_providers" "example" { + cluster_name = aws_ecs_cluster.ecs_cluster.name + + capacity_providers = [aws_ecs_capacity_provider.ecs_capacity_provider.name] + + default_capacity_provider_strategy { + base = 1 + weight = 100 + capacity_provider = aws_ecs_capacity_provider.ecs_capacity_provider.name + } +} + +# Define the ECS task definition for the service +resource "aws_ecs_task_definition" "ecs_task_definition" { + family = "my-ecs-task" + network_mode = "awsvpc" + execution_role_arn = "arn:aws:iam::844333597536:role/ecsTaskExecutionRole" + cpu = 256 + runtime_platform { + operating_system_family = "LINUX" + cpu_architecture = "X86_64" + } + container_definitions = jsonencode([ + { + name = "dockergs" + image = "public.ecr.aws/f9n5f1l7/dgs:latest" + cpu = 256 + memory = 512 + essential = true + portMappings = [ + { + containerPort = 80 + hostPort = 80 + protocol = "tcp" + } + ] + } + ]) +} + +# Define the ECS service that will run the task +resource "aws_ecs_service" "ecs_service" { + name = "my-ecs-service" + cluster = aws_ecs_cluster.ecs_cluster.id + task_definition = aws_ecs_task_definition.ecs_task_definition.arn + desired_count = 2 + + network_configuration { + subnets = [aws_subnet.subnet.id, aws_subnet.subnet2.id] + security_groups = [aws_security_group.security_group.id] + } + + force_new_deployment = true + placement_constraints { + type = "distinctInstance" + } + + triggers = { + redeployment = timestamp() + } + + capacity_provider_strategy { + capacity_provider = aws_ecs_capacity_provider.ecs_capacity_provider.name + weight = 100 + } + + load_balancer { + target_group_arn = aws_lb_target_group.ecs_tg.arn + container_name = "dockergs" + container_port = 80 + } + + depends_on = [aws_autoscaling_group.ecs_asg] +}