ClientError:发生错误(AccessDeniedException)

时间:2020-09-26 01:45:57

标签: amazon-web-services aws-lambda yaml amazon-cloudformation

当我尝试将_item()放入DynamoDB时,AWS Lambda函数中会发生此错误。

c_table.put_item(Item={"id": result['conn_id'], "username": payload.get("username"), "created": datetime.utcnow().isoformat()})

WSConnectionTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: my_table_name
    AttributeDefinitions:
      - AttributeName: id
        AttributeType: S
    KeySchema:
      - AttributeName: id
        KeyType: HASH
    BillingMode: PAY_PER_REQUEST
    StreamSpecification:
      StreamViewType: NEW_AND_OLD_IMAGES

ConnectFunction:
  Type: AWS::Serverless::Function
  Properties:
    FunctionName: !Sub ${AWS::StackName}-ConnectFunction
    Description: !Sub
      - Stack ${StackTagName} Environment ${EnvironmentTagName} Function ${ResourceName}
      - ResourceName: ConnectFunction
    CodeUri: sources
    Handler: handlers.connection
    Runtime: python3.7
    MemorySize: 3008
    Timeout: 30
    Tracing: Active
    Policies:
      - AWSXRayDaemonWriteAccess
      - DynamoDBCrudPolicy:
          TableName: !Ref WSConnectionTable
    Environment:
      Variables:
        TABLE_NAME: !Ref WSConnectionTable
        TABLE_ARN: !GetAtt WSConnectionTable.Arn

一切正常,期望在WS连接之后,我想将连接ID​​保存到DB,并且对put_item()的调用失败,并出现以下错误:

[ERROR] ClientError: An error occurred (AccessDeniedException) when calling the PutItem operation: User: arn:aws:sts::74xxxxxxx:assumed-role/websocket-dev-ConnectFunctionRole-1PCCYG1DTLQYW/websocket-dev-ConnectFunction is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:us-east-2:74xxxxxxx:table/my_table_name_dev

对于解决此问题的任何线索,我将不胜感激

1 个答案:

答案 0 :(得分:1)

您的DdB表称为my_table_name

WSConnectionTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: my_table_name

并且您的策略仅允许访问该表:

      - DynamoDBCrudPolicy:
          TableName: !Ref WSConnectionTable

但是,错误消息表明您正在尝试访问其他表(称为my_table_name_dev),并且您拒绝访问