Amazon ECS Deploy Task Definition

repository·master·Indexed 20 days ago

https://github.com/aws-actions/amazon-ecs-deploy-task-definition

A GitHub Action that automates the registration of new Amazon ECS task definitions and deploys them to an ECS service or as a standalone task. It supports ECS and CodeDeploy deployment controllers, managed EBS volumes, and provides options for service stability verification, ad-hoc task execution, and custom tagging.

Tokens
5K
Snippets
12
Records
18
Agent score
23%

What's inside aws-actions-amazon-ecs-deploy-task-definition

  1. Run ad-hoc ECS tasks

    master

    You can use this action to trigger an ad-hoc ECS task run, which is useful for initialization tasks like database migrations. When run-task: true is used, the service update will not proceed until the ad-hoc task exits successfully if wait-for-task-stopped: true is also set.

    Note: For FARGATE launch types, your task definition must use the awsvpc network mode and you must specify a network configuration (refer to action.yml for details).

        - name: Deploy to Amazon ECS
          uses: aws-actions/amazon-ecs-deploy-task-definition@v2
          with:
            task-definition: task-definition.json
            service: my-service
            cluster: my-cluster
            wait-for-service-stability: true
            run-task: true
            wait-for-task-stopped: true
  2. Deploy to Amazon ECS using the GitHub Action

    master

    Use the aws-actions/amazon-ecs-deploy-task-definition action to register an Amazon ECS task definition and deploy it to an ECS service.

    When running a one-off task (instead of a service), you will typically need to provide subnet IDs, subnet groups, and an assign-public-ip value. Note that assign-public-ip is only applied if a subnet or security group is defined.

    - name: Deploy to Amazon ECS
      uses: aws-actions/amazon-ecs-deploy-task-definition@v2
      with:
        task-definition: task-definition.json
        service: my-service
        cluster: my-cluster
        wait-for-service-stability: true
  3. Update Task Definition with new container images

    master

    To avoid using the latest tag (which prevents easy rollbacks), generate a unique image ID (e.g., using the GitHub commit SHA) for every build.

    Use the aws-actions/amazon-ecs-render-task-definition action to inject the new image ID into your existing task-definition.json before calling the deploy action.

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: my-ecr-repo
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
    
    - name: Fill in the new image ID in the Amazon ECS task definition
      id: task-def
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: task-definition.json
        container-name: my-container
        image: ${{ steps.build-image.outputs.image }}
    
    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v2
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: my-service
        cluster: my-cluster
        wait-for-service-stability: true
  4. Configure AWS CodeDeploy support

    master

    If your ECS service uses the CODE_DEPLOY deployment controller, you must provide additional configuration to the action, including the AppSpec file, the CodeDeploy application name, and the deployment group name.

    Minimal IAM permissions required include ecs:RegisterTaskDefinition, iam:PassRole for the task and execution roles, and several CodeDeploy permissions (codedeploy:GetDeploymentGroup, codedeploy:CreateDeployment, etc.) along with ecs:DescribeServices.

        - name: Deploy to Amazon ECS
          uses: aws-actions/amazon-ecs-deploy-task-definition@v2
          with:
            task-definition: task-definition.json
            service: my-service
            cluster: my-cluster
            wait-for-service-stability: true
            codedeploy-appspec: appspec.json
            codedeploy-application: my-codedeploy-application
            codedeploy-deployment-group: my-codedeploy-deployment-group
  5. Configure Amazon EBS Volumes

    master

    This action supports Amazon EBS volumes for both ECS Services and standalone tasks.

    For Services (Update Service): Requires the task definition to include a volume configured with "configuredAtLaunch": true.

    For Standalone Tasks (RunTask): Requires setting run-task: true and providing the volume configuration via run-task-managed-ebs-volume.

    # Service Deployment with EBS
    - name: Deploy to Amazon ECS with EBS Volume
      uses: aws-actions/amazon-ecs-deploy-task-definition@v2
      with:
        task-definition: task-definition.json
        service: my-service
        cluster: my-cluster
        wait-for-service-stability: true
        service-managed-ebs-volume-name: "ebs1"
        service-managed-ebs-volume: '{"sizeInGiB": 30, "volumeType": "gp3", "encrypted": true, "roleArn":"arn:aws:iam::<account-id>:role/ebs-role"}'
    
    # Standalone Task Deployment with EBS
    - name: Deploy to Amazon ECS
      uses: aws-actions/amazon-ecs-deploy-task-definition@v2
      with:
        task-definition: task-definition.json
        cluster: my-cluster
        run-task: true
        run-task-launch-type: EC2
        run-task-managed-ebs-volume-name: "ebs1"
        run-task-managed-ebs-volume: '{"filesystemType":"xfs", "roleArn":"arn:aws:iam::<account-id>:role/github-actions-setup-stack-EBSRole-YwVmgS4g7gQE", "encrypted":false, "sizeInGiB":30}'
  6. Manage Task Definition files

    master

    It is recommended to treat your task definition as code by checking a JSON file into your git repository. You can manage these files in three ways:

    1. Download an existing definition: Use the AWS CLI to pull the current configuration into a file.
    2. Generate a new skeleton: Use the AWS CLI to create a template file to fill in manually.
    3. Download during workflow: If you don't want to store the file in git, use the AWS CLI within your GitHub Actions workflow to download it on the fly.
    # Download existing task definition
    aws ecs describe-task-definition \
       --task-definition my-task-definition-family \
       --query taskDefinition > task-definition.json
    
    # Generate a new skeleton
    aws ecs register-task-definition \
       --generate-cli-skeleton > task-definition.json
  7. Preserve empty values with keep-null-value-keys

    master

    By default, the action removes empty strings, arrays, and objects from the task definition to align with ECS defaults. If you need to explicitly override a non-null default value with an empty or null value, use keep-null-value-keys.

    Provide a comma-separated list of key names to ensure their empty values are retained during registration.

    - name: Deploy to Amazon ECS
      uses: aws-actions/amazon-ecs-deploy-task-definition@v2
      with:
        task-definition: task-definition.json
        service: my-service
        cluster: my-cluster
        keep-null-value-keys: tag,command,placementConstraints
        wait-for-service-stability: true
  8. Configure deployment retries with max-retries

    master

    To handle transient failures (like eventual consistency or temporary AWS API errors) during task definition registration or service updates, use the max-retries input.

    • Default: 3
    • Minimum: 0 (no retries)
    - name: Deploy to Amazon ECS
      uses: aws-actions/amazon-ecs-deploy-task-definition@v2
      with:
        task-definition: task-definition.json
        service: my-service
        cluster: my-cluster
        max-retries: 5
        wait-for-service-stability: true
  9. Configure polling intervals with wait-max-delay-seconds

    master

    The AWS SDK uses exponential backoff by default, which can cause delays of up to 120 seconds between polls. To use a consistent polling interval and detect service stability faster, set wait-max-delay-seconds to your desired interval in seconds.

        - name: Deploy to Amazon ECS
          uses: aws-actions/amazon-ecs-deploy-task-definition@v2
          with:
            task-definition: task-definition.json
            service: my-service
            cluster: my-cluster
            wait-for-service-stability: true
            wait-max-delay-seconds: 15
  10. Register a new ECS task definition

    master

    The action starts by reading a task definition file (YAML or JSON) from your repository. It automatically cleans the input by:

    1. Removing attributes returned by the ECS API that are invalid for registration (e.g., taskDefinitionArn, revision, status).
    2. Cleaning null or empty values, unless they are explicitly preserved via the keep-null-value-keys input.
    3. Ensuring valid object structures for proxyConfiguration and container environment variables.

    If registration fails, the action will log the exact task definition contents to the debug logs to help troubleshoot schema errors.

  11. Configure ECS Tags

    master

    You can control how tags are applied to your tasks:

    • enable-ecs-managed-tags: Set to true to enable Amazon ECS-managed tags (aws:ecs:serviceName and aws:ecs:clusterName).
    • propagate-tags: Set to SERVICE to propagate custom tags from your existing service to the tasks.
    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v2
      with:
        task-definition: task-definition.json
        service: my-service
        cluster: my-cluster
        wait-for-service-stability: true
        enable-ecs-managed-tags: true
        propagate-tags: SERVICE
  12. Tag ECS tasks

    master

    You can apply tags to your tasks using the following inputs:

    • enable-ecs-managed-tags: Set to true to enable Amazon ECS-managed tags (e.g., aws:ecs:clusterName).
    • run-task-tags: A JSON array of custom tags to apply.
        - name: Deploy to Amazon ECS
          uses: aws-actions/amazon-ecs-deploy-task-definition@v2
          with:
            task-definition: task-definition.json
            service: my-service
            cluster: my-cluster
            wait-for-service-stability: true
            run-task: true
            enable-ecs-managed-tags: true
            run-task-tags: '[{"key": "project", "value": "myproject"}]'
            wait-for-task-stopped: true