当我单独测试第二个lambda时,它将记录插入到dynamoDB中。
然后我有另一个lambda试图调用它,但似乎什么也没发生。
Cloudwatch日志显示没有问题,并在调用后显示puts语句。
但是dynamodb表没有插入任何记录。
两个Lambda都有完整的Lambda访问权限
第二个lambda可以独立工作:
# lambda 'two'
require 'aws-sdk-lambda'
require 'aws-sdk-dynamodb'
require 'time'
require 'json'
def handler(event:, context:)
p "index2"
dynamodb = Aws::DynamoDB::Client.new(region: 'us-east-2')
item = {
"name": "#{Time.now.to_s[0..19]} #{event['Records'][0]['eventSource']} #{event['Records'][0]['eventName']}",
"eventTime": "#{Time.now.to_s[0..19]}",
"eventSource": "lambda",
"description": "from lambda event",
"eventID": "#{event['Records'][0]['eventID']}",
"eventName": "#{event['Records'][0]['eventName']}",
"eventSource":"#{event['Records'][0]['eventSource']}",
"awsRegion":"#{event['Records'][0]['awsRegion']}"
}
params = {
table_name: 'any_event',
item: item
}
begin
dynamodb.put_item(params)
puts 'Added item'
rescue Aws::DynamoDB::Errors::ServiceError => error
puts 'Unable to add item:'
puts error.message
end
end
第一个尝试调用它的Lambda:
require 'aws-sdk-lambda'
require 'time'
require 'json'
def lambda_handler(event:, context:)
client = Aws::Lambda::Client.new(region: 'us-east-2')
payload = '{"name": "thing"}'
the_payload = JSON.generate(payload)
resp = client.invoke({
function_name: 'LamdaLamdaDynamodb',
invocation_type: 'RequestResponse',
log_type: 'None',
payload: the_payload
})
end
答案 0 :(得分:2)
lambda:InvokeFunction
动作。invocationType=RequestResponse
(其默认类型)。AWSJavaScriptSDK invoke_resp = LAMBDA_CLIENT.invoke(
FunctionName='second_lambda',
InvocationType='RequestResponse',
Payload=encoded_payload)
invocationType
如下RequestResponse (default)
,Event
和DryRun
。aws docs取决于您的同步调用还是异步调用。答案 1 :(得分:1)
IMO:[3]是最简单的方法。您也可以尝试将function_name参数设置为Lambda函数的ARN。