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)
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 offunction 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:
I have the same issue, when running offline I have to set
AWS_REGIONor I get aFailure: Missing region in configerrorAWS_DEFAULT_REGIONdoesn’t do the trick, butAWS_REGIONdid it!I still get the error when I set the defaults as you have listed.
Imho the correct way the tool should react:
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.Am I wrong in assuming that this should work since I’ve specified what region to use?
I’ve tried a no. of permutations:
sls function run funcNamesls function run funcName -s devsls function run funcName -s dev -r eu-west-1sls function run funcName -r eu-west-1sls function runsls function run -s devsls function run -s dev -r eu-west-1sls function run -r eu-west-1got the same error every time…
Setting the
AWS_DEFAULT_REGIONwould 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
-sshouldn’t it run my code locally? In those cases I get a prompt asking me to pick a staging instead: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 runis 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_REGIONenv 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_REGIONwas 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 runcommand 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.