Introduction
In this article, we are going to see how to deploy a .Net Core Web API to AWS Elastic Beanstalk service using CI/CD Pipeline. To configure the CI/CD pipeline, we are using AWS CodeBuild & CodePipeline services.
I have divided the deployment process into 6 steps.
- Create a sample .NET 6 Web API project
- Add a buildspec.yml file to our .Net 6 Web API project
- Create and configure AWS CodeBuild
- Create and configure AWS CodePipeline
- Run CodePipeline pipeline and deploy our .Net Web API
- Test the deployed API Application
Let's begin.
Step 1. Create a sample .NET 6 Web API project
- Open Visual Studio 2022 and click on Create a new project.
- Select 'ASP.NET Core Web API' project
-
Provide the Project name "demo.api"
-
Select Framework as .NET 6, keep other configurations with the default selected value, and click on 'Create'.
- Run and test your demo.api project on localhost
Now, our Web API is ready to deploy
Step 2. Add a 'buildspec.yml' file to our .Net 6 Web API project
A buildspec (build specification) is a collection of build commands and related settings in YAML format that CodeBuild uses to run a build. If you include a buildspec as part of the source code, by default, the buildspec file must be named buildspec.yml and placed in the root of your source directory.
Right-click on project > Add new item > select a text file > rename to buildspec.yml
Add the below content to the buildspec.yml file.
# AWS CodeBuild spec to build an Elastic Beanstalk artifact for AWS CodePipeline to deploy
version: 0.2
phases:
install:
runtime-versions:
dotnet: 6.0
pre_build:
commands:
- echo Restore started on `date`
- echo "CURRENT DIR `pwd`"
- dotnet restore demo.api/demo.api/demo.api.csproj
build:
commands:
- echo Build started on `date`
- dotnet build demo.api/demo.api/demo.api.csproj
post_build:
commands:
- echo Publish started on `date`
- dotnet publish -c Release -o ./build-output demo.api/demo.api/demo.api.csproj
artifacts:
name: DEMO_API_Build_#$CODEBUILD_BUILD_NUMBER
base-directory: './build-output'
files:
- ./**/*
This buildspec declaration
- version represents the version of the build spec standard being used. This build spec declaration uses the latest version, 0.2.
- phases represent the build phases during which you can instruct CodeBuild to run commands. These build phases are listed here as install and build. You cannot change the spelling of these build phase names, and you cannot create more build phase names.
After updating the buildspec.yml file, commit your changes to your source control (GitHub, Bitbucket, etc.).
That's it; we are now done with the code changes.
For the next steps, we will work on the AWS console.
Step 3. Create and configure AWS CodeBuild
Go to AWS console > CodeBuild > click on Build Project
- Provide Project configuration and
- Provide Source repository details (in this example, we are using GitHub as our source provider).
Note. For the first time, you will need to create a connection by providing your GitHub access token and save it.
Provide your github repository URL and branch name.
- Provide Environment details.
- Provide the path of buildspec file
Copy the path of buildspec.yml from your GitHub
and paste the path in the 'Buildspec name' field
Leave Batch configuration, Artifacts & Logs section with default settings and click on 'Create build project'.
Run and test your CodeBuild pipeline
The build is successful. Now, let's move on to the next step.
Step 4. Create and configure AWS CodePipeline
Below are the steps to create CodePipeline to deploy our API.
Step 4.1. Choose pipeline settings
Step 4.2. Add source stage
- Note: For source, you might need to configure connection, if not configured earlier.
Step 4.3. Add build stage
Step 4.4. Add deploy stage
Step 4.5. Review
Review your changes and click on 'Create pipeline'.
Once your pipeline has been created, it will start to run automatically.
First, it detects the application code in your source location and then moves it to the build stage that you defined. In this stage, it will build your code and then will move on to the deployment stage. During this stage, it passes the code to Elastic Beanstalk, which contains the EC2 instance that will host your code. Elastic Beanstalk handles deploying the code to the EC2 instance.
Step 5. Run CodePipeline pipeline and deploy our .Net Web API
After your pipeline is created, the pipeline status page appears, and the pipeline automatically starts to run. You can view progress as well as success and failure messages as the pipeline performs each action.
To verify your pipeline ran successfully, monitor the progress of the pipeline as it moves through each stage. The status of each stage will change from No executions yet to In progress, and then to either Succeeded or Failed. The pipeline should complete the first run within a few minutes.
- To deploy manually, click on the 'Release change' button
The application deployed successfully.
Now, let's verify the deployment and test the deployed API.
Step 6. Test the deployed API Application
To verify deployment, go to Elastic Beanstalk Application and check application versions. You should see the application version deployed through the pipeline.
To test the deployed API, hit the API endpoint on the browser.
Example: http://<--elasticbeanstalk-url-->/weatherforecast
Tested successfully!
Conclusion
In this article, we have learned how we can use AWS CodeBuild & CodePipeline services to configure CI/CD pipeline on AWS and how we can deploy .Net Core Web API to an Elastic Beanstalk application using AWS CodeBuild & CodePipeline CI/CD pipeline