Create ECS Service for Amazon EC2 Launch Type

Prepare create-service.json and Create ECS Service for Amazon EC2 Launch Type

Before we setup CodeDeploy Application, we need to create ECS Service, to refer in CodeDeploy configuration.

ECS Service, runs and maintains a desired number of tasks from a specified task definition. If the number of tasks running in a service drops below the desiredCount , Amazon ECS runs another copy of the task in the specified cluster. In addition to maintaining the desired count of tasks in your service, you can optionally run your service behind one or more load balancers. The load balancers distribute traffic across the tasks that are associated with the service.

  1. We need to download create-service_src.json file to create create-service.json - ECS Service definition file.

    cd ~/environment/tripmgmt/
  2. Please verify or change below given environment variables with respective values.

    export TASK_NAME="task-tripmgmt-demo:1"
    export ECS_CLUSTER_NAME="ecs-cluster-tripmgmtdemo"
    export ALB_TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:<<AWS-REGION>>:<<ACCOUNT ID>>:targetgroup/alb-tg-tripmgmtdemo-1/<<UNIQUE ID>>"
    export MULTIPLE_SUBNET_ID='"subnet-xxxx","subnet-xxx"'
    export MULTIPLE_SG='"sg-xxxx","sg-xxxx.","sg-xxxxx"'
    1. TASK_NAME: Task name from previously created Task Definition in Create ECS Task section.

    2. ECS_CLUSTER_NAME: ECS Cluster name from previously created ECS Cluster in Create ECS Cluster section.

    3. ALB_TARGET_GROUP_ARN: ARN of already setup Application Load Balancer’s first Target Group Listner as created in Create Application Load Balancer section.

    4. MULTIPLE_SUBNET_ID: The subnet-ids used in earlier sections for Application Load Balancer setup. It should be of the form "subnet-xx..", "subnet-xx..". You can also use below command to fetch subnet and security groups associated with Application Load Balancer.

      # ALB Subnet Ids under AvailabilityZones->SubnetId(s)
      # ALB Securigy Group under SecurityGroups
      aws elbv2 describe-load-balancers --names tripmgmtdemo-alb
    5. MULTIPLE_SG: Specify Security Groups used in earlier sections (ALB, Autoscaling resources and Aurora DB). It should be of the form "sg-xx..","sg-xx..","sg-xx..". These security groups should allow Task communication from ALB and to other services used (Autoscaling resources & Aurora RDS).

      You can also use below commands to fetch security groups associated with Amazon Aurora RDS and Auto Scaling Launch Configuration.

      # RDS Security Group under VpcSecurityGroups -> VpcSecurityGroupId
      aws rds describe-db-clusters --db-cluster-identifier tripmgmtdb-cluster
      # Auto scaling Launch Configuration Securigy Group under SecurityGroups
      aws autoscaling describe-launch-configurations --launch-configuration-names ASGLaunchConfig-TripmgmtDemo
  3. Create ECS Service from commandline, here in place of serv-tripmgmt-ec2, you can give any other unique name to the service.

    envsubst < "create-service_src.json" > "create-service.json"
    aws ecs create-service --service-name serv-tripmgmt-ec2 --cli-input-json file://create-service.json
  4. You can verify Service created in Amazon ECS console, left navigation bar under Amazon ECS, Clusters and select the cluster name, you can see recently created service, status of the service and tasks. It may take few more minutes if ECS Cluster doesn’t have readily available EC2 instances to deploy service tasks.