aws / serverless-application-model

The AWS Serverless Application Model (AWS SAM) transform is a AWS CloudFormation macro that transforms SAM templates into CloudFormation templates.
https://aws.amazon.com/serverless/sam
Apache License 2.0
9.36k stars 2.39k forks source link

Proposal: Less server serverless applications #857

Open rhboyd opened 5 years ago

rhboyd commented 5 years ago

I would like to be able to use SAM to directly proxy requests from API Gateway to DynamoDB. It feels like Lambda sits at the center of SAM and almost all of the features are geared towards shuttling data through Lambda with as little friction as possible. If I wanted to create an API that allowed a user to POST some data, which was mapped to a DynamoDB PutItem request, then a lambda processed the stream of changes to DynamoDB, I wouldn't be able to easily do this today with SAM. I would have to create a function that sits behind the API and accepts the item, (maybe processes it), puts it in dynamo, then returns a status code to the client. This can take about 100ms per request and I'm capped at 1000 concurrent executions before my clients start to get error messages. By allowing a direct APIG<->DDB integration, the client gets a response in ~10m, I can batch updates to my Lambda function, and I can build a more event-driven architecture.

I think an awesome approach would be something like:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: 'API Gateway to data store'
Resources:
  StreamProcessor:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: '.'
      Handler: 'routes/root.handler'
      Runtime: 'nodejs8.10'
      Events:
      Events:
        DynamoDB1:
          Type: DynamoDB
          Properties:
            Stream:
              'Fn::GetAtt':
                - Table1
                - StreamArn
            StartingPosition: TRIM_HORIZON
            BatchSize: 100
  Table1:
      Type: AWS::Serverless::SimpleTable
      Properties:
        PrimaryKey:
          Name: id
          Type: String
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
        Events:
          PUT:
            Type: 'Api'
            Properties:
              Path: '/test'
              Method: 'post'
          UPDATE:
            Type: 'Api'
            Properties:
              Path: '/test'
              Method: 'update'

When a user performs a POST request the "id" field from the request body is mapped to the partition key, remaining fields in the body can be mapped to other items, then API Gateway puts the item directly in the Table and returns a response to the user.

keetonian commented 5 years ago

This is an interesting idea. Would be happy to work with someone to implement this. Leaving this up for +1's.

rhboyd commented 5 years ago

@keetonian I have a solution that I'm speaking to the API Gateway team about this week. I'd love to be able to add it to SAM

rhboyd commented 5 years ago

My goal was to make the API Integrations mimic the boto3 sdk calls so if you wanted an API that listed route53 hostedzones, it would look like

GetMethod:

Type: AWS::ApiGateway::Method

'Fn::Transform':

  - Name: SimpleAPI

Properties:

  AuthorizationType: NONE

  RestApiId:

    Ref: RestAPI

  ResourceId:

    Ref: SimpleProxyResource

  HttpMethod: GET

  Integration:

    Credentials:
      Fn::GetAtt:

        - DDBReadRole

        - Arn

    Service: route53

    Action:

     Name: GetHostedZone

     Parameters:

      - Id: "myhostedzonename"

    ResponseMaps:

      # This would map the DelegationSet and HostedZone from the response to an apig response body that looks like

      # {

      #   "myset": [response.DelegationSet],

      #   "hz": [response.HostedZone],

      # }

      Success:

        - DelegationSet: "myset"

        - HostedZone: "hz"

      # this does the same for the InvalidInput exception that may be thrown

      InvalidInput:

        - Message: "error-message"

      # this is behavior to perform for any other exception the service may return

      Failure: #TODO