|
| 1 | +provider "aws" { |
| 2 | + region = "eu-west-1" |
| 3 | +} |
| 4 | + |
| 5 | +##### |
| 6 | +# VPC and subnets |
| 7 | +##### |
| 8 | +module "vpc" { |
| 9 | + source = "terraform-aws-modules/vpc/aws" |
| 10 | + version = "~> 2.21" |
| 11 | + |
| 12 | + name = "simple-vpc" |
| 13 | + |
| 14 | + cidr = "10.0.0.0/16" |
| 15 | + |
| 16 | + azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"] |
| 17 | + private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] |
| 18 | + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] |
| 19 | + |
| 20 | + enable_nat_gateway = false |
| 21 | +} |
| 22 | + |
| 23 | +##### |
| 24 | +# ALB |
| 25 | +##### |
| 26 | +module "alb" { |
| 27 | + source = "umotif-public/alb/aws" |
| 28 | + version = "~> 1.0" |
| 29 | + |
| 30 | + name_prefix = "alb-example" |
| 31 | + load_balancer_type = "application" |
| 32 | + internal = false |
| 33 | + vpc_id = module.vpc.vpc_id |
| 34 | + subnets = module.vpc.public_subnets |
| 35 | +} |
| 36 | + |
| 37 | +resource "aws_lb_listener" "alb_80" { |
| 38 | + load_balancer_arn = module.alb.arn |
| 39 | + port = "80" |
| 40 | + protocol = "HTTP" |
| 41 | + |
| 42 | + default_action { |
| 43 | + type = "forward" |
| 44 | + target_group_arn = module.fargate.target_group_arn |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +##### |
| 49 | +# Security Group Config |
| 50 | +##### |
| 51 | +resource "aws_security_group_rule" "alb_ingress_80" { |
| 52 | + security_group_id = module.alb.security_group_id |
| 53 | + type = "ingress" |
| 54 | + protocol = "tcp" |
| 55 | + from_port = 80 |
| 56 | + to_port = 80 |
| 57 | + cidr_blocks = ["0.0.0.0/0"] |
| 58 | + ipv6_cidr_blocks = ["::/0"] |
| 59 | +} |
| 60 | + |
| 61 | +resource "aws_security_group_rule" "task_ingress_80" { |
| 62 | + security_group_id = module.fargate.service_sg_id |
| 63 | + type = "ingress" |
| 64 | + protocol = "tcp" |
| 65 | + from_port = 80 |
| 66 | + to_port = 80 |
| 67 | + source_security_group_id = module.alb.security_group_id |
| 68 | +} |
| 69 | + |
| 70 | +##### |
| 71 | +# EFS |
| 72 | +##### |
| 73 | +resource "aws_efs_file_system" "efs" { |
| 74 | + creation_token = "efs-html" |
| 75 | + |
| 76 | + tags = { |
| 77 | + Name = "efs-html" |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +##### |
| 82 | +# ECS cluster and fargate |
| 83 | +##### |
| 84 | +resource "aws_ecs_cluster" "cluster" { |
| 85 | + name = "ecs-spot-test" |
| 86 | + capacity_providers = ["FARGATE_SPOT", "FARGATE"] |
| 87 | + |
| 88 | + default_capacity_provider_strategy { |
| 89 | + capacity_provider = "FARGATE_SPOT" |
| 90 | + } |
| 91 | + |
| 92 | + setting { |
| 93 | + name = "containerInsights" |
| 94 | + value = "disabled" |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +module "fargate" { |
| 99 | + source = "../../" |
| 100 | + |
| 101 | + name_prefix = "ecs-fargate-example" |
| 102 | + vpc_id = module.vpc.vpc_id |
| 103 | + private_subnet_ids = module.vpc.public_subnets |
| 104 | + lb_arn = module.alb.arn |
| 105 | + cluster_id = aws_ecs_cluster.cluster.id |
| 106 | + |
| 107 | + platform_version = "1.4.0" |
| 108 | + |
| 109 | + task_container_image = "marcincuber/2048-game:latest" |
| 110 | + task_definition_cpu = 256 |
| 111 | + task_definition_memory = 512 |
| 112 | + |
| 113 | + task_container_port = 80 |
| 114 | + task_container_assign_public_ip = true |
| 115 | + |
| 116 | + health_check = { |
| 117 | + port = "traffic-port" |
| 118 | + path = "/" |
| 119 | + } |
| 120 | + |
| 121 | + capacity_provider_strategy = [ |
| 122 | + { |
| 123 | + capacity_provider = "FARGATE_SPOT", |
| 124 | + weight = 100 |
| 125 | + } |
| 126 | + ] |
| 127 | + |
| 128 | + task_stop_timeout = 90 |
| 129 | + |
| 130 | + task_mount_points = [ |
| 131 | + { |
| 132 | + "sourceVolume" = aws_efs_file_system.efs.creation_token, |
| 133 | + "containerPath" = "/usr/share/nginx/html", |
| 134 | + "readOnly" = true |
| 135 | + } |
| 136 | + ] |
| 137 | + |
| 138 | + volume = [ |
| 139 | + { |
| 140 | + name = "efs-html", |
| 141 | + efs_volume_configuration = [ |
| 142 | + { |
| 143 | + "file_system_id" : aws_efs_file_system.efs.id, |
| 144 | + "root_directory" : "/usr/share/nginx" |
| 145 | + } |
| 146 | + ] |
| 147 | + } |
| 148 | + ] |
| 149 | +} |
0 commit comments