Search code examples
amazon-web-servicesserverless-framework

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?


Solution

  • 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.