ECS Fargate service
Steps
In this example, we are going to deploy the source code in the directory Kattehotell.Api/ to your AWS account:
We want this ECS Fargate to be deployed as part of the application Kattehotell.
Step 1: Prepare an ECR repository
- Navigate to repository SharedKontoInfrastruktur
- Navigate to
./Shared/<application-name>/eu-west-1/ecr/main.tf - Update
localswith new entries: - Create a Pull Request to Terraform apply this change.
Terraform will provision a new ECR repository with the name
${application_name}-${service}. In our example, the resulting ECR repository will be namedkattehotell-api.
Step 2: Build and push Docker Image
Before you can proceed the ECR repository cannot be empty.
-
Create a GitHub Actions workflow file
.github/workflows/deploy-to-test.yml:with the following content:. ├── .github/ │ └── workflows/ │ └── deploy-to-test.yml └── Kattehotell.Api/ ├── ... └── Dockerfile./.github/workflows/deploy-to-test.ymlwherename: 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@v0 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<<AWS_ACCOUNT_ID>>is replaced with the Id of your AWS Account. -
Run the pipeline, which will build and push your Docker image to your ECR repository.
Why did deploy_images_to_ecs_service_test crash?
This pipeline will crash when running deploy_images_to_ecs_service_test as the ECS Fargate service does not exist yet.
We will fix this in the forthcoming steps.
Step 3: Create an ECS Fargate service
Provision your ECS Fargate service referencing the ECR repository's URI as follows:
module "application" {
source = "git@github.com:BYM-IKT/terraform-byks-module.git"
environment = "test"
...
ecs_services = {
api = {
...
ecr_uri = data.terraform_remote_state.shared_ecr.outputs["api"]
image_tag = "test"
}
}
}
Create a Pull Request to Terraform apply this change.
Terraform will provision a new ECS Fargate service named ${environment}-${application_name}-${service}.
In our example, the resulting name of the ECS Fargate service will be api which resides in kattehotell cluster.
Step 4: Retry application pipeline
The whole pipeline created in Step 2 should work now. Try rerunning it.