The following is the SAM YAML template for the Weather Service example. It broadly consists of three components:
- An API Gateway definition that points to the Swagger file for detailed configuration.
- An AWS Lambda function configuration with a link to code the .zip file, handler name, and runtime environment. In order to link this Lambda function with the API gateway instance, it also has details on the REST API methods and the URI paths, which will be used as triggers to the Lambda function.
- The final section is around output, which is basically the URI of the API endpoint once the entire stack has been created by SAM:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A simple cloud-native Microservice sample, including an AWS SAM template with API defined in an external Swagger file along with Lambda integrations and CORS configurations
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
DefinitionUri: ./cloud-native-api-swagger.yaml
StageName: Prod
Variables:
# NOTE: Before using this template, replace the <<region>> and <<account>> fields
# in Lambda integration URI in the swagger file to region and accountId
# you are deploying to
LambdaFunctionName: !Ref LambdaFunction
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./Cloudnative-weather-service.zip
Handler: lambda_function.lambda_handler
Runtime: python3.6
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /weatherservice
Method: get
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /weatherservice
Method: options
The output from the preceding code is as follows:
ApiUrl:
Description: URL of your API endpoint
Value: !Join
- ''
- - https://
- !Ref ApiGatewayApi
- '.execute-api.'
- !Ref 'AWS::Region'
- '.amazonaws.com/Prod'
- '/weatherservice'