|
| 1 | +module "ecs" { |
| 2 | + source = "terraform-aws-modules/ecs/aws" |
| 3 | + |
| 4 | + cluster_name = "rite-blend" |
| 5 | + |
| 6 | + cluster_configuration = { |
| 7 | + execute_command_configuration = { |
| 8 | + logging = "OVERRIDE" |
| 9 | + log_configuration = { |
| 10 | + cloud_watch_log_group_name = "/aws/ecs/ecs-exec-group" # aws_cloudwatch_log_group.ecs_execute_command.name |
| 11 | + } |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + #Specify how fargate provisions serverless resources for your containers |
| 16 | + fargate_capacity_providers = { |
| 17 | + FARGATE = { |
| 18 | + default_capacity_provider_strategy = { |
| 19 | + base = 1 |
| 20 | + weight = 60 |
| 21 | + } |
| 22 | + } |
| 23 | + FARGATE_SPOT = { |
| 24 | + default_capacity_provider_strategy = { |
| 25 | + base = 0 |
| 26 | + weight = 40 |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + # Task execution IAM role policy |
| 33 | + # Automatically creates a task execution role that has default permission for ECS to access ECR, SSM |
| 34 | + create_task_exec_policy = true |
| 35 | + |
| 36 | + |
| 37 | + # Service Configuration |
| 38 | + services = { |
| 39 | + (local.service_name) = { |
| 40 | + cpu = 1024 |
| 41 | + memory = 4096 |
| 42 | + |
| 43 | + assign_public_ip = true |
| 44 | + enable_execute_command = true |
| 45 | + |
| 46 | + |
| 47 | + # Container definition(s) |
| 48 | + container_definitions = { |
| 49 | + (local.container_name) = { |
| 50 | + cpu = 512 |
| 51 | + memory = 2048 |
| 52 | + essential = true |
| 53 | + image = "public.ecr.aws/aws-containers/ecsdemo-frontend:latest" |
| 54 | + |
| 55 | + |
| 56 | + port_mappings = [ |
| 57 | + { |
| 58 | + name = local.container_name |
| 59 | + containerPort = local.container_port |
| 60 | + hostPort = local.container_port |
| 61 | + protocol = "tcp" |
| 62 | + } |
| 63 | + ] |
| 64 | + |
| 65 | + # Example image used requires access to write to root filesystem |
| 66 | + readonly_root_filesystem = false |
| 67 | + |
| 68 | + #Allows terraform to manage the cloudwatch log group |
| 69 | + create_cloudwatch_log_group = true |
| 70 | + cloud_watch_log_group_name = "/aws/ecs/${local.service_name}/${local.container_name}" |
| 71 | + cloudwatch_log_group_retention = 7 |
| 72 | + |
| 73 | + |
| 74 | + log_configuration = { |
| 75 | + logDriver = "awslogs" |
| 76 | + options = { |
| 77 | + awslogs-region = var.aws_region |
| 78 | + awslogs-group = "/aws/ecs/${local.service_name}/${local.container_name}" |
| 79 | + awslogs-stream-prefix = "ecs" |
| 80 | + |
| 81 | + } |
| 82 | + } |
| 83 | + memory_reservation = 100 |
| 84 | + |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + # Connect ECS Service to Load Balancer |
| 89 | + load_balancer = { |
| 90 | + service = { |
| 91 | + target_group_arn = module.alb.target_groups["ror-frontend-service"].arn |
| 92 | + container_name = local.container_name |
| 93 | + container_port = local.container_port |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + #Specify subnets to host ECS service and tasks |
| 99 | + subnet_ids = data.terraform_remote_state.vpc.outputs.public_subnets |
| 100 | + |
| 101 | + #Specify ECS service security groups |
| 102 | + security_group_rules = { |
| 103 | + public_ingress_80 = { |
| 104 | + type = "ingress" |
| 105 | + from_port = local.container_port |
| 106 | + to_port = local.container_port |
| 107 | + protocol = "tcp" |
| 108 | + source_security_group_id = module.alb.security_group_id |
| 109 | + description = "Allow traffic from ALB SG only" |
| 110 | + } |
| 111 | + egress_all = { |
| 112 | + type = "egress" |
| 113 | + from_port = 0 |
| 114 | + to_port = 0 |
| 115 | + protocol = "-1" |
| 116 | + cidr_blocks = ["0.0.0.0/0"] |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + # TODO |
| 124 | + # - Create Task Execution Role -- to allow ECS access other AWS services it requires to create the task. |
| 125 | + # E.g accessing SSM parameter store for secrets to be injected when creating tasks for containers |
| 126 | + # - Create Task Role -- to allow running containers access other aws services- e.g container needs access to S3. |
| 127 | +} |
| 128 | + |
| 129 | + |
0 commit comments