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
?
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.
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 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 ).
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.
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.
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.
This is now supported natively in Serverless Framework.
functions: hello: handler: my-function.handler environment: var: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*:*:*'
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With