A developer is deploying a new AWS Lambda function that reads data from an Amazon DynamoDB table. What is the AWS-recommended best practice for authorizing the Lambda function to perform this action?
- AAdd the DynamoDB read permissions directly to the trust policy of the Lambda function's IAM execution role to allow the function to perform actions on the table.
- Assign an IAM execution role to the Lambda function with a permission policy that grants DynamoDB read access, and initialize the AWS SDK client without specifying static credentials.Answer
- CEmbed the AWS access key ID and secret access key directly in the Lambda function's source code during the initialization of the AWS SDK client.
- DConfigure the Lambda function's environment variables to store the AWS access key ID and secret access key, and load them into the AWS SDK client at runtime.
Answer
Assign an IAM execution role to the Lambda function with a permission policy that grants DynamoDB read access, and initialize the AWS SDK client without specifying static credentials.
The correct approach is to assign an IAM execution role to the AWS Lambda function. The permission policy attached to this role should grant the minimum required permissions (such as read access to the specific DynamoDB table). When the Lambda function runs, the AWS SDK automatically retrieves temporary security credentials provided by the execution role. This avoids the need to manage or store long-lived AWS credentials in the function code or configuration.
Step-by-Step Solution
Key Concept
IAM Execution Role and AWS SDK Default Credential Provider Chain