Paullo
Paullo

Reputation: 2127

ClientError: An error occurred (AccessDeniedException)

This error occurs in AWS Lambda Function when I attempt to put_item() into the DynamoDB.

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

Everything works well expect that after the WS connection, I want to save the Connection ID to DB and a call to put_item() fails with the error below:

[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

I will appreciate any clue on how to fix this

Upvotes: 2

Views: 4494

Answers (1)

Marcin
Marcin

Reputation: 238081

Your DdB table is called my_table_name:

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

and your Policy allows access to only this table:

      - DynamoDBCrudPolicy:
          TableName: !Ref WSConnectionTable

However, the error message says that you are trying to access different table (called, my_table_name_dev) and you get access deny

Upvotes: 4

Related Questions