'AWS SAM template share same policy in multiple functions

I saw this answer but it does not work for me. AWS SAM Multiple Functions with same Inline Policy

when I run sam local start-api it show 'Globals', "'Policies' is not a supported property of 'Function'

this is my sample code

Globals:
  Function:
    Runtime: nodejs14.x
    MemorySize: 128    
    Timeout: 100
    Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - 'dynamodb:GetItem'
              - 'dynamodb:PutItem'
              - 'dynamodb:Scan'
              - 'dynamodb:Query'
              - 'dynamodb:DeleteItem'
              - 'dynamodb:Update*'
            Resource: 'arn:aws:dynamodb:us-xxxx-1:xxxxxxx:table/xxxxxxxx'

is there any way to declare global policy



Solution 1:[1]

According to AWS SAM Globals documentation, Policies is not currently supported in the Globals > Function section.

However, you can define a role like

CommonFunctionsRole:
  Type: AWS::IAM::Role
  Properties:
    Path: /
    AssumeRolePolicyDocument:
      Version: '2012-10-17'
      Statement:
        - Effect: Allow
          Action:
            - 'dynamodb:GetItem'
            - 'dynamodb:PutItem'
            - 'dynamodb:Scan'
            - 'dynamodb:Query'
            - 'dynamodb:DeleteItem'
            - 'dynamodb:Update*'
          Resource: 'arn:aws:dynamodb:us-xxxx-1:xxxxxxx:table/xxxxxxxx'

And reference it inside all your functions like:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Role: !Ref CommonFunctionsRole

I haven't tested this, but you'll get the idea

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leopoldo Varela