1

I am trying to implement versioning on my API's. The aim is to have different URL endpoints for two different API versions which point to 2 different versions of my lambda.

Solution I can think of is to have 2 API endpoints Example :

  1. https://abcd.aws.com/v1 - GET method
  2. https://abcd.aws.com/v2 - GET method

serverless.yml

service: versions

provider:
  name: aws
  runtime: python3.7
  versionFunctions: false
  stage: ${opt:stage, 'dev'}
  region: us-east-1
  timeout: 30
  memorySize: 256
  deploymentBucket: ${ssm:${self:service}/${self:provider.stage}/sls/deployment/bucket}
  role: ${ssm:${self:service}/${self:provider.stage}/lambda/role}
  apiName: ${self:service}-api-${self:provider.stage}
  vpc:
    securityGroupIds:
      - ${ssm:${self:service}/${self:provider.stage}/lambda/sgId}
    subnetIds:
      - ${ssm:${self:service}/${self:provider.stage}/lambda/subnetA}

custom:
  serverless-layers:
    functions:
      - hello-lambda
    dependenciesPath: ./requirements.txt

  standardResponseHeaders:
    'Access-Control-Allow-Origin': "'*'"
    'Access-Control-Allow-Credentials': "'true'"
    'Access-Control-Allow-Headers': "'*'"

plugins:
  - serverless-layers
  - serverless-plugin-multiple-responses
  - serverless-api-stage

package:
  individually: true
  exclude:
    - '**/*'
    - serverless.yaml

functions:
  hello-lambda: ${file(config/functions/hello-lambda.yml):function1}

My lambda function

import os
import json
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

def handler(event, context):
    logger.info("Event: {}".format(json.dumps(event)))
    response = {
        "statusCode": 200,
        "body": "Hello from the {}!".format(os.environ.get('SERVICE_NAME'))
    }
    return response

Within my serverless.yml have 2 different function templates, like

function1:
  handler: src/helloLambda.handler
  name: ${self:service}-hello-lambda-${self:provider.stage}
  description: testing API versioning - ${self:provider.stage}
  events:
    - http:
        path: /hello/lambda/v1
        method: GET
        integration: lambda

function2:
  handler: src/helloLambda.handler
  name: ${self:service}-hello-lambda-${self:provider.stage}
  description: testing API versioning - ${self:provider.stage}
  events:
    - http:
        path: /hello/lambda/v2
        method: GET
        integration: lambda

You can see in both my function templates, I am pointing it to the same lambda function. How can change that to point to the same lambda with a different version?

DV82XL
  • 5,350
  • 5
  • 30
  • 59
ashakshan
  • 419
  • 1
  • 5
  • 17
  • 1
    I am not sure how to do it using serverless framework, but in cloudformation you can create two alias for v1 and v2 and in the api gateway method integration URI you can pass the arn of aliases. You can also use api gateway stage variables as mentioned in this blog (https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variables-to-manage-lambda-functions/) – nirvana124 Mar 17 '21 at 05:55

0 Answers0