Skip to content

Commit

Permalink
Create main.tf
Browse files Browse the repository at this point in the history
  • Loading branch information
anusha94 authored Apr 12, 2024
1 parent 95521ee commit d352dfc
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions config-files/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -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]
}

0 comments on commit d352dfc

Please sign in to comment.