Ted Nyman Ted Nyman

Implementation guide: branch deploys for ECS using Cased

How to deploy to ECS using Cased.
Implementation guide: branch deploys for ECS using Cased

This guide will walk you through setting up branch deploys for your Amazon ECS (Elastic Container Service) application using Cased. We’ll cover the basic steps to get you started with a more robust CI/CD pipeline.

Prerequisites

Step 1: Connect Cased to Your Services

  1. Connect Cased to GitHub:

    • Go to the Cased connections page and select the GitHub option.
    • Click “Install GitHub App” and follow the prompts to give Cased access to your repository.
  2. Connect Cased to AWS:

    • In your AWS console, create a new IAM role named “CasedRole” using the policy provided in the Cased documentation.
    • Copy the Role ARN.
    • In Cased, go to the AWS Connections page and enter the Role ARN and your AWS region.

Step 2: Set Up Your Project in Cased

  1. In Cased, go to the Deploys section and click “Create new project”.
  2. Select your GitHub repository and click “Create Project”.

Step 3: Create Deployment Targets

  1. In your project page, click “Create new target”.
  2. Create two targets: one for staging and one for production. For each:
    • Name the target (e.g., “staging”, “production”)
    • Select a GitHub Actions workflow (we’ll create this in the next step)
    • If it’s the production target, check the “production” box

Step 4: Create a GitHub Actions Workflow

  1. In your GitHub repository, create a new file: .github/workflows/cased-deploy.yml
  2. Use this basic workflow as a starting point:
name: Cased ECS Deploy
on:
  workflow_dispatch:
    inputs:
      branch:
        required: true
      target_name:
        required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          ref: ${{ github.event.inputs.branch }}

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: your-aws-region

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Build, tag, and push image to Amazon ECR
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: your-ecr-repo-name
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

      - name: Update ECS service
        run: |
          aws ecs update-service --cluster your-ecs-cluster --service your-ecs-service --force-new-deployment

      - name: Notify Cased of deployment
        uses: cased/cased-deploy-action@v2
        with:
          branch_name: ${{ github.event.inputs.branch }}
          target: ${{ github.event.inputs.target_name }}
          cased_token: ${{ secrets.CASED_TOKEN }}

Make sure to replace your-aws-region, your-ecr-repo-name, your-ecs-cluster, and your-ecs-service with your actual values.

Step 5: Set Up Secrets

In your GitHub repository settings, add these secrets:

Step 6: Deploy a Branch

Now you’re ready to deploy:

  1. In Cased, go to your project’s deploy page.
  2. Select the target you want to deploy to (e.g., “staging”).
  3. Choose the branch you want to deploy.
  4. Click “Deploy”.

Cased will trigger the GitHub Actions workflow, which will build your Docker image, push it to ECR, update your ECS service, and notify Cased of the deployment.

Step 7: Monitor and Analyze

After deployment:

Conclusion

This setup allows you to easily deploy any branch to your ECS environment through Cased. It provides the benefits of branch deploys - increased safety, easy rollbacks, and better visibility - while leveraging the power of ECS for your containerized applications.

Remember to adjust and expand this workflow based on your specific needs, such as adding more sophisticated testing or approval processes before production deploys.