serverless/serverless

AWS_REGION environment variable doesn't work

jamy015 opened this issue ยท 13 comments

This is a Bug Report

Description

For bug reports:

  • What went wrong? I set the AWS_REGION environment variable to eu-west-1 as described in the documentation, but my API was deployed to us-east-1
  • What did you expect should have happened? The API should have been deployed to the eu-west-1 AWS region
  • What was the config you used? A config without a default region set
  • What stacktrace or error message from your provider did you see?

For feature proposals:

  • What is the use case that should be solved. The more detail you describe this in the easier it is to understand for us.
  • If there is additional config how would it look

Similar or dependent issues:

Additional Data

  • Serverless Framework Version you're using: 1.17.0
  • Operating System: Windows 10 (1703)
  • Stack Trace:
  • Provider Error messages:

I also have reproduced this on my local.
Here is my serverless.yml and a config without default region.

service: aws-nodejs-testestes

provider:
  name: aws
  runtime: nodejs4.3

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get

@pmuens Is this bug or the document go wrong?

Thanks for opening @jamy015 and thanks for jumping in @horike37 ๐Ÿ‘

๐Ÿค” AFAIK we start the Serverless process by setting the stage and region to the options , provider or default values (

this.options.stage = this.options.stage
|| (this.serverless.service.provider && this.serverless.service.provider.stage)
|| 'dev';
this.options.region = this.options.region
|| (this.serverless.service.provider && this.serverless.service.provider.region)
|| 'us-east-1';
).

So the env variables is never evaluated.

Seems like it's a bug or we need to update the docs that this is not supported (yet).

/cc @eahefnawy

hmmm I thought the aws-sdk would pick this up automatically ๐Ÿค”

@eahefnawy @pmuens

I'm doing the deep debugging on this. Seems like that would fix to add the process of picking AWS_REGION env var to the following 2 places.

this.options.region = this.options.region
|| (this.serverless.service.provider && this.serverless.service.provider.region)

https://github.com/serverless/serverless/blob/master/lib/plugins/aws/provider/awsProvider.js#L227-L228

Then, I'm considering the following priority to pick the region config up. What do you think?

  1. CLI option like --region=eu-west-1
  2. AWS_REGION env var
  3. setting in serverless.yml
  4. if nothing to setting, set us-east-1 automatically

@horike37 I'm thinking the env var should go before the cli option, specially that it's an AWS env var, not Serverless.

Eitherway, I know AWS_PROFILE is working, so we can copy the priority from there ๐Ÿ˜Š

Thanks @horike37 ๐Ÿ™Œ

@eahefnawy @horike37 I would think this order makes the most sense:

  1. CLI option like --region=eu-west-1
  2. setting in serverless.yml
  3. AWS_REGION env var
  4. setting in ~\.aws\config (set by the AWS CLI)
  5. if nothing to setting, set us-east-1 automatically

My reasoning behind this is that more specific options should, IMO, get more precedence:

  • the CLI option is the most specific because I'm telling serverless right there on the command line that I want to do that
  • serverless.yml is specific to the service being deployed
  • AWS_REGION is specific to serverless on the machine
  • ~\.aws\config is specific to the AWS config on the machine

Thank you for the opinion @jamy015! surely, the region setting in ~\.aws\config also doesn't work..(I have also tried)
IMO, the env vars for aws-sdk should be given priority over the service setting as well as AWS_PROFILE.

@horike37

Here is my workaround, AWS_DEFAULT_REGION or AWS_REGION is an environment variable, it should be set like this:

provider:
  name: aws
  runtime: nodejs6.10
  region: ${env:AWS_DEFAULT_REGION, 'us-east-1'}

I can report that this also doesn't work for me.

i can confirm the bug is still present on the latest version of serverless framework 1.34

This bug seems to be present in v1.56.1 as well.

I found my way here as a new serverless user, wondering why serverless was ignoring my region setting in ~/.aws/config, even though I clearly specify the profile in serverless.yml.

I totally concur with the 5-levels of source for AWS region that @jamy015 wrote on 20 Jul 2017 (!). New users will expect sls to piggy-back off normal AWS config (including profile semantics), unless specified in the .yml.

AWS and Serverless CLI's never supported and do not support AWS_REGION variable.

Check https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html#envvars-list for lists of all env vars as supported by AWS CLI.

AWS CLI supports AWS_DEFAULT_REGION of which support is broken in Framework, and matter is handled at #2151

AWS_REGION env variable exists in lambda environment, to indicate in which region given lambda is executed (so in Framework codebese you may see it being assigned for local invocations.

I've removed the invalid documentation which referenced this variable with 739045d

Closing as this variable should not and was not intended to have influence on deployment process