|
| 1 | +/* -------------------------------------------------------------------------- */ |
| 2 | +/* Data */ |
| 3 | +/* -------------------------------------------------------------------------- */ |
| 4 | +data "aws_caller_identity" "this" {} |
| 5 | + |
| 6 | +data "aws_availability_zones" "available" { |
| 7 | + state = "available" |
| 8 | +} |
| 9 | + |
| 10 | +/* -------------------------------------------------------------------------- */ |
| 11 | +/* VPC */ |
| 12 | +/* -------------------------------------------------------------------------- */ |
| 13 | +module "vpc" { |
| 14 | + source = "oozou/vpc/aws" |
| 15 | + version = "1.2.5" |
| 16 | + prefix = var.prefix |
| 17 | + environment = var.environment |
| 18 | + account_mode = "spoke" |
| 19 | + |
| 20 | + cidr = "10.0.0.0/16" |
| 21 | + public_subnets = ["10.0.1.0/24", "10.0.2.0/24"] |
| 22 | + private_subnets = ["10.0.3.0/24", "10.0.4.0/24"] |
| 23 | + availability_zone = slice(data.aws_availability_zones.available.names, 0, 2) |
| 24 | + |
| 25 | + is_create_nat_gateway = true |
| 26 | + is_enable_single_nat_gateway = true |
| 27 | + is_enable_dns_hostnames = true |
| 28 | + is_enable_dns_support = true |
| 29 | + is_create_flow_log = false |
| 30 | + is_enable_flow_log_s3_integration = false |
| 31 | + |
| 32 | + tags = var.custom_tags |
| 33 | +} |
| 34 | + |
| 35 | +/* -------------------------------------------------------------------------- */ |
| 36 | +/* ACM */ |
| 37 | +/* -------------------------------------------------------------------------- */ |
| 38 | +module "acm" { |
| 39 | + source = "oozou/acm/aws" |
| 40 | + version = "1.0.4" |
| 41 | + |
| 42 | + acms_domain_name = { |
| 43 | + cms = { |
| 44 | + domain_name = "terraform-test.devops.team.oozou.com" |
| 45 | + } |
| 46 | + } |
| 47 | + route53_zone_name = "devops.team.oozou.com" |
| 48 | + is_automatic_verify_acms = true |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +/* -------------------------------------------------------------------------- */ |
| 53 | +/* Fargate Cluster */ |
| 54 | +/* -------------------------------------------------------------------------- */ |
| 55 | +module "fargate_cluster" { |
| 56 | + source = "oozou/ecs-fargate-cluster/aws" |
| 57 | + version = "1.1.0" |
| 58 | + # Generics |
| 59 | + prefix = var.prefix |
| 60 | + environment = var.environment |
| 61 | + name = var.name |
| 62 | + |
| 63 | + # IAM Role |
| 64 | + ## If is_create_role is false, all of folowing argument is ignored |
| 65 | + is_create_role = true |
| 66 | + allow_access_from_principals = ["arn:aws:iam::${data.aws_caller_identity.this.account_id}:root"] |
| 67 | + additional_managed_policy_arns = [] |
| 68 | + |
| 69 | + # VPC Information |
| 70 | + vpc_id = module.vpc.vpc_id |
| 71 | + |
| 72 | + # ALB |
| 73 | + is_create_alb = true |
| 74 | + is_public_alb = true |
| 75 | + enable_deletion_protection = false |
| 76 | + alb_listener_port = 443 |
| 77 | + is_ignore_unsecured_connection = true |
| 78 | + public_subnet_ids = module.vpc.public_subnet_ids |
| 79 | + is_create_alb_dns_record = true |
| 80 | + alb_certificate_arn = module.acm.certificate_arns["cms"] |
| 81 | + route53_hosted_zone_name = "devops.team.oozou.com" |
| 82 | + fully_qualified_domain_name = "terraform-test.devops.team.oozou.com" |
| 83 | + |
| 84 | + |
| 85 | + tags = var.custom_tags |
| 86 | +} |
| 87 | + |
| 88 | +/* -------------------------------------------------------------------------- */ |
| 89 | +/* Service */ |
| 90 | +/* -------------------------------------------------------------------------- */ |
| 91 | +module "api_service" { |
| 92 | + source = "../.." |
| 93 | + |
| 94 | + prefix = var.prefix |
| 95 | + environment = var.environment |
| 96 | + name = format("%s-api-service", var.name) |
| 97 | + |
| 98 | + # ECS service |
| 99 | + task_cpu = 1024 |
| 100 | + task_memory = 2048 |
| 101 | + ecs_cluster_name = module.fargate_cluster.ecs_cluster_name |
| 102 | + service_discovery_namespace = module.fargate_cluster.service_discovery_namespace |
| 103 | + is_enable_execute_command = true |
| 104 | + application_subnet_ids = module.vpc.private_subnet_ids |
| 105 | + security_groups = [ |
| 106 | + module.fargate_cluster.ecs_task_security_group_id |
| 107 | + ] |
| 108 | + additional_ecs_task_role_policy_arns = [ |
| 109 | + "arn:aws:iam::aws:policy/AmazonSSMFullAccess" |
| 110 | + ] |
| 111 | + |
| 112 | + # ALB |
| 113 | + alb_listener_arn = module.fargate_cluster.alb_listener_http_arn |
| 114 | + alb_host_header = null |
| 115 | + alb_paths = ["/*"] |
| 116 | + alb_priority = "100" |
| 117 | + vpc_id = module.vpc.vpc_id |
| 118 | + health_check = { |
| 119 | + interval = 20, |
| 120 | + path = "/", |
| 121 | + timeout = 10, |
| 122 | + healthy_threshold = 3, |
| 123 | + unhealthy_threshold = 3, |
| 124 | + matcher = "200,201,204" |
| 125 | + } |
| 126 | + |
| 127 | + is_create_cloudwatch_log_group = true |
| 128 | + |
| 129 | + container = { |
| 130 | + main_container = { |
| 131 | + name = format("%s-%s-%s-api-service", var.prefix, var.environment, var.name) |
| 132 | + image = "nginx" |
| 133 | + cpu = 128 |
| 134 | + memory = 256 |
| 135 | + is_attach_to_lb = true |
| 136 | + port_mappings = [ |
| 137 | + { |
| 138 | + # If a container has multiple ports, index 0 will be used for target group |
| 139 | + host_port = 80 |
| 140 | + container_port = 80 |
| 141 | + protocol = "tcp" |
| 142 | + } |
| 143 | + ] |
| 144 | + entry_point = [] |
| 145 | + command = [] |
| 146 | + } |
| 147 | + } |
| 148 | + environment_variables = { |
| 149 | + main_container = { |
| 150 | + THIS_IS_ENV = "ENV1", |
| 151 | + THIS_IS_ENVV = "ENVV", |
| 152 | + } |
| 153 | + side_container = { |
| 154 | + XXXX = "XXXX", |
| 155 | + XXXXX = "XXXXX", |
| 156 | + } |
| 157 | + } |
| 158 | + secret_variables = { |
| 159 | + main_container = { |
| 160 | + THIS_IS_SECRET = "1xxxxx", |
| 161 | + THIS_IS_SECRETT = "2xxxxx", |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + target_tracking_configuration = { |
| 166 | + policy_type = "TargetTrackingScaling" |
| 167 | + name = "cpu-average" |
| 168 | + capacity = { |
| 169 | + min_capacity = 1 |
| 170 | + max_capacity = 10 |
| 171 | + } |
| 172 | + scaling_behaviors = { |
| 173 | + predefined_metric_type = "ECSServiceAverageCPUUtilization" |
| 174 | + target_value = 60 |
| 175 | + scale_in_cooldown = 180 |
| 176 | + scale_out_cooldown = 60 |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + tags = var.custom_tags |
| 181 | +} |
0 commit comments