2

是否有任何类似于Lambda 函数权限的DynamoDBReadPolicy托管策略?ssm:GetParameter*我正在使用 aws-sam-cli 并尝试遵循this,但是当我尝试在 using 时获取参数时sam local start-api,出现以下错误:

InvalidAction: The action or operation requested is invalid. Verify that the action is typed correctly.

这是我尝试获取参数的片段:

const ssm = new AWS.SSM();
const param = {
    Name: "param1",
    WithDecryption: true
};
const secret = await ssm.getParameter(param).promise();

相关的模板部分如下。谢谢!

KeyAlias:
    Type: AWS::KMS::Alias
    Properties:
      AliasName: 'param1Key'
      TargetKeyId: !Ref Key
Key:
    Type: AWS::KMS::Key
    Properties:
      KeyPolicy:
        Id: default
        Statement:
        - Effect: Allow
          Principal:
            AWS: !Sub arn:aws:iam::${AWS::AccountId}:root
          Action:
          - 'kms:Create*'
          - 'kms:Encrypt'
          - 'kms:Describe*'
          - 'kms:Enable*'
          - 'kms:List*'
          - 'kms:Put*'
          - 'kms:Update*'
          - 'kms:Revoke*'
          - 'kms:Disable*'
          - 'kms:Get*'
          - 'kms:Delete*'
          - 'kms:ScheduleKeyDeletion'
          - 'kms:CancelKeyDeletion'
          Resource: '*'
          Sid: Allow root account all permissions except to decrypt the key
        Version: 2012-10-17

LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../
      Handler: app.lambda
      Runtime: nodejs8.10
      Policies:
      - DynamoDBReadPolicy:
          TableName: !Ref Table
      - KMSDecryptPolicy:
          KeyId: !Ref Key
      # I think I need the ssm policy here
4

1 回答 1

5

可用的 SAM 策略模板列在其 Github 存储库中。这些策略模板均未授予任何 SSM 操作的权限,因此您目前无法使用 SAM 策略模板授予您的 AWS Lambda 函数对 SSM 参数的访问权限。

作为解决方法,您可以手动将所需的策略语句内联添加到您的策略中。那看起来像:

LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../
      Handler: app.lambda
      Runtime: nodejs8.10
      Policies:
      - DynamoDBReadPolicy:
          TableName: !Ref Table
      - KMSDecryptPolicy:
          KeyId: !Ref Key
      - Statement:
        - Action:
            - ssm:GetParameter
          Effect: Allow
          Resource: arn:aws:ssm:region:account-id:parameter/parameter_name

您还应该考虑打开一个拉取请求,为 SAM 添加 SSM 参数访问策略模板,因为这样的模板当然是表达此类权限的更方便的方式。根据我的经验,开发人员非常友好,并且总是欢迎这样的添加。

更新SSMParameterReadPolicyAWS SAM 中现在提供了一个,因此您现在可以简单地执行以下操作,而不是使用解决方法:

LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../
      Handler: app.lambda
      Runtime: nodejs8.10
      Policies:
      - DynamoDBReadPolicy:
          TableName: !Ref Table
      - KMSDecryptPolicy:
          KeyId: !Ref Key
      - SSMParameterReadPolicy:
          ParameterName: parameter_name
于 2018-11-18T14:41:01.720 回答