Allow Lambda function to interact with DynamoDB

Table of Content

To allow a Lambda function the permissions to interact with a DynamoDB table, you only needs a single line of code.

Add CDK code to grant Lambda function the permissions to interact with DynamoDB table

  • In the CDK stack file (cdk-workshop-stack.ts), inside the constructor and below code for UsersListHandler Lambda function, add the following code:

    table.grantReadData(usersList);
    

    Your code should looks like this:

    alt text

[Optional] Run cdk diff to check the difference of your stack

  • Run cdk diff to see the change set

    cdk diff
    

    alt text

Run cdk deploy to deploy your CDK stack that grant Lambda function permissions

  • Run cdk deploy to deploy your CDK stack with the IAM permissions.

    cdk deploy
    

    alt text

  • Test the UsersListHandler Lambda function again, the function should be invoked succeeded with a 200 status.

    alt text