serverless-operations / serverless-lambda-edge-pre-existing-cloudfront

A Serverless Framework plugin to create your Lambda@Edge against a pre-existing CloudFront.
Other
56 stars 17 forks source link

Deploy to CloudFront distribution based on stages #14

Closed AllegraChen closed 4 years ago

AllegraChen commented 4 years ago

I hope to deploy to different existing CloudFront distributions based on different stages. However, this plugin does not seem to provide me with that extra layer of functionality. The validStages property that we can configure at the custom section does not do the work since we are not able to set the validStages property for each preExistingCloudFront object.

custom:
  lambdaEdgePreExistingCloudFront: # this configuration applies to every preExistingCloudFront distribution
    validStages:
      - staging
      - production

Therefore, I was thinking that I could open a pull request to add that extra feature. I hope that we could have a stage property for each preExistingCloudFront object under the event array like this:

functions:
  viewerRequest:
    handler: lambdaEdge/viewerRequest.handler
    events:
      - preExistingCloudFront:
          distributionId: xxxxxxx
          eventType: viewer-request
          pathPattern: '*' 
          includeBody: false
          stage: dev # specify the stage when the lambda function is deployed to this cloudfront distribution

See detailed implementation in PR #15

jonasbarsten commented 4 years ago

We use the "serverless-plugin-ifelse" to define if the lambda@edge should be deployed or not and yaml logic to define the distribution IDs:

custom:
  cloudFrontDistIds:
      dev: xxx
      prod: yyy
  cloudFrontDistId: ${self:custom.cloudFrontDistIds.${self:custom.stage}, self:custom.cloudFrontDistIds.dev}
  ...
  serverlessIfElse:
    - If: '"${self:custom.stage}" != "${self:custom.resourcesStage}"'
      Exclude:
        - functions.lambdaEdgeTest
AllegraChen commented 4 years ago

Hi @jonasbarsten, thank you for your solution. That is a very interesting way of solving this problem! I would definitely use that if-else plugin next time if needed.

However, I think that this serverless-lambda-edge-pre-existing-cloudfront plugin would be more powerful and easier to use with this feature. As your solution suggests, we need to depend on another plugin. Besides, we need to write some more code by using the if-else plugin whereas the configuration for my solution is simpler, easier to understand, and easier to maintain.

Therefore, I would suggest @horike37 to add that feature in. :)

jonasbarsten commented 4 years ago

Yeah, makes sense 👍