Automatic Deployment .NET Web API to AWS Elastic Beanstalk using GitHub Action
Tags: aws, elasticbeanstalk, githubactions, continuousdeployment
Published: 2023-07-28 03:05:33

Hello everyone!

I want to share how to automatically deploy .NET Web API to AWS Elastic Beanstalk. We will use GitHub Action as a deployment tool.

Please take a look at my strategy for the deployment process.
1. Publish the .NET Application
2. Zip the published .NET Application
3. Set up the EB (Elastic Beanstalk) CLI
4. Deploy with `eb deploy --staged`

## Infrastructure Provisioning

Thanks to [SAGAR/SHANKY](https://automateinfra.com/2021/03/24/how-to-launch-aws-elastic-beanstalk-using-terraform/) for providing a great tutorial to provision AWS Elastic Beanstalk using Terraform. Please take a look into at their article. If you are curious my adoption, please take a look [here](https://github.com/bervProject/tf-beanstalk-dotnet-6/tree/main/infra).

If you are unsure how to use the Terraform, no worries. I would like to explain Terraform and AWS Elastic Beanstalk in the next article. You may provision AWS Elastic Beanstalk manually. 

## Setting up GitHub Action

As mentioned at the beginning of my strategy, you may need to find these detailed steps of the Pipeline. You may need to check in the [repository](https://github.com/bervProject/tf-beanstalk-dotnet-6/blob/main/.github/workflows/deploy.yaml).

```yaml
name: Deploy .NET
on:
  push:
    branches:
    - main
jobs:
  build_deploy:
    name: Build & Deploy
    runs-on: ubuntu-latest
    steps:
    - name: Checkout source code
      uses: actions/checkout@v2
    - uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '6.0.x'
    - name: Publish
      run: dotnet publish WebApi -o site
    - name: Generate deployment package
      run: cd site; zip -r ../deploy.zip . -x '*.git*'; cd ..
    - name: Upload .NET to artifact
      uses: actions/upload-artifact@v3
      with:
        name: dotnet-zip
        path: deploy.zip
    - name: Setup EB CLI
      run: |
        python --version
        pip --version
        pip install awsebcli --upgrade --user
        eb --version
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v2 # More information on this action can be found below in the 'AWS Credentials' section
      with:
        role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
        role-session-name: ${{ secrets.AWS_ROLE_SESSION_NAME }}
        aws-region: 'ap-southeast-1'
    - name: Deploy to EB
      run: eb deploy dotnet-env-dev --staged
```

Additionally, I added a step of uploading to the artifact to archive the published result.

---

Room of improvement:

* Using multiple stages/jobs
* Setting tools using Docker container, provide the tools of Elastic Beanstalk CLI using Docker, and set that as the action.

## References & Artifacts

* [How to Launch AWS Elastic beanstalk using Terraform](https://automateinfra.com/2021/03/24/how-to-launch-aws-elastic-beanstalk-using-terraform/) thanks to SAGAR/SHANKY.

### Repository

{% github bervProject/tf-beanstalk-dotnet-6 %}

### Deployed Version

![Deployed Version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnlny9i1c4v6kfyfe2td.png)

## Thank you

Thank you for reading my short article. Feel free to give feedback in the comment section. Have a great day!

![Thank you GIF](https://media.giphy.com/media/WS09ex6XM9yYDCeH5c/giphy.gif)