将内联策略添加到 aws SAM 模板

Add inline policy to aws SAM template

我正在使用 SAM 模板创建我的无服务器应用程序。

使用资源属性下的标签 Policies 我可以添加 standard 策略,如下所示:

Resources:
  QueryFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: query/
      Handler: app.lambda_handler
      Policies:
        - AmazonDynamoDBFullAccess
        - AWSLambdaVPCAccessExecutionRole
      Runtime: python3.7

问题是我需要附加一个内联策略以仅访问特定的 DynamoDB table。

如何将此内联策略放入模板中?

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "dynamodb:*",
            "Resource": "dynamo_db_table_endpoint"
        }
    ]
}

谢谢

试试这个:

QueryFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: query/
      Handler: app.lambda_handler
      Policies:
        - AmazonDynamoDBFullAccess
        - AWSLambdaVPCAccessExecutionRole
        - Version: '2012-10-17' # Policy Document
          Statement:
            - Effect: Allow
              Action:
                - dynamodb:*
              Resource: 'arn:aws:dynamodb:*:*:table/dynamo_db_table_endpoint'
      Runtime: python3.7

Amazon DynamoDB: Allows Access to a Specific Table

如果您想将 tableName 作为参数传递,请将 Resource: 'arn:aws:dynamodb:*:*:table/dynamo_db_table_endpoint' 更改为 Resource: !Sub 'arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tableName}'

希望对您有所帮助