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

Problem Running on AWS Lambda #98

Closed
vectorhacker opened this issue Sep 16, 2022 · 17 comments
Closed

Problem Running on AWS Lambda #98

vectorhacker opened this issue Sep 16, 2022 · 17 comments
Assignees
Labels
bug Something isn't working

Comments

@vectorhacker
Copy link

Describe the bug

I'm getting an error when running unleash proxy behind a lambda function of it not being able to use a functions utils.forOwn. I'm running the proxy behind a lambda function using the serverless-http package.

Steps to reproduce the bug

  1. Setup an http lambda function
  2. Write this code
import unleash from '@unleash/proxy';
import { LogLevel } from '@unleash/proxy/dist/logger';
import { APIGatewayProxyHandlerV2 } from 'aws-lambda';
import serverless from 'serverless-http';

const app = unleash.createApp({
  unleashUrl: process.env.UNLEASH_URL,
  unleashInstanceId: process.env.UNLEASH_INSTANCE_ID,
  unleashAppName: process.env.UNLEASH_APP_NAME,
  clientKeys: [process.env.UNLEASH_PROXY_CLIENT_KEYS!],
  unleashApiToken: process.env.UNLEASH_API_TOKEN,
  refreshInterval: 1000,
  logLevel: LogLevel.debug,
  enableOAS: true,
  proxyBasePath: '/unleash',
});

export const handler: APIGatewayProxyHandlerV2 = serverless(app);
  1. Make any /unleash/proxy call

Expected behavior

A list of toggles

{
  "toggles": [
    {
      "name": "string",
      "enabled": true,
      "impressionData": true,
      "variant": {
        "name": "string",
        "enabled": true,
        "payload": {
          "type": "string",
          "value": "string"
        }
      }
    }
  ]

Logs, error output, etc.

{"error":"Whoops! We dropped the ball on this one (an unexpected error occurred): utils.forOwn is not a function"}

Screenshots

No response

Additional context

I'm utilizing the sst framework, hosting the endpoint behind a /unleash/{proxy+} endpoint through an AWS Api Gateway HTTP API

Unleash version

0.10.4

Subscription type

Open source

Hosting type

Self-hosted

SDK information (language and version)

No response

@vectorhacker vectorhacker added the bug Something isn't working label Sep 16, 2022
@vectorhacker
Copy link
Author

I'm able only to view the open API ui

@vectorhacker vectorhacker changed the title utils.forOwn is not a function in aws lambda Problem Running on AWS Lambda Sep 21, 2022
@sighphyre
Copy link
Member

Hey @vectorhacker I'm not sure what's causing this off the top of my head but I'll add it to our triage for next week

@sighphyre
Copy link
Member

I want to say this is closed through an unrelated fix, but we'll need to set this up in a lambda and poke it a bit to confirm

@vectorhacker
Copy link
Author

@sighphyre thanks for letting me know!

@vectorhacker
Copy link
Author

@sighphyre any update on this?

@sighphyre
Copy link
Member

Hey @vectorhacker, not at the moment, I'm actually on leave at the moment but I've asked a team mate to look into this one tomorrow. Otherwise I'll pick it up on Wednesday

@gastonfournier
Copy link
Contributor

Hi @vectorhacker one quick question about Setup an http lambda function. How are you setting it up? Manually from AWS console or through a serverless?

If it's the latter, maybe you can share the serverless config (after removing the environment values that have your secrets). That'd help me to reproduce this.

@gastonfournier
Copy link
Contributor

Hi @vectorhacker, I managed to run the proxy using the following code and the config from https://github.com/Unleash/unleash-proxy:
sls-lambda-test.zip

  1. Unzip
  2. Run yarn to download package dependencies
  3. Run serverless deploy
  4. Test curl -H 'Authorization: proxy-secret' -H 'Accept: application/json' <endpoint>/unleash/proxy (note it may take some attempts until the proxy connects with Unleash server)

It seems your setup is slightly different and you might be using Typescript, so let me know if this helps you

@vectorhacker
Copy link
Author

Hi @vectorhacker one quick question about Setup an http lambda function. How are you setting it up? Manually from AWS console or through a serverless?

If it's the latter, maybe you can share the serverless config (after removing the environment values that have your secrets). That'd help me to reproduce this.

We're using a serverless framework to deploy it along side our app. We use sst.

@vectorhacker
Copy link
Author

Hi @vectorhacker, I managed to run the proxy using the following code and the config from https://github.com/Unleash/unleash-proxy: sls-lambda-test.zip

  1. Unzip
  2. Run yarn to download package dependencies
  3. Run serverless deploy
  4. Test curl -H 'Authorization: proxy-secret' -H 'Accept: application/json' <endpoint>/unleash/proxy (note it may take some attempts until the proxy connects with Unleash server)

It seems your setup is slightly different and you might be using Typescript, so let me know if this helps you

I'll set it up similarly, we use typescript.

@vectorhacker
Copy link
Author

Is there a reason why you pinned serverless-http to version 2.7.0?

@vectorhacker
Copy link
Author

The same problem, getting {"error":"Whoops! We dropped the ball on this one (an unexpected error occurred): utils.forOwn is not a function"}

@vectorhacker
Copy link
Author

This is our proxy stack

import { StackContext, use } from '@serverless-stack/resources';
import { ApiStack } from './Api';

export function UnleahProxyStack({ stack }: StackContext) {
  const { api } = use(ApiStack);

  api.addRoutes(stack, {
    'ANY /unleash/{proxy+}': {
      authorizer: 'none',
      function: 'unleash-proxy/proxy.handler',
    },
  });
}

This is the function handler

import { createApp } from '@unleash/proxy';
import { LogLevel } from '@unleash/proxy/dist/logger';
import serverless from 'serverless-http';

const unleashApp = createApp({
  unleashUrl: 'https://app.unleash-hosted.com/demo/api/',
  unleashApiToken: '56907a2fa53c1d16101d509a10b78e36190b0f918d9f122d',
  clientKeys: ['proxy-secret'],
  logLevel: LogLevel.debug,
  unleashAppName: 'default',
  environment: 'development',
  proxyBasePath: '/unleash',
});

export const handler = serverless(unleashApp);

@gastonfournier
Copy link
Contributor

Is there a reason why you pinned serverless-http to version 2.7.0?

No particular reason, probably it was there from the example I used to have it up and running. I tested with 3.0.3 but this was not the problem

I set myself to testing with SST, and I managed to reproduce the bug, but the bad news is I have no idea yet what's causing this. I'll have to involve the team to see what's going on.

The "good" news is that the issue starts with 0.10.0 of the proxy, so as a workaround you should be able to use 0.8.1 until we resolve the issue. Please, if you do test with that version of the proxy, do let us know the result.

@gastonfournier
Copy link
Contributor

Hi @vectorhacker, long story short, it seems this is a problem with dependency resolution with clone-deep package. The dependency chain goes like this:
<your-app> -> unleash-proxy -> @unleash/express-openapi -> merge-deep -> clone-deep (a deep problem)

I believe the way <your-app> resolves the dependencies is what's causing the issue. If you use yarn (not sure if it works with npm), you can add this to the root package.json from sst:

  "resolutions": {
    "clone-deep": "^4.0.1"
  },

I guess this will be solved if jonschlinkert/merge-deep#21 gets merged, but I don't have high hopes on that because there are similar PRs opened in that repo for years.

Please, let me know if this works for you.

@vectorhacker
Copy link
Author

I'll try it out!

@ivarconr
Copy link
Member

Closing due to inativity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

No branches or pull requests

4 participants