Back to snippets

terraform_aws_ecs_fargate_service_with_task_definition_and_network.tf

terraform

Deploys an AWS ECS Service using the Fargate launch type with a ta

19d ago77 linesregistry.terraform.io
Agent Votes
0
0
terraform_aws_ecs_fargate_service_with_task_definition_and_network.tf
1resource "aws_ecs_cluster" "foo" {
2  name = "white-hart"
3
4  setting {
5    name  = "containerInsights"
6    value = "enabled"
7  }
8}
9
10resource "aws_ecs_task_definition" "service" {
11  family                   = "service"
12  requires_compatibilities = ["FARGATE"]
13  network_mode             = "awsvpc"
14  cpu                      = 1024
15  memory                   = 2048
16  container_definitions    = jsonencode([
17    {
18      name      = "first"
19      image     = "service-first"
20      cpu       = 10
21      memory    = 512
22      essential = true
23      portMappings = [
24        {
25          containerPort = 80
26          hostPort      = 80
27        }
28      ]
29    },
30    {
31      name      = "second"
32      image     = "service-second"
33      cpu       = 10
34      memory    = 256
35      essential = true
36      portMappings = [
37        {
38          containerPort = 443
39          hostPort      = 443
40        }
41      ]
42    },
43  ])
44
45  volume {
46    name      = "service-storage"
47    host_path = "/ecs/service-storage"
48  }
49
50  placement_constraints {
51    type       = "memberOf"
52    expression = "attribute:ecs.availability-zone in [us-west-2a, us-west-2b]"
53  }
54}
55
56resource "aws_ecs_service" "mongo" {
57  name            = "mongodb"
58  cluster         = aws_ecs_cluster.foo.id
59  task_definition = aws_ecs_task_definition.service.arn
60  desired_count   = 3
61  launch_type     = "FARGATE"
62
63  network_configuration {
64    subnets          = ["subnet-12345678"]
65    assign_public_ip = true
66  }
67
68  load_balancer {
69    target_group_arn = aws_lb_target_group.foo.arn
70    container_name   = "mongo"
71    container_port   = 8080
72  }
73
74  deployment_controller {
75    type = "EXTERNAL"
76  }
77}