SAM YAML template

We will use a SAM template to create the serverless stack. SAM uses YAML or JSON, and allows you to define the Lambda function and API Gateway settings, as well as create a DynamoDB table. The template looks as follows:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
  This Lambda is invoked by API Gateway and queries DynamoDB.
Parameters:
    AccountId:
        Type: String
Resources:
  lambdadynamodataapi:
    Type: AWS::Serverless::Function
    Properties:
      Handler: lambda_return_dynamo_records.lambda_handler
      Runtime: python3.6
      CodeUri: ../../package/lambda-dynamo-data-api.zip
      FunctionName: lambda-dynamo-data-api-sam
      Description: >-
        This Lambda is invoked by API Gateway and queries DynamoDB.
      MemorySize: 128
      Timeout: 3  
      Role: !Sub 'arn:aws:iam::${AccountId}:
role/lambda-dynamo-data-api' Environment: Variables: environment: dev Events: CatchAll: Type: Api Properties: Path: /visits/{resourceId} Method: GET DynamoDBTable: Type: AWS::DynamoDB::Table Properties: TableName: user-visits-sam SSESpecification: SSEEnabled: True AttributeDefinitions: - AttributeName: EventId AttributeType: S - AttributeName: EventDay AttributeType: N KeySchema: - AttributeName: EventId KeyType: HASH - AttributeName: EventDay KeyType: RANGE ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1

From top to bottom, we first specify the template type, a description, and pass in a string parameter, AccountId. We then specify the Lambda details, such as Handler, which is the entry point, location of the ZIP code, and give the function a name and description. We then choose 128 MB RAM as this is a proof of concept and we won't need more memory; we specify 3 for the timeout. After this, the Lambda will terminate even if it is still running; this limits costs and is reasonable, since we expect a synchronous response. We then have the IAM Lambda execution role with the ${AccountId} parameter that gets passed in when we deploy the serverless stack.

We saw how to add the environment variable that will be available in the Lambda function. The variable is environment: dev.

We then have the trigger or event source for the Lambda function. Here, we create an API Gateway with a resource in the /visits/{resourceId} path with the GET method that will invoke a Lambda function with resourceId, which will be the EventId.

Finally, we create a DynamoDB table with an EventId hash of the data type string and an EventDay range of data type number using Python. To keep costs down (or free), I've put the read and write capacities to 1.

So in one SAM YAML file, we have configured the Lambda, API Gateway with its Lambda integration, and created a new DynamoDB table.

For DynamoDB, I strongly recommend that you append sam at the end when it is a resource created by SAM, so you know the origin. I also recommend that if a DynamoDB table is shared between services, you create it using Boto3 or the AWS CLI. This is because the deletion of one serverless stack could mean the table is deleted for all services.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset