Skip to content

Deploy an ECS Fargate service

Steps

In this example, the source code in the directory Kattehotell.Api/ is deployed to your AWS account:

GitHub repository containing your source code
.
└── Kattehotell.Api/
    ├── ...
    └── Dockerfile

This ECS Fargate is deployed as part of the application Kattehotell.

Step 1: Prepare an ECR repository

  1. Navigate to repository SharedKontoInfrastruktur
  2. Navigate to ./Shared/<application-name>/eu-west-1/ecr/main.tf
  3. Update locals with new entries:
    ./Shared/kattehotell/eu-west-1/ecr/main.tf
    locals {
      kattehotell_repos = toset([
        ...
        "api",
      ])
    
      account_access = [
        local.accounts["BYM-DP-Kattehotell-Test"].id, 
        local.accounts["BYM-DP-Kattehotell-Prod"].id 
      ]
    }
    
    module "kattehotell_ecr" {
      for_each         = local.kattehotell_repos                                    
      source = "git@github.com:BYM-IKT/terraform-aws-ecr.git?ref=v3"
      application_name = "kattehotell"                                    
      service          = each.value
      account_access   = local.account_access 
    }
    
    output "kattehotell_ecr_repo" {
      value = module.kattehotell_ecr
    }
    
  4. Create a Pull Request to Terraform apply this change. Terraform provisions a new ECR repository with the name ${application_name}-${service}. In this example, the resulting ECR repository receives the name kattehotell-api.

Step 2: Build and push Docker Image

Before you can proceed, the ECR repository cannot be empty. To populate it with an image, navigate to your product's repository, and do the following steps:

  1. Create a GitHub Actions workflow file .github/workflows/deploy-to-test.yml:

    .
    ├── .github/
    │   └── workflows/
    │       └── deploy-to-test.yml
    └── Kattehotell.Api/
        ├── ...
        └── Dockerfile
    
    with the following content:
    ./.github/workflows/deploy-to-test.yml
    name: Deploy ECS Fargate to TEST
    
    on:
      push:
        branches: [master]
      workflow_dispatch:
    
    jobs:
      build_push_image_to_shared:
        name: Build image and publish to ECR
        runs-on: ubuntu-latest
        permissions:
          id-token: write
          contents: read
        steps:
          - name: "Kattehotell.Api"
            uses: BYM-IKT/github-actions/build-and-push-image-to-ecr@master
            with:
              dockerfile-path: Kattehotell.Api/Dockerfile
              aws-account-id:  "<<AWS_ACCOUNT_ID>>"        
              ecr-name:        kattehotell-api
              image-tags:      latest
    
      deploy_images_to_ecs_service_test:
        needs: [build_push_image_to_shared]
        name: "Deploy to TEST"
        uses: BYM-IKT/github-actions/.github/workflows/deploy-image-to-ecs.yml@master
        with:
          environment:           testing
          aws-account-id-target: "<<AWS_ACCOUNT_ID>>"
          ecr-name:              kattehotell-api
          image-tag-target:      latest
          image-tag-new:         test
          ecs-cluster-name:      kattehotell
          ecs-service-name:      api
    
    where <<AWS_ACCOUNT_ID>> is replaced with the Id of your AWS Account.

  2. Run the pipeline, which builds and pushes your Docker image to your ECR repository.

Why did deploy_images_to_ecs_service_test crash?

This pipeline crashes when running deploy_images_to_ecs_service_test as the ECS Fargate service does not exist yet. This is fixed in the next steps.

Step 3: Create an ECS Fargate service

In your product's Infrastraktur-repo, provision your ECS Fargate service referencing the ECR repository's URI as follows:

main.tf
module "application" {
  source = "git@github.com:BYM-IKT/terraform-byks-module.git?ref=v12"
  environment = var.environment
  ...
  ecs_services = {
    api = {
      ...
      ecr_uri   = data.terraform_remote_state.shared_ecr.outputs["api"]
      image_tag = var.environment
    }
  }
}

Create a Pull Request to Terraform apply this change.

Terraform provisions a new ECS Fargate service named ${environment}-${application_name}-${service}. In this example, the resulting name of the ECS Fargate service is api and it resides in the kattehotell cluster.

Step 4: Retry application pipeline

The whole pipeline created in Step 2 should work now. Try rerunning it.