Part 8 - Publish Your Code to Lambda as a function

In this task, you will publish your code to AWS Lambda as a function

1. Stop the debugger and open the serverless.template file, expand the Resources section and the child AspNetCoreFunction section and the child Events section, add a comma at the end of line 33 and add a new line after line 33

Note: The curly brackets must match for the code to be well-formed, Visual Studio will give you an indication if your curly brackets are not matching (and the project will not build successfully if that’s the case) this may take a little trial and error to get right but hang in there

2. On the newly created line 34 add the following GetResource code, this will allow GET requests to “/” to return the index.html file

"GetResource": {
	"Type": "Api",
	"Properties": {
	"Path": "/",
	"Method": "GET"
	}
}

Your file should look like this after adding the code:

3. Save and Rebuild the solution, then from the Solution Explorer, right-click the project and select “Publish to AWS Lambda”

4. In Publish AWS Serverless Application UI make sure you have the correct Region selected, enter a Stack Name and either create a new S3 bucket or use an existing (in the same region)

5. Once the publishing process completes a CloudFormation tab will appear in Visual Studio that will allow you to monitor the progress of the deployment, once the deployment is finished the status will change from CREATE_IN_PROGRESS to CREATE_COMPLETE and a URL will appear in the AWS Serverless URL field

6. Copy the AWS Serverless URL into a browser and add a trailing slash/. for example:

https://bonwb5sb76.execute-api.us-east-2.amazonaws.com/Prod/

The initial load will take a few seconds, as it is a cold start for the Lambda function, once the calendar UI is loaded, login and try creating events by clicking on any date

Note: JavaScript here is just to demo the minimum Cognito functionality to support a custom AuthorizationHandler in ASP.NET Core

Note: EventsController is storing events in a static collection. Our Lambda function could experience a “cold start” at any time, erasing any events you create - especially after a few minutes of inactivity. For any real application, you will want to store your events in a database. DynamoDB is a great option for this

Conclusion

Hosting an entire website in Lambda is a fun approach but using Lambda to serve static content isn’t optimal. Lambda cold starts mean the index.html page will be slow to load for users whenever the website hasn’t been visited for more than a few minutes.

To address that you should move static content (html, css and JavaScript) to an Amazon S3 bucket and enable static website hosting. You can also use your own domain for both the API Gateway API and for the S3 bucket setting the domain root yourdomain.com or www.yourdomain.com to point to the S3 bucket and a CNAME like api.yourdomain.com pointing at your API Gateway API. That way your webpage can load immediately and show an updating message to users if the API is responding slowly.

If you have an existing ASP.NET Web API project that you would like to migrate to run in AWS Lambda, rather than starting from a new project template, check out Norm Johanson’s blog, Deploy an Existing ASP.NET Core Web API to AWS Lambda