Deploy .Net Core Web API to Elastic Beanstalk using AWS CodeBuild & CodePipeline

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.

  1. Create a sample .NET 6 Web API project
  2. Add a buildspec.yml file to our .Net 6 Web API project
  3. Create and configure AWS CodeBuild
  4. Create and configure AWS CodePipeline
  5. Run CodePipeline pipeline and deploy our .Net Web API
  6. Test the deployed API Application

Let's begin.

Step 1. Create a sample .NET 6 Web API project

  1. Open Visual Studio 2022 and click on Create a new project.

    Create a sample .NET 6 Web API project

  2. Select 'ASP.NET Core Web API' project

    Create a sample .NET 6 Web API project

  3. Provide the Project name "demo.api"

  4. Select Framework as .NET 6, keep other configurations with the default selected value, and click on 'Create'.

    Create a sample .NET 6 Web API project

  5. Run and test your demo.api project on localhost

    Create a sample .NET 6 Web API project

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

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

  1. Provide Project configuration and

    Create and configure AWS CodeBuild

  2. 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.

    Create and configure AWS CodeBuild

    Provide your github repository URL and branch name.

    Create and configure AWS CodeBuild

  3. Provide Environment details.

    Create and configure AWS CodeBuild

  4. Provide the path of buildspec file
    Copy the path of buildspec.yml from your GitHub
    Create and configure AWS CodeBuild
    and paste the path in the 'Buildspec name' field

    Create and configure AWS CodeBuild

Leave Batch configuration, Artifacts & Logs section with default settings and click on 'Create build project'.

Run and test your CodeBuild pipeline

Create and configure AWS CodeBuild

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

Create and configure AWS CodePipeline

Step 4.2. Add source stage

  • Note: For source, you might need to configure connection, if not configured earlier.

Create and configure AWS CodePipeline

Step 4.3. Add build stage

Create and configure AWS CodePipeline

Step 4.4. Add deploy stage

Create and configure AWS CodePipeline

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

Run CodePipeline pipeline and deploy our .Net Web API

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.

Test the deployed API Application

To test the deployed API, hit the API endpoint on the browser.

Example: http://<--elasticbeanstalk-url-->/weatherforecast

Test the deployed API Application

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


Similar Articles