getlift / lift

Expanding Serverless Framework beyond functions using the AWS CDK
MIT License
912 stars 109 forks source link

Add posibility to modify origins in single-page-app #359

Closed cdominguezg closed 10 months ago

cdominguezg commented 10 months ago

Start from the Use-case

I'm trying to deploy a SPA that make some calls to an API Gateway with Lambda Integration. I need to define the origin in the distribution for these API calls, but when I try to do it through the extensions I got the following message:

Invalid request provided: One or more of your cache behavior target origins or origin groups do not exist.

I think it's due to the default origin or maybe the new origin is not created before setting the CacheBehaviors.

Example Config


service: myservice
provider:
    name: aws

constructs:
    landing:
        type: single-page-app
        path: ../dist
        extensions:
          distribution:
            Properties:
              DistributionConfig:
                Origins:
                  - Id: 'xxxxx'
                    OriginPath: '/dev'
                    DomainName: xxxxx.execute-api.eu-west-1.amazonaws.com
                    CustomOriginConfig: 
                      OriginProtocolPolicy: https-only
                      HTTPSPort: 443
                CacheBehaviors:
                  - ViewerProtocolPolicy: https-only
                    AllowedMethods: ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
                    PathPattern: 'api/*'
                    DefaultTTL: 3600
                    ForwardedValues:
                      Headers:
                      - Access-Control-Request-Headers
                      - Access-Control-Request-Method
                      - Origin
                      - Authorization
                      - Accept-Language
                      QueryString: true
                    TargetOriginId: 'xxxxx'
                Enabled: true
                DefaultRootObject: index.html        

plugins:
    - serverless-lift

Implementation Idea

I think there are two ways to do that.

  1. Allowing to define the default originId (and exporting it). For example:
constructs:
    landing:
        type: single-page-app
        path: ../dist
        defaultOriginId: test
  1. Allowing a list of origins with some behavior (I think this is a better way):
constructs:
    landing:
        type: single-page-app
        path: ../dist
        origins:
          - id: test
            behavior:
                ....

Maybe there is a way to solve that yet, but I've not achieved

mnapoli commented 10 months ago

This might be a duplicate of #82 or #256?

cdominguezg commented 10 months ago

Yeah, @mnapoli, sorry, is corresponding to #256. Anyway, I have the same issue with the origin in the last comment. I've forked the repo and I'm working on it :)

mnapoli commented 10 months ago

👍 got it I'll be closing this one.