serverless: function run causes aws-sdk error 'ConfigError: Missing region in config'

When I run sls function run myFunction -s dev -r eu-west-1 I get the following error:

 [ConfigError: Missing region in config]
  message: 'Missing region in config',
  code: 'ConfigError',
  time: Tue Apr 19 2016 13:41:21 GMT+0100 (BST) } 'ConfigError: Missing region in config\n ...

I can work around it by specifying the region in my code:

let AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});

but that’s not going to work when we go multi-region.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 39 (21 by maintainers)

Most upvoted comments

I ran into the same thing. I think the root cause is the AWS JavaScript SDK not setting a region by default. See here, specifically The AWS SDK for Node.js doesn't select the region by default. Maybe sls could set the region according to the CLI argument of function run?

Note, the region will be set when you deploy to Lambda it is only not set locally. As a workaround I did this which works both locally and when deployed:

// set region if not set (as not set by the SDK by default)
if (!AWS.config.region) {
  AWS.config.update({
    region: 'eu-west-1'
  });
}

I have the same issue, when running offline I have to set AWS_REGION or I get a Failure: Missing region in config error

AWS_DEFAULT_REGION doesn’t do the trick, but AWS_REGION did it!

I still get the error when I set the defaults as you have listed.

Imho the correct way the tool should react:

  • Use the region defined by the CLI parameter as the default parameter
  • Leave it to the users code to override this in the code if you want to talk to other regions

This way we support the default expectation of users but you can of course still do something different.

This happens even when you specify the stage and region, i.e. sls function run -s dev -r eu-west-1. Which accordingly to the doc should execute the deployed code but it also fails with the same missing region in config.

image

Am I wrong in assuming that this should work since I’ve specified what region to use?

I’ve tried a no. of permutations:

  • in project folder:
    • sls function run funcName
    • sls function run funcName -s dev
    • sls function run funcName -s dev -r eu-west-1
    • sls function run funcName -r eu-west-1
  • in function folder:
    • sls function run
    • sls function run -s dev
    • sls function run -s dev -r eu-west-1
    • sls function run -r eu-west-1

got the same error every time…

Setting the AWS_DEFAULT_REGION would work, but that means I’d have to remember to keep changing that when my function is deployed in multiple regions.

Also, maybe I misunderstood the doc, but where I don’t specify a -s shouldn’t it run my code locally? In those cases I get a prompt asking me to pick a staging instead:

image

Let me know if you want me to put together an example project to illustrate this. Appreciate I might have just misunderstood how sls function run is supposed to work, but I feel like I’m following the documentation and not getting the expected behaviour.

Thanks,

Btw a super workaround is to just add the AWS_DEFAULT_REGION env var … which is added by default in the Lambda runtime, and that probably explains why the deployed version works.

Hope that makes sense 😊

Setting env variable AWS_REGION was also working for me

@theburningmonk I just checked your code. I feel that there’s a cleaner way to solve it without using try/catch blocks. I’m gonna look into it and try to figure it out asap! 😊

I think the sls function run command should set the region for you (if aws-sdk package is installed), otherwise anyone who depends on aws-sdk in their function would need to do this in order to run their function locally.