Advance Configuration for AWS SAM template to deploy ASP.net Core Web APIs

Anuj Garg
3 min readNov 2, 2020

--

While working on SAM template for AWS Lambda, I did struggle in finding proper template specification to have more control on resources and took some time to figure it out.

Lets start with some introduction to SAM and I would add references for Lambda Functions and the process to setup ASP.net core Web API for Lambda at the end of Page.

The AWS Serverless Application Model (AWS SAM) is an open-source framework that you can use to build serverless applications on AWS.

A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda function — it can include additional resources such as APIs, databases, and event source mappings.

Read more on SAM at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html

Challenges with SAM: There are several hidden defaults which are added to reduce template complexity. What if we want to use advantages of SAM and also can control default values. The common questions which we see

  1. How to configure API Gateway Stage Name?
  2. Can we add an Authorizer for API Gateway through SAM template?
  3. What if we need to add a Resource Policy to API Gateway?
  4. How to configure Logging for API Gateway?
  5. Can we enable XRay tracing using SAM template?
  6. Can we customize Lambda Function Name?
  7. How to add VPC configuration for lambda?

And Yes, we can do all of it and even more if we can figure out right way to do it.

When you create a new SAM application, it come with a default SAM file something like this

https://github.com/anujgarg06/aws-sam/blob/main/LambdaVPC/defaultSAM.yml

If you look at this default file, you won’t see a definition for Lambda Role, API Gateway and that’s where the challenge start.

Good News: We can still configure these resource explicitly and SAM template can be customized to support it.

Refer this customized SAM template

https://github.com/anujgarg06/aws-sam/blob/main/LambdaVPC/lambdavpc.yml

MYAPI resource definition

We can create “AWS::Serverless::Api” explicitly and configure it according to our need like a custom stage name, authorizer, logging configurations, resource policy and all others which are part of CloudFormation templates.

Another change is needed in Lambda function definition to use this rest API definition. Refer Yellow highlighted text where we need to configure RestAPI logical name.

References for additional Read:

AWS Lambda: AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume. You can look at all feature offerings at https://aws.amazon.com/lambda/features/

ASP.net Core Web API for Lambda: I found this great article exampling to create and configure a web API for lambda.

SAM Repositories:

SAM Specifications:

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification.html

--

--

Anuj Garg
Anuj Garg

Written by Anuj Garg

AWS Serverless experts | Use your products at best

No responses yet