Last active
          September 15, 2018 16:31 
        
      - 
      
 - 
        
Save joaoferrao/d531160e38cf23821731d0d9881344d7 to your computer and use it in GitHub Desktop.  
    Terraform NLB served ECS service
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # Following assumes you already have an ECS Cluster in place | |
| resource "aws_lb" "this" { | |
| name = "some-service-nlb" | |
| load_balancer_type = "network" | |
| internal = false | |
| idle_timeout = "150" | |
| subnets = "${var.subnet_list}" | |
| } | |
| resource "aws_ecs_task_definition" "this" { | |
| family = "${var.task_family_name}" | |
| task_role_arn = "${var.task_definition_role_arn}" | |
| network_mode = "awsvpc" | |
| container_definitions = <<EOF | |
| [ | |
| { | |
| "cpu": 128, | |
| "essential": true, | |
| "image": "postgres:latest", | |
| "memory": 512, | |
| "memoryReservation": 512, | |
| "name": "postgres", | |
| "portMappings": [ | |
| { | |
| "hostPort": ${var.container_port}, | |
| "protocol": "tcp", | |
| "containerPort": ${var.container_port} | |
| } | |
| ] | |
| } | |
| ] | |
| EOF | |
| } | |
| resource "aws_ecs_service" "this" { | |
| name = "${var.app_name}-service" | |
| task_definition = "${var.task_family_name}:latests" | |
| cluster = "${var.ecs_cluster_id}" | |
| desired_count = "${var.service_task_count}" | |
| network_configuration { | |
| subnets = "${var.subnet_list}" | |
| security_groups = ["${var.task_service_security_group}"] | |
| } | |
| deployment_maximum_percent = "${var.deployment_max_percent}" | |
| deployment_minimum_healthy_percent = "${var.deployment_min_healthy_percent}" | |
| health_check_grace_period_seconds = "${var.health_check_grace_period}" | |
| scheduling_strategy = "REPLICA" | |
| load_balancer { | |
| target_group_arn = "${aws_lb_target_group.this.arn}" | |
| container_name = "${var.container_name}" | |
| container_port = "${var.container_port}" | |
| } | |
| } | |
| resource "aws_lb_target_group" "this" { | |
| name = "${var.environment}-${var.base_name}-ecs-current" | |
| port = "${var.container_port}" | |
| protocol = "TCP" | |
| vpc_id = "${var.vpc_id}" | |
| target_type = "ip" | |
| health_check { | |
| protocol = "${var.lb_tg_health_check_protocol}" | |
| } | |
| } | |
| resource "aws_lb_listener" "this" { | |
| load_balancer_arn = "${aws_lb.this.arn}" | |
| port = "${var.container_port}" | |
| protocol = "TCP" | |
| default_action { | |
| type = "forward" | |
| target_group_arn = "${aws_lb_target_group.this.arn}" | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment