samgen will generate a skeleton Serverless Application Model (SAM) template from either an existing API Gateway REST API, or an OpenAPI 3.0 spec.
sam --rest-api-id 1a2b3c4d5e --stage dev --project-name myproject
where myproject
is the name of a directory where the SAM template and related assets will be created.
Once the project is created, you will see a template.yaml
file, along with a Lambda function for each of the API Gateway method requests.
For example:
AWSTemplateFormatVersion: 2010-09-09
Description: Template generated by samgen [https://github.com/mrichman/samgen]
Resources:
CreatePetsFunction:
Properties:
CodeUri: functions/createPets
Events:
PostResource:
Properties:
Method: post
Path: /createPets
Type: Api
FunctionName: CreatePets
Handler: app.lambda_handler
Runtime: python3.8
Type: AWS::Serverless::Function
ListPetsFunction:
Properties:
CodeUri: functions/listPets
Events:
GetResource:
Properties:
Method: get
Path: /listPets
Type: Api
FunctionName: ListPets
Handler: app.lambda_handler
Runtime: python3.8
Type: AWS::Serverless::Function
ShowPetByIdFunction:
Properties:
CodeUri: functions/showPetById
Events:
GetResource:
Properties:
Method: get
Path: /showPetById
Type: Api
FunctionName: ShowPetById
Handler: app.lambda_handler
Runtime: python3.8
Type: AWS::Serverless::Function
Transform: AWS::Serverless-2016-10-31
Note that the actual Lambda function implementations are not imported. This is left as an exercise to the developer.
The Lambda functions are stubbed out as Python functions that look like this:
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
}),
}
- OpenAPI 3.0 spec import