AWS DevOps & Developer Productivity Blog
Deploying a ASP.NET Core web application to Amazon ECS using an Azure DevOps pipeline
For .NET developers, leveraging Team Foundation Server (TFS) has been the cornerstone for CI/CD over the years. As more and more .NET developers start to deploy onto AWS, they have been asking questions about using the same tools to deploy to the AWS cloud. By configuring a pipeline in Azure DevOps to deploy to the AWS cloud, you can easily use familiar Microsoft development tools to build great applications.
Solution overview
This blog post demonstrates how to create a simple Azure DevOps project, repository, and pipeline to deploy an ASP.NET Core web application to Amazon ECS using Azure DevOps. The following screenshot shows a high-level architecture diagram of the pipeline:
In this example, you perform the following steps:
- Create an Azure DevOps Project, clone project repo, and push ASP.NET Core web application.
- Create a pipeline in Azure DevOps
- Build an Amazon ECS Cluster, Task and Service.
- Kick-off deployment of the ASP.Net Core web application using the newly create Azure DevOps pipeline.
Prerequisites
Ensure you have the following prerequisites set up:
- An Amazon ECR repository
- An IAM user with permissions for Amazon ECR and Amazon ECS (the user will need an access key and secret access key)
Create an Azure DevOps Project, clone project repo, and push ASP.NET Core web application
Follow these steps to deploy a .NET Core app onto your Amazon ECS cluster using the Azure DevOps (ADO) repository and pipeline:
- Login to dev.azure.com and navigate to the marketplace.
- Go to Visual Studio, search for “AWS”, and add the AWS Tools for Microsoft Visual Studio Team Services.
- Create a project in ADO: Provide a project name and choose Create.
- On the Project Summary page, choose Project Settings.
- In the Project Settings pane, navigate to the Service Connections page.
- Choose Create service connection, select AWS, and choose Next.
- Input an Access Key ID and Secret Access Key. (You’ll need an IAM user with permissions for Amazon ECR and Amazon ECS in order to deploy via the Azure DevOps pipeline.) Choose Save.
- Choose Repos in the left pane, then Clone in Visual Studio under Clone to your computer.
- Create a ASP.NET Core web application in Visual Studio, set the location to locally cloned repository, and check Enable Docker support.
- Once you’ve created the new project, perform an initial commit and push to the repository in Azure DevOps.
Creating a pipeline in Azure DevOps
Now that you have synced the repository, create a pipeline in Azure DevOps.
- Go to the pipeline page within Azure DevOps and choose Create Pipeline.
- Choose Use the classic editor.
- Select Azure Repos Git for the location of your code and select the repository you created earlier.
- On the Choose a Template page, select Docker Container and choose Apply.
- Remove the Push an image step.
- Add an Amazon ECR Push task by choosing the + symbol next to Agent job 1. You can search for “AWS” in the Add tasks pane to filter for all AWS tasks.
Now, configure each task:
- Choose the Build an image task and ensure that the action is set to Build an image. Additionally, you can modify the Image Name to your standards.
- Choose the Push Image task and provide the following
- Enter a name under Display Name.
- Select the AWS Credentials that you created in Service Connections.
- Select the AWS Region.
- Provide the source image name, which you can find in the setting for the Build an image task.
- Enter the name of the repository in Amazon ECR to which the image is pushed
- Choose Save and queue.
Build Amazon ECS Cluster, Task, and Service
The goal here is to test up to building the Docker image and ensure it’s pushed to Amazon ECR. Once the Docker image is in Amazon ECR, you can create the Amazon ECS cluster, task definition, and service leveraging the newly created Docker image.
- Create an Amazon ECS cluster.
- Create an Amazon ECS task definition. When you create the task definition and configure the container, use the Amazon ECR URI for the Docker image that was just pushed to Amazon ECR.
- Create an Amazon ECS service.
Go back and edit the pipeline:
- Add the last step by choosing the + symbol next to Agent job 1.
- Search for “AWS CLI” in the search bar and add the task.
- Choose AWS CLI and configure the task.
- Enter a name under Display Name, such as Update ECS Service.
- Select the AWS Credentials that you created in Service Connections.
- Select the AWS Region.
- Input the following command, which updates the Amazon ECS service after a new image is pushed to Amazon ECR. Replace <clustername> and <servicename> with your Amazon ECS cluster and service names.
- Command:
ecs
- Subcommand:
update-service
- Options and parameters:
--cluster <clustername> --service <servicename> --force-new-deployment
- Command:
- Now choose the Triggers tab and select Enable continuous integration with the repository you created.
- Choose Save and queue.
At this point, your build pipeline kicks off and builds a Docker image from the source code in the repository you created, pushes the image to Amazon ECR, and updates the Amazon ECS service with the new image.
You can verify by viewing the build. Choose Pipelines in Azure DevOps, selecting the entry for the latest run, and then the icon under the status column. Once it successfully completes, you can log in to the AWS console and view the updated image in Amazon ECR and the updated service in Amazon ECS.
Every time you commit and push your code through Visual Studio, this pipeline kicks off and builds and deploys your application to Amazon ECS.
Cleanup
At the end of this example, once you’ve completed all steps and are finished testing, follow these steps to disable or delete resources to avoid incurring costs:
- Go to the Amazon ECS console within the AWS Console.
- Navigate to the cluster you created, then choose the Tasks tab.
- Choose Stop all to turn off the tasks.
Conclusion
This blog post reviewed how to create a CI/CD pipeline in Azure DevOps to deploy a Docker Image to Amazon ECR and container to Amazon ECS. It provided detailed steps on how to set up a basic CI/CD pipeline, leveraging tools with which .NET developers are familiar and the steps needed to integrate with Amazon ECR and Amazon ECS.
I hope this post was informative and has helped you learn the basics of how to integrate Amazon ECR and Amazon ECS with Azure DevOps to create a robust CI/CD pipeline.
About the Authors
John Formento is a Solution Architect at Amazon Web Services. He helps large enterprises achieve their goals by architecting secure and scalable solutions on the AWS Cloud.