serverless-heaven / serverless-aws-alias

Alias support for Serverless 1.x
MIT License
189 stars 68 forks source link

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

Open ziadloo opened 4 years ago

ziadloo commented 4 years ago

Consider the two main responsibilities of the plugin:

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.