AWS DevOps & Developer Productivity Blog
Deploy .NET Blazor WebAssembly Application to AWS Amplify
AWS Amplify is a set of purpose-built tools and features that lets developers quickly and easily build CI/CD Pipeline for full-stack applications on AWS.
Blazor can run your client-side C# code directly in the browser, using WebAssembly. It is a .NET running on WebAssembly, and you can reuse code and libraries from the server-side parts of your application.
Overview of solution
In this post, you will deploy a Blazor WebAssembly Application from git repository to AWS Amplify. We will use .NET 6. to create a Blazor WebAssembly on local machine using AWS Command Line Interface (AWS CLI), use GitHub as a git repository, and deploy the application to Amplify.
Follow this post on: Windows 10, Windows 11/Ubuntu 20.04 LTS/macOS 10.15 “Catalina”, macOS 11.0 “Big Sur”, or macOS 12.0 “Monterey”.
Walkthrough
We will walk through the following steps:
- Create Blazor WebAssembly application on our local machine using AWS CLI
- Test /run the application locally
- Create a new repository on Github
- Create a local repository
- Setup Amplify
- Test /run the application on AWS
Prerequisites
For this walkthrough, you should have the following prerequisites:
- AWS Account – How to create an AWS account
- Dotnet version on Windows/Linux/MacOS – How to install Dotnet version
- Git CLI on Windows/Linux/MacOS – How to install Git CLI
- Github account – How to create a github account
Let’s start creating a Blazor WebAssembly application on our local machine using CLI:
-
- Open the command line interface
- Create a directory for your application running the following command:
mkdir BlazorWebApp
-
- Change to the application directory running the following command:
cd BlazorWebApp
-
- Create the Blazor WebAssembly Application running the following command:
dotnet new blazorwasm
-
- Run the application:
dotnet run
-
- Copy the URL after “Now listening on:”, and paste it on your browser.
Example: http://localhost:5152 (port might be different in your CLI)
- Copy the URL after “Now listening on:”, and paste it on your browser.
-
- After testing your application, go back to the terminal and press <ctrl> + c to stop the application.
- Create a gitignore for your project running the following command:
dotnet new gitignore
-
- Create a file called “amplify.yml” in the root directory of your application. The name must be exactly “amplify.yml”. This file contains the commands to build your application used by AWS CodeBuild.
- Copy and paste the following code to the file amplify.yml.
version: 1
frontend:
phases:
preBuild:
commands:
- curl -sSL https://dot.net/v1/dotnet-install.sh > dotnet-install.sh
- chmod +x *.sh
- ./dotnet-install.sh -c 6.0 -InstallDir ./dotnet6
- ./dotnet6/dotnet --version
build:
commands:
- ./dotnet6/dotnet publish -c Release -o release
artifacts:
baseDirectory: /release/wwwroot
files:
- '**/*'
cache:
paths: []
Create a new repository on Github:
-
-
- Log in to the Github website and create a new repository:
-
-
-
- Type a name for your repository, choose private, and add a read.me file as shown in the following screenshot:
-
Create a local repository for your application:
-
-
- On the root folder of your application enter the following commands. Make sure that you have configured git CLI with email and user
-
git add --all
git commit -m “first commit”
git branch -M main
git remote add origin https://github.com/perinei/blazorToAmplify.git
(replace red text with your repo)
for ssh authentication use:
git remote add origin git@github.com: perinei/blazorToAmplify.git
git push -u origin main
Setup Amplify:
-
-
- Log in to the AWS account
- Go to AWS Amplify Service
- On the left panel, choose All apps
- Select New app as per the following screen
- Select Host Web App from the dropdown list
-
-
-
- Choose Github
-
-
-
- Select Continue. If you are still logged in on your Github account, then the page will automatically authenticate you, otherwise select the Authenticate Button
- Choose your repository: in my case, perinei/bazortoamplify
- Branch: main
- Select next
-
-
-
- Give your app a name
- amplify.yml will be automatically detected and will be used to build the application on AWS
-
-
-
- Select Next to review the configuration
- Select Save and Deploy
- Amplify will provision, build, deploy, and verify the application
-
-
-
- When the process is complete, select the URL of your application and test the application.
-
-
-
- Congratulations! Your Blazor WebAssembly is running on Amplify.
-
Cleaning up
To avoid incurring future charges, delete the resources. On Amplify, choose your app name on the left panel, select action, and then delete app.
Conclusion
Congratulations, you deployed your first Blazor Webassembly Application to AWS Amplify.
In this blog post you learned how to easily build a full CI/CD pipeline for a Blazor WebAssembly using the AWS amplify. It was only necessary to specify the repository and the commands build the application on the file amplify.yml that should be include on the root folder of repository. You can also easily add a custom domain to your application. Visit Set up custom domains on AWS Amplify Hosting
AWS can help you to migrate .NET applications to the Cloud. Visit .NET on AWS.
The .NET on AWS YouTube playlist is the place to get the latest .NET on AWS videos, including AWS re:Invent sessions.
To learn more about how to amplify.yml to build your application, visit Configuring build settings – AWS Amplify.