【发布时间】:2020-09-16 12:55:40
【问题描述】:
我正在使用 aws-sam-cli 构建 AWS lambda。在函数中,我想访问某个 DynamoDB 表。
我的问题是,当我使用 sam local invoke 命令在本地调用该函数时,该函数返回此错误:ResourceNotFoundException: Requested resource not found
const axios = require('axios')
const AWS = require('aws-sdk')
AWS.config.update({region: <MY REGION>})
const dynamo = new AWS.DynamoDB.DocumentClient()
exports.handler = async (event) => {
const scanParams = {
TableName: 'example-table'
}
const scanResult = await dynamo.scan(scanParams).promise().catch((error) => {
console.log(`Scan error: ${error}`)
// => Scan error: ResourceNotFoundException: Requested resource not found
})
console.log(scanResult)
}
但是,如果我实际上将其 sam deploy 发送到 AWS 并在实际的 Lambda 控制台中对其进行测试,它会正确记录表信息。
{
Items: <TABLE ITEMS>,
Count: 1,
ScannedCount: 1
}
这是预期的行为吗?或者我需要做一些额外的配置才能让它在本地工作吗?我的template.yaml 看起来像这样:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'Example SAM stack'
Resources:
ExampleFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
Policies:
- DynamoDBCrudPolicy:
TableName: 'example-table'
【问题讨论】:
标签: amazon-web-services aws-lambda amazon-dynamodb aws-sam-cli