Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using an existing API Gateway, the APIs created are not pointing to the alias #189

Open
ziadloo opened this issue Apr 18, 2020 · 0 comments

Comments

@ziadloo
Copy link

ziadloo commented Apr 18, 2020

Consider the two main responsibilities of the plugin:

  • Create a lambda function with an alias.
  • Make an API Gateway entry (like GET /user) which points to that specific alias of the lambda functions (like users:dev)

This works fine if your service fits into one serverless.yml file. But if it does not, based on the documentation easiest way is to manually split your file. Here's an example:

api-gateway/serverless.yml:

service:                              serverless-test

provider:
  name:                               aws
  runtime:                            nodejs12.x
  endpointType:                       REGIONAL

plugins:
- serverless-aws-alias

resources:
  Resources:
    ServerlessTest:
      Type:                           AWS::ApiGateway::RestApi
      Properties:
        Name:                         ServerlessTest

  Outputs:
    apiGatewayRestApiId:
      Value:
        Ref:                          ServerlessTest
      Export:
        Name:                         SlsTest-restApiId

    apiGatewayRestApiRootResourceId:
      Value:
        Fn::GetAtt:
          - ServerlessTest
          - RootResourceId
      Export:
        Name:                         SlsTest-rootResourceId

users/serverless.yml:

service:                        serverless-test-users

provider:
  name:                         aws
  runtime:                      nodejs12.x
  endpointType:                 REGIONAL
  apiGateway:
    restApiId:
      'Fn::ImportValue':        SlsTest-restApiId
    restApiRootResourceId:
      'Fn::ImportValue':        SlsTest-rootResourceId

plugins:
- serverless-aws-alias

functions:
  users:
    handler:                    src/code.users
    events:
      - http:
          path:                 /user
          integration:          lambda
          method:               get
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "list_users" }'
      - http:
          path:                 /user
          integration:          lambda
          method:               post
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "create_user", "payload": $input.body }'

Both these files could be deployed like this (one at a time):

$ sls deploy --region us-east-1 --stage dev --alais dev

And they have to be deployed in the given order. The first one just creates the API Gateway and outputs its ID so it can later be picked up by the second service. The problem is when I deploy the second service, while it creates the lambda function, its alias and the API entry /user but the API entry is pointing to the lambda function without the alias.

Of course, if there's only one file everything will be created as it should and the API entry /user will invoke the lambda function with alias users:dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant