Creating Serverless application using SAM Part2

There are two ways to create API gateway resource using SAM template. These two types are called explicit API definition and implicit API definition.

An explicit API gateway creation involves defining the resource explicitly with type
AWS::Serverless::API
In this type we give the API resource some name and thus we have an handle to the API.

An implicit API gateway involves defining the end points for API as part of the serverless function event definition. There is no separate API gateway resource defined. As part of SAM deployment, all the endpoints are collected and a new API gateway resource is automatically created by the SAM.

Implicit API Gateway
Using our previous example where we created lambda function, we add few more properties as shown in the below snippet which creates an API gateway resource





Explicit API Gateway example

As we see that we don't have control with implicit API gateway. Also it is not possible to create a custom stage name for implicit API. The default stage name is prod. This name cannot be deleted or renamed.
With explicit API gateway definition we have more control towards configuration of API. The default stage name created by SAM for this type is called stage. It cannot be deleted or renamed. However we can create our own stage with custom name in addition to the default value.

As discussed in part1 of this blog series, we can use global section configuration to define various gateway resource properties like cors headers, method settings and end point configuration.

API gateway needs more configuration unlike  lambda function. For example, we need to specify what is the input content type for API end point. The format that the API will produce and the parameters expected in the API. We might want to configure path parameters, caching, input transformation, response transformation, handling of error code and how the lambda is tied to the API.

To provide detailed configuration and documentation of the end points we use swagger file. SAM template provides both the option to include the swagger definition body as part of the template or externalize the content to some s3 bucket location and reference that as an URI





Comments

Popular posts from this blog

Integrate an API with lambda - Part 5

Custom authorizer

Integrate an API with lambda - Part 3