Amit
Amit

Reputation: 1680

Serverless Framework - Switch existing application from REST to HTTP in AWS API Gateway

AWS just announced HTTP API support for Amazon API Gateway. There are some very impressive pricing and performance numbers coming in with this new release. Off the top, AWS says that the general cost of using v2 will be 70% cheaper and have 50% lower latency than v1. I'd love to try this out in my existing projects.

I use Serverless framework in my application. How do I convert my existing API to use this new feature? This is what my serverless.yml file looks like:

service: amitsn-blog-api

# Use the serverless-webpack plugin to transpile ES6
plugins:
  - serverless-webpack
  - serverless-offline

# serverless-webpack configuration
# Enable auto-packing of external modules
custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

provider:
  name: aws
  runtime: nodejs10.x
  stage: prod
  region: ap-south-1

  # 'iamRoleStatements' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
      Resource: "arn:aws:dynamodb:ap-south-1:*:*"
    - Effect: Deny
      Action:
        - logs:CreateLogGroup
        - logs:CreateLogStream
        - logs:PutLogEvents
      Resource: "*"

functions:
  # Defines an HTTP API endpoint that calls the main function in create.js
  # - path: url path is /posts
  # - method: POST request
  # - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
  #     domain api call
  # - authorizer: authenticate using the AWS IAM role
  options:
    handler: options.main
    events:
      - http:
          path: posts
          method: options
          cors: true
  create:
    handler: create.main
    events:
      - http:
          path: posts
          method: post
          cors: true
          authorizer: aws_iam
  get:
  # Defines an HTTP API endpoint that calls the main function in get.js
  # - path: url path is /posts/{id}
  # - method: GET request
    handler: get.main
    events:
      - http:
          path: posts/{id}
          method: get
          cors: true
  list:
    # Defines an HTTP API endpoint that calls the main function in list.js
    # - path: url path is /posts
    # - method: GET request
    handler: list.main
    events:
      - http:
          path: posts
          method: get
          cors: true
          integration: lambda
          request:
            template:
              application/json: '{ "postType" : "$input.params(''postType'')" }'
  update:
    # Defines an HTTP API endpoint that calls the main function in update.js
    # - path: url path is /posts/{id}
    # - method: PUT request
    handler: update.main
    events:
      - http:
          path: posts/{id}
          method: put
          cors: true
          authorizer: aws_iam
  delete:
    # Defines an HTTP API endpoint that calls the main function in delete.js
    # - path: url path is /posts/{id}
    # - method: DELETE request
    handler: delete.main
    events:
      - http:
          path: posts/{id}
          method: delete
          cors: true
          authorizer: aws_iam

# Create our resources with separate CloudFormation templates
resources:
  # API Gateway Errors
  - ${file(resources/api-gateway-errors.yml)}

Upvotes: 1

Views: 399

Answers (1)

Aaron Stuyvenberg
Aaron Stuyvenberg

Reputation: 3787

The HTTP API is not yet supported by the Serverless Framework, although we are currently working on it! You can track the effort here: https://github.com/serverless/serverless/issues/7052

Upvotes: 1

Related Questions