dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 795 forks source link

How to add http proxy event ? #1532

Open erwan-joly opened 2 years ago

erwan-joly commented 2 years ago

Hello, I'm trying to understand how I can handle an http proxy event from lambda using serverless-offline.

The configuration works fine on aws api gateway but I can't find how to make it work locally.

I have a server and can access images using direct address example: http://localhost:9999/image1.png (on aws it's on a s3 bucket with cloudfront address) I would like this to be served by the proxy a request to http://localhost:3000/images/images1.png should serve the image http://localhost:9999/image1.png

Here is a simplified version of my configuration. Is there a way to make it works on serverless-offline currently ? I have resourceRoutes: true as the documentation suggest but it doesn't seems to work. http://localhost:3000/images/images1.png returns nothing

service: myapp
frameworkVersion: "3"
useDotenv: true
variablesResolutionMode: 20210326
disabledDeprecations:
  - CLI_OPTIONS_SCHEMA

provider:
  name: aws
  stackName: myapp
  architecture: arm64
  runtime: nodejs14.x
  region: ${opt:region, 'us-west-2'}
  stage: ${opt:stage, 'prod'}
  logRetentionInDays: 7
  lambdaHashingVersion: 20201221
  versionFunctions: false
  apiGateway:
      binaryMediaTypes:
        - '*/*'

  environment:
    NODE_ENV: ${self:custom.nodeenv.${self:provider.stage}}
    SERVERLESS_PROJECT: ${self:service}
    SERVERLESS_REGION: ${self:provider.region}
    SERVERLESS_STAGE: ${self:provider.stage}

package:
  individually: true

plugins:
  - serverless-webpack
  - serverless-plugin-scripts
  - serverless-offline

functions:
  myapp:
    memorySize: 512
    provisionedConcurrency: 0
    timeout: 6
    handler: handler.serve
    name: myapp
    events:
      - http:
            path: /images/{any+}
            method: ANY
            cors: true
            integration: http-proxy
            request:
              uri: 'http://localhost:9999/{any}'
              parameters:
                paths:
                  any: true
      - http:
          path: /
          method: any
          cors: true

      - http:
          path: /{any+}
          method: any
          cors: true

custom:
  webpack:
    webpackConfig: "webpack.server.config.js"

  serverless-offline:
    resourceRoutes: true
    useChildProcesses: false
    noPrependStageInUrl: true
    httpPort: 3000
    lambdaPort: 3002

resources:
  Resources:
    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: myapp
dnalborczyk commented 2 years ago

thank you for filing the issue @erwan-joly

as far as I know support for binary data types is either only partially implemented or not supported at all yet.

it's on the top of the list to support next.

dnalborczyk commented 2 years ago

possibly related: https://github.com/dherault/serverless-offline/issues/1509

erwan-joly commented 2 years ago

@dnalborczyk issue is not really about binary data types here but the http proxy part. I only get 204 No Content. If I had binary issue I would still expect a content with corrupted data

erwan-joly commented 2 years ago

I tried to fix it myself https://github.com/erwan-joly/serverless-offline/pull/1/files but it seems like my knowledge of this codebase is not big enough. The new seems to return 404