ASP.NET Core on AWS Elastic Beanstalk Docker image through GitHub, CodeBuild, and CodePipeline CI/CD.
At this story, we will see how to create an ASP.NET Core service, build a Docker image with that service, how to process it through AWS Code Pipelines, and automatically deploys it to an Elastic Beanstalk instance.
The ASP.NET Core web project
Run the scripts to above to get a brand new web project.
dotnet new web
Then, feel free to change your endpoints on the Startup.cs file as you want.
The Buildspec file
Create a Buildspec.yml file as below.
version: 0.2phases:pre_build:commands:- dotnet restorebuild:commands:- dotnet publish -o output- mkdir build- cp -R output build/output- cp Dockerfile build/Dockerfileartifacts:files:- '**/*'base-directory: build
What that file defines is:
- Restore the project dependencies into a pre-build phase.
- Publish the project to an output folder
- Create a folder named build
- Copy the output and Dockerfile to inside the build folder.
- Then, it defines all files inside the base directory build to be copied as artifacts.
Create a Dockerfile as below.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1COPY output/* /app/EXPOSE 80ENTRYPOINT ASPNETCORE_URLS=http://*:80 dotnet /app/aspnetcore-api-aws.dll
It starts an image from aspnet:3.1 templates, copy the content on the output folder to inside the container, define the port at 80, and define an startup execution point that runs our aspnetcore application at port 80.
Of course, create a GitHub repository, commit, and push all your code to the master branch.
No more code from that point! :D
The AWS services
The Elastic Beanstalk service
Create a elastic beanstalk application and then a new application environment as a Web server environment.
On platform options, choose Docker as below:
Afterward, it’s gonna be star… Wait for it! …ted.
You must see a website as the following image when accessing your environment URL.
The CodePipeline service
Create a CodePipeline and give a name to it. Select GitHub as a source provider and connect to it. Choose your GitHub project and the master branch.
At the Build Stage section, select AWS CodeBuild as the provider, and at the same flow, create a new CodeBuild project on the popup, with the environment settings as following.
Leave the Buildspec config as it is. Remember! The CodeBuild will use our repository’s Buildspec file.
At the Deploy section, select AWS Elastic Beanstalk as the provider. Also, select the application and environment we previously created.
Review everything and click on Create Pipeline.
Now, let the Pipeline get everything setup.
If everything went well, you will see the Pipeline steps as the image below.
It means your Pipeline worked properly. Just check the environment URL out, to see your service online.
All these tools together provide a good solution for our continuous integration and delivery.
You may need functionalities like blue/green deployment, for example. So, that’s why it worth checking out more functionalities through Elastic Beanstalk instances and Code Pipelines documentations.
If you want to check my GitHub repository used on that example, check it out on the following link.
Contribute to fmacedoo/aspnetcore-api-aws development by creating an account on GitHub.
Thanks you all!