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

Support returning promises from serverless.js #4827

Merged
merged 1 commit into from
Nov 27, 2018

Conversation

simonbuchan
Copy link
Contributor

What did you implement:

From discussion in #4787 this is the low-hanging fruit that still adds useful functionality.

For the (undocumented) use of serverless.js as a config file, it is quite useful to be able to return a promise for a config, since nearly all node APIs and packages that do something interesting are async.

As a simple example, looking up VPC configuration by name.

How did you implement it:

Fortunately, promises (including bluebird) are specified that if a promise-like value (ie. has a function-typed then property) would be used to resolve a promise, it gets unwrapped and the resolution value is instead used (or if it rejects, the outer promise rejects also). This handles the "might be a promise, might not be" case straightforwardly.

Oddly, I found the config is loaded in at least two places. Seems like something wiggy happened here?

How can we verify it:

The new test is a rather unrealistic example, here's a more full example that pulls a VPC configuration

const EC2 = require('aws-sdk/clients/ec2');

// async functions return Promises
async function getVpcConfig() {
  const ec2 = new EC2({
    region: 'ap-southeast-2',
  });

  const vpcResponse = await ec2.describeVpcs({
    Filters: [
      { Name: 'tag:aws:cloudformation:stack-name', Values: ['test-core'] }
    ],
  }).promise();

  if (!vpcResponse.Vpcs.length) {
    throw new Error('Could not find test VPC');
  }

  const vpcId = vpcResponse.Vpcs[0].VpcId;

  const [sgResponse, subnetResponse] = await Promise.all([
      ec2.describeSecurityGroups({
        Filters: [
          { Name: 'vpc-id', Values: [vpcId] },
          { Name: 'tag:aws:cloudformation:logical-id', Values: ['securityGroupDb'] },
        ],
      }).promise(),

      ec2.describeSubnets({
        Filters: [
          { Name: 'vpc-id', Values: [vpcId] },
          { Name: 'tag:Name', Values: ['*Public*'] },
        ],
      }).promise(),
  ]);

  return {
    securityGroupIds: sgResponse.SecurityGroups.map(sg => sg.GroupId),
    subnetIds: subnetResponse.Subnets.map(sn => sn.SubnetId),
  };
}

async function getConfig() {
  const vpc = await getVpcConfig();

  return {
    service: 'promise-example',

    provider: {
      name: 'aws',
      runtime: 'node6.10',
    },

    functions: {
      example: {
        handler: 'src/example.default',
        vpc,
      },
    },
  };
}

module.exports = getConfig();
$ node_modules/serverless/bin/serverless print
service: promise-example
provider:
  name: aws
  runtime: node6.10
functions:
  example:
    handler: src/example.default
    vpc:
      securityGroupIds:
        - sg-SNIP
      subnetIds:
        - subnet-SNIP
        - subnet-SNIP
    events: []
    name: promise-example-dev-example

Todos:

  • Write tests
  • Write documentation - we don't have serverless.js docs yet?
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@kevinrambaud
Copy link
Contributor

@pmuens what is the state of this PR? I'd love to have it available.

@thomasmichaelwallace
Copy link
Contributor

(Thought I'd help out and review- seems ok to me- although turns out it can't be a review from just anyone!)

@deftomat
Copy link

Any news? We would really appreciate this feature.

@simlu
Copy link

simlu commented Nov 16, 2018

@deftomat Take a look at yaml boost. I don't think it currently supports promises, but it would be very easy to add.

To clarify, using yaml boost increases the complexity quite a bit, but it gives you basically unlimited power over your serverless config.

We've been using this for a long time now (since creation) and it works really, really well for us (we have about 20 different sls services we deploy with two stacks each).

Disclaimer: I'm the author, so might be a bit biased ;)

@deftomat
Copy link

@simlu Thanks, but we are happy with "everything" in JS. The only missing thing is promise support.

@dschep
Copy link
Contributor

dschep commented Nov 19, 2018

@deftomat I'll try to include this in the next feature release. I'm holding off on merging right now because I wan't to merge and release a fix bugfix for #5497 when I figure out what is wrong there.

@dschep dschep merged commit dd4a49d into serverless:master Nov 27, 2018
@simonbuchan
Copy link
Contributor Author

Thanks for merging this!

@simonbuchan simonbuchan deleted the config-promises branch November 28, 2018 05:28
@mrashidse
Copy link

I still have this issue in 2021

@pgrzesik
Copy link
Contributor

@mrashidse What version are you on?

@mrashidse
Copy link

mrashidse commented Apr 14, 2021

@pgrzesik

Serverless: Running "serverless" installed locally (in service node_modules)
Framework Core: 2.35.0 (local)
Plugin: 4.5.3
SDK: 4.2.2
Components: 3.8.2

@pgrzesik
Copy link
Contributor

Could you share a minimal reproducible example that you're having trouble with? It should be supported without any issues.

@mrashidse
Copy link

mrashidse commented Apr 15, 2021

@pgrzesik thanks for your response but It is working fine now, basically problem was something else. It was returning nothing because there is not logs exist for the function. Due to some reason my function was unable to create the log but when I check with other function, it is working perfectly fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants