Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the AccountId as a variable in a serverless.yml file?

I want to build an ARN in my file dynamically, but I need to get my current AccountId. How can I access it as a variable?

For example:

example: arn:aws:states:${region}:${accountId}:stateMachine:${self:service}-${self:custom.stage}-example 

What is the proper way to reference the current region and accountId?

like image 497
justin.m.chase Avatar asked Mar 05 '17 18:03

justin.m.chase


People also ask

How do I use environment variables in serverless yml?

To reference environment variables, use the ${env:SOME_VAR} syntax in your serverless. yml configuration file. It is valid to use the empty string in place of SOME_VAR . This looks like " ${env:} " and the result of declaring this in your serverless.

What are the valid file type to store variables for serverless Yaml?

yml file: You can store your variables in serverless. yml if they don't contain sensitive data, and then reference them elsewhere in the file using self:someProperty .

What can you do in serverless yml?

What you can do in serverless.yml is: What this says is to use the stage CLI option if it exists, if not, use the default stage (which lives in provider.stage ).

How do I use self reference in yml serverless?

Reference Properties In serverless.yml To self-reference properties in serverless.yml, use the $ {self:someProperty} syntax in your serverless.yml. someProperty can contain the empty string for a top-level self-reference or a dotted attribute reference to any depth of attribute, so you can go as shallow or deep in the object tree as you want.

How do I reference environment variables in a yml file?

Referencing Environment Variables To reference environment variables, use the $ {env:SOME_VAR} syntax in your serverless.yml configuration file. It is valid to use the empty string in place of SOME_VAR.

What are the serverless variables available?

Those values are exposed via the Serverless Variables system and can be re-used with the {sls:} variable prefix. The following variables are available: A random id which will be generated whenever the Serverless CLI is run. This value can be used when predictable random variables are required. The stage used by the Serverless CLI.


2 Answers

This is now supported natively in Serverless Framework.

functions example

  functions:     hello:       handler: my-function.handler       environment:         var: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*:*:*' 

iam role example

  iam:     role:       statements:         - Effect: Allow           Action:             - dynamodb:*           Resource: !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${AWS::StackName}-* 

See Pseudo Parameters Reference for the official docs.

like image 151
V Maharajh Avatar answered Sep 23 '22 23:09

V Maharajh


There's a handy serverless plugin https://www.npmjs.com/package/serverless-pseudo-parameters that adds the ability to reference aws parameters such as region and account id that i've just started using to much success.

like image 36
Sam J Avatar answered Sep 21 '22 23:09

Sam J