Create ECS Task

Prepare Task Definition and Create ECS Task

A task definition is required to run Docker containers in Amazon Elastic Container Service. You can define multiple containers in a task definition. The parameters that you use depend on the launch type you choose for the task.

  1. First create cloud watch log group, where taskdef is configured to send all logs.

    1. Go to Cloudwatch Console
    2. Select Log Groups,
    3. Click Create log group button,
    4. Log Group Name: enter tripmgmt-demo-ecstask-loggrp
  2. We need to download taskdef_src.json file to create taskdef.json - ECS Task Definition file.

    cd ~/environment/tripmgmt/
       
    wget https://build-first-devops-ecs.workshop.aws/reference-files/taskdef_src.json
    
  3. Please verify or change below given environment variables with respective values.

    export LOG_GROUP=tripmgmt-demo-ecstask-loggrp
    export DB_USERNAME=admin
    export DB_PASSWORD=mysql123
       
    export TASK_EXECUTION_ROLE_ARN="arn:aws:iam::<<AccountID>>:role/ecsTaskExecutionRole"
    export AURORA_MYSQL_RDS_URL="tripmgmtdb-cluster.cluster-<<UNIQUEID.AWS-REGION>>.rds.amazonaws.com"
    export ECR_LATEST_IMAGE_URL="<<AccountID>>.dkr.ecr.<<AWS-REGION>>.amazonaws.com/devops/tripmgmtdemo:latest"
    
    
    1. TASK_EXECUTION_ROLE_ARN: Task Execution Role created in Create IAM Roles section.
    2. LOG_GROUP : As created in above step.
    3. AURORA_MYSQL_RDS_URL: Aurora MySQL RDS Cluster URL as created in Create Aurora MySQL DB section.
    4. DB_USERNAME: Database username set while creating Aurora MySQL DB Cluster.
    5. DB_PASSWORD: Database password set while creating Aurora MySQL DB Cluster.
      Note: This is for dev/test purpose only, in production you can use AWS SSM service, with Spring Boot.
    6. ECR_LATEST_IMAGE_URL: Elastic Container Registry Image URL from Create Container Repository section.
  4. Create Taskdef from commandline

    envsubst < "taskdef_src.json" > "taskdef.json"
    aws ecs register-task-definition --cli-input-json file://taskdef.json
    
  5. Capture the TaskDefinition ARN from output.

  6. Commit taskdef.json in repository.

    git add taskdef.json
    git commit -m "Updated Taskdef"
    
  7. You can also verify Task in Amazon ECS console, left navigation bar under Amazon ECS, Task Definitions.

Prepare AppSpec File

AppSpec file is a YAML-formatted file used by CodeDeploy to manage a deployment.

  1. Download appspec.yaml file.

    cd ~/environment/tripmgmt/
       
    wget https://build-first-devops-ecs.workshop.aws/reference-files/appspec.yaml
    
  2. Edit appspec.yaml file.

    • Replace «TASKDEF_ARN» with previously created Task Definition, it should be of the form arn:aws:ecs:region:accountid:task-definition/taskname:1
  3. Commit appspec.yaml in repository.

    git add appspec.yaml
    git commit -m "Updated appspec file with Taskdef url"
    git push --set-upstream origin master