This project is a template for local development on an API and an Angular application that work in tandem. After working locally, you can also use this same template to deploy a serverless API and to host an Angular application both on AWS with just a few simple CLI commands.
- The API is served through ASP.NET Core WebAPI and uses hardcoded weather values. The API uses all the familiar aspects of WebAPI locally, and the API switches to use the AWS AspNetCoreServer package when running in an AWS Lambda function behind API Gateway.
- The Angular application has Angular Material already configured and included.
This project needs the following installed:
The project can be started with the single command: dotnet run
. The .csproj
file has been modified to automatically start the Angular developer server included in the Angular CLI (ng serve
). The build tools will print out to the command line the localhost
URLs for both the API and the Angular application after the project starts. The Angular developer server automatically watches for changes in the Angular project and will restart the server to reflect those changes. Any changes to the WebAPI server, however, will require a manual restart of the local server by stopping and reissuing dotnet run
.
The Angular project is configured to use http://localhost:5000
as the base URL for making API calls when running locally. You can edit this setting in ClientApp > src > environments > environment.ts
.
In addition to being a template for working locally and serverlessly without any code changes, there are additional build tool scripts and enhancements meant to demonstrate various ways to automate deployment in a .NET environment with AWS.
When deploying this solution, we need to deploy the API before the front end, at least for the first deployment.
For this deployment method, you'll need the AWS Toolkit for Visual Studio and the AWS Tools for PowerShell installed.
- When you're ready to deploy, right click on the project and select
Publish to AWS Lambda
. - You can select to create a new S3 Bucket to store the compiled C# code, or you can use an existing Bucket.
- Give the CloudFormation stack a descriptive name. The stack name you enter needs to match the
StackName
variable provided in the.csproj
file. By default,serverless-angular-api
is used in the.csproj
file provided. - When CloudFormation finishes provisioning resources, you can view the new stack in the AWS Toolkit for Visual Studio.
For this deployment method, you'll need the Node.js and NPM, the Angular CLI, and the AWS Tools for PowerShell installed.
- There is an included PowerShell script for deploying the front end application. Right click on the script and select
Open with PowerShell ISE
. - You may need to change the
ExecutionPolicy
to execute the script. Use the following command:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
- Click the
Play
button inPowerShell ISE
and the script will execute. After a successful execution, the command line will print the URL for your deployed Angular application. The URL should match the following pattern: http://%s3_bucket_name%.s3-website.%aws_region%.amazonaws.com.
- Create an S3 Bucket to hold your code.
- AWS Console: Create an S3 Bucket
- AWS CLI:
aws s3 mb s3://%bucket_name%
- AWS Tools for PowerShell:
New-S3Bucket -BucketName %bucket_name%
- In the root of the project, run
dotnet lambda deploy-serverless -sb %bucket_name% -sn %stack_name% --region %aws_region%
. Make sure the stack name you use matches with what's defined in the.csproj
file. By default,serverless-angular-api
is used as the stack name in the.csproj
file provided.
- Run
dotnet msbuild /t:DeployNgToAWS
. - If you're on a Unix-based system, there's also a
deploy_ng_to_aws.sh
script file you can run. - After a successful execution, the command line will print the URL for your deployed Angular application. The URL should match the following pattern: http://%s3_bucket_name%.s3-website.%aws_region%.amazonaws.com.
Each of the various methods for building and deploying the Angular application in the ClientApp
folder involves the following steps:
- Builds all front end assets with
npm build -- --prod
which usesng build --prod
. - Creates a new S3 Bucket to host your front end assets.
- Enables the S3 Bucket to host a static website.
- Removes any existing files from the S3 Bucket.
- Copies all files from the
ClientApp > dist
build folder to the S3 Bucket. - Locates and prints the public URL to your static website.
When inspecting the .csproj
file, you'll notice several custom build steps for the MSBuild engine. MSBuild is the underlying tool used when calling dotnet build
or running Build
within Visual Studio. The additional MSBuild steps in the .csproj
file go through all the requirements for building and deploying the Angular application in the ClientApp
folder to the public Internet. The MSBuild requires the AWS CLI
to be installed on whatever system you run it on.
To start the front end custom MSBuild steps, target the DeployNgToAWS
step. For example, when using the dotnet
tool, use the command dotnet msbuild /t:DeployNgToAWS
. This method is well paired with build and deployment pipeline tools like CodeBuild and CodePipeline.