Skip to content

Add support for aws:region and aws:accountId variables - #9662

Merged
pgrzesik merged 1 commit into
masterfrom
add-support-for-region-and-account-id-var-resolution
Jun 30, 2021
Merged

pgrzesik merged 1 commit into
masterfrom
add-support-for-region-and-account-id-var-resolution

Conversation

@pgrzesik

Copy link
Copy Markdown
Contributor

Closes: #9005

@pgrzesik pgrzesik self-assigned this Jun 30, 2021
@pgrzesik
pgrzesik force-pushed the add-support-for-region-and-account-id-var-resolution branch from 501c33e to 9f691e8 Compare June 30, 2021 08:19
@codecov

codecov Bot commented Jun 30, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #9662 (4b9367b) into master (d861d11) will increase coverage by 0.01%.
The diff coverage is 92.42%.

❗ Current head 4b9367b differs from pull request most recent head ead9316. Consider uploading reports for the commit ead9316 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9662      +/-   ##
==========================================
+ Coverage   86.14%   86.15%   +0.01%     
==========================================
  Files         326      327       +1     
  Lines       12439    12483      +44     
==========================================
+ Hits        10715    10755      +40     
- Misses       1724     1728       +4     
Impacted Files Coverage Δ
lib/Serverless.js 71.28% <ø> (ø)
lib/plugins/aws/provider.js 93.75% <ø> (ø)
scripts/serverless.js 52.07% <ø> (ø)
lib/cli/interactive-setup/deploy.js 91.22% <50.00%> (-1.37%) ⬇️
lib/configuration/variables/resolve.js 98.57% <92.68%> (-1.05%) ⬇️
lib/configuration/variables/parse.js 98.95% <100.00%> (+0.01%) ⬆️
lib/configuration/variables/sources/file.js 100.00% <100.00%> (ø)
...on/variables/sources/instance-dependent/get-aws.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d861d11...ead9316. Read the comment docs.

@pgrzesik
pgrzesik requested a review from medikoo June 30, 2021 08:44

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pgrzesik that was fast, and that looks really good!

There's just one thing by spec it was outlined that region should also be resolved in context of aws source (in our internals it's AWS specific property, and I think it's the way it should stay)

if (!stage) stage = 'dev';
return { value: stage };
}
case 'region': {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Region is internally considered as AWS specific. It's also the reason why it was specified to be put at aws: source

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's considered AWS specific, other major providers also have such concept and support it on provider level, however, I agree that it's tricky because it defaults to us-east-1 which is AWS-specific.

Azure: https://github.com/serverless/serverless-azure-functions/blob/c53f535204c467339e8ae1f6edb985c3280bc32c/src/models/serverless.ts#L39
Google: https://github.com/serverless/serverless-google-cloudfunctions/blob/master/provider/googleProvider.js#L107

Given above, do you think we should stick to region being in aws source?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given above, do you think we should stick to region being in aws source?

I believe so. Main reason is that different dictionaries are used in different providers. While with stage we rely on same dictionary no matter what provider is used (dev serves as default for all)

Also provider may introduce their variable source as e.g. azure:region. So we do not block anything here. We're putting it on aws to actually not collide with how region is handled in case of other providers.

@pgrzesik
pgrzesik force-pushed the add-support-for-region-and-account-id-var-resolution branch 2 times, most recently from be47d61 to 22a47ca Compare June 30, 2021 10:37
@pgrzesik pgrzesik changed the title Add support for sls:region and aws:accountId variables Add support for aws:region and aws:accountId variables Jun 30, 2021
@pgrzesik
pgrzesik requested a review from medikoo June 30, 2021 10:43

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. I've just spotted one typo in error message

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In "aws" source

@pgrzesik
pgrzesik force-pushed the add-support-for-region-and-account-id-var-resolution branch from 22a47ca to ead9316 Compare June 30, 2021 10:49
@pgrzesik
pgrzesik requested a review from medikoo June 30, 2021 10:55

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍

@pgrzesik
pgrzesik merged commit 33794ea into master Jun 30, 2021
@pgrzesik
pgrzesik deleted the add-support-for-region-and-account-id-var-resolution branch June 30, 2021 11:03
@ffxsam

ffxsam commented Jun 30, 2021

Copy link
Copy Markdown
Contributor

Awesome, this is great! 🙌

@corydorning

Copy link
Copy Markdown

is this usable in 2.55.0? couldn't find docs on it, but i have the following:

provider:
  name: aws
  region: ${aws:region}

and it gives this error:

Serverless Error ----------------------------------------
 
  Invalid variable reference syntax for variable aws:region. You can only reference env vars, options, & files. You can check our docs for more info.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.15.4
     Framework Version:         2.55.0 (local)
     Plugin Version:            5.4.3
     SDK Version:               4.3.0
     Components Version:        3.15.1

@medikoo

medikoo commented Aug 24, 2021

Copy link
Copy Markdown
Contributor

@corydorning it should work without issues.

In your case it appears that new variable resolver couldn't resolve the configuration, logged the reasoning and variables got resolved by old resolver which doesn't support this source.

Have you inspected why new resolver aborted ? What was the reason printed?

@corydorning

Copy link
Copy Markdown

Here's the full output:

SLS_DEBUG=* && npx sls package
 
 Serverless Error ----------------------------------------
 
  Invalid variable reference syntax for variable aws:region. You can only reference env vars, options, & files. You can check our docs for more info.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.15.4
     Framework Version:         2.55.0 (local)
     Plugin Version:            5.4.3
     SDK Version:               4.3.0
     Components Version:        3.15.1
 
Serverless: Deprecation warning: "provider.region" is not accessible (configured behind variables which cannot be resolved at this stage).
            Starting with next major release, this will be communicated with a thrown error.
            Set "variablesResolutionMode: 20210326" in your service config, to adapt to this behavior now
            More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_VARIABLES_RESOLVER

@medikoo

medikoo commented Aug 24, 2021

Copy link
Copy Markdown
Contributor

@corydorning please read presented warning, it contains the answer

@corydorning

corydorning commented Aug 24, 2021 via email

Copy link
Copy Markdown

@medikoo

medikoo commented Aug 24, 2021

Copy link
Copy Markdown
Contributor

@corydorning provider.region is needed to e.g. resolve AWS sourced variables (e.g. ssm, cf etc), so it's needed to be resolved earlier.

I don't know your configuration so I cannot say precisely for what provider.region is required in this specific case.

If you feel that message is not understood in its form, we're open for suggestions on how to improve it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variables: Introduce access to resolved AWS accountId and region

4 participants