How do you add permission for a scheduled lambda function to query a DynamoDB table?

0

In my amplify application I need a lambda function to execute once a day and query a Database table for records matching certain criteria and send an e-mail if they are found.

I added a secondary index in my graphql schema with the fields I wanted to use. I then generated a lambda function with amplify and added this statement to the execution policy:

      {
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:Query"
            ],
            "Resource": "arn:aws:dynamodb:us-west-1:#redacted#:table/Request-#redacted#-prod",
            "Effect": "Allow"
        },

I can see the permissions in the created role but when I test the function I get the following error message:

"message":"User: arn:aws:sts::#redacted#:assumed-role/crnLambdaRole0600f8a6-prod/sendReminders-prod is not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:us-west-1:#redacted#:table/Request/index/requestsByDate because no identity-based policy allows the dynamodb:Query action"

How do I go about granting the correct permissions to my lambda function or is there some other reason I might get this error?

2개 답변
2
수락된 답변

Hi, you need to update the policy to allow you access to the index as well as the base table. Modify the Resource part of the policy to include the index:

      {
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:Query"
            ],
            "Resource": [
               "arn:aws:dynamodb:us-west-1:#redacted#:table/Request-#redacted#-prod",
               "arn:aws:dynamodb:us-west-1:#redacted#:table/Request-#redacted#-prod/index/*"
             ],
            "Effect": "Allow"
        },

See more here

profile pictureAWS
전문가
답변함 2년 전
1

Hi There.

I understand you are finding issues when you add permissions for a scheduled lambda function to query a DynamoDB table and you are getting an error :

"message":"User: arn:aws:sts::#redacted#:assumed-role/crnLambdaRole0600f8a6-prod/sendReminders-prod is not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:us-west-1:#redacted#:table/Request/index/requestsByDate because no identity-based policy allows the dynamodb:Query action"

Please note that for a DynamoDB index, the resource path is different. So you need to add the index path as well in iamRoleStatements.

For example:

{

        "Action": [

            "dynamodb:GetItem",

            "dynamodb:Query"

        ],

    “Resource”: 

"arn:aws:dynamodb:${self:provider.region}::table/${self:provider.environment.DYNAMODB_DATA}/index/”,

        "Effect": "Allow"

    },

For further understanding please refer to the document [1]

Please also note that the following need to be confirmed

Your Lambda function execution role and that you have the necessary permissions for DynamoDB table on AWS Managed policy "AmazonDynamoDBFullAccess" attached to the role.

Then further check you have permissions boundary attached to the role and it only has the table ARN permissions. Because you are querying on the index, you will be adding "arn:aws:dynamodb:${self:provider.region}::table/${self:provider.environment.DYNAMODB_DATA}/index/” to the resource as mentioned in the permissions boundary policy.

I hope you find this information helpful.

=========References==========

[1] https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/using-identity-based-policies.html

Mfanelo
답변함 2년 전
  • Thank you for your answer, both you and Leeroy had the information I needed. I'm accepting Leeroy's answer since it is more succinct.

  • Thank you, I am happy that you have resolved your issue.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠