我目前在lambda函数和API网关方面遇到了问题。我已经在API网关中启用了代理集成,以便返回Lambda函数的状态代码。现在我有一个问题,调用根本没有执行。错误图片
如果我测试Lambda,一切正常,数据库中的条目将被执行。不通过API网关。
我有什么问题?
非常感谢你的帮助!
测验
const mysql = require('mysql');
var cors = require('cors');
const express = require('express');
const app = express();
app.use(cors());
const connection = mysql.createConnection({
//following param coming from aws lambda env variable
host: process.env.RDS_HOSTNAME,
user: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
port: process.env.RDS_PORT,
// calling direct inside code
connectionLimit: 10,
multipleStatements: true,
// Prevent nested sql statements
connectionLimit: 1000,
connectTimeout: 60 * 60 * 1000,
acquireTimeout: 60 * 60 * 1000,
timeout: 60 * 60 * 1000,
debug: true
});
let query;
let addressId;
let personId;
exports.handler = async (event, context, callback) => {
try{
query="Insert into sys.Address (street, houseNumber, postalCode, city) Values ('"+event.street+"', "+event.houseNumber+", "+event.postalCode+", '"+event.city+"')";
let dataAddress = await new Promise((resolve, reject) => {
connection.query(query, function (err, result) {
if (err) {
console.log("Error->" + err);
reject(err);
}
resolve(result);
});
});
addressId=dataAddress.insertId;
console.log("addressId=" +addressId);
query="Insert into sys.Persons (lastName, firstName, dateOfBirth, phoneNumber, addressId) Values ('"+event.lastName+"', '"+event.firstName+"', '"+event.dateOfBirth+"', "+event.phoneNumber+", "+addressId+")";
let dataPerson = await new Promise((resolve, reject) => {
connection.query(query, function (err, result) {
if (err) {
console.log("Error->" + err);
reject(err);
}
resolve(result);
});
});
var response = {
"statusCode": 201,
"headers": {
"my_header": "my_value"
},
"body": JSON.stringify(dataAddress, dataPerson),
"isBase64Encoded": false
};
callback(null, response);
} catch (err) {
var response = {
"statusCode": 400,
"headers": {
"my_header": "my_value"
},
"body": JSON.stringify(err.message),
"isBase64Encoded": false
};
callback(null, response);
}
};
编辑:
我将此作为IAM政策添加:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"execute-api:*"
],
"Resource": [
"arn:aws:execute-api:*:*:*"
]
}
]
}
但它仍然带来这个错误:
Request: /persons
Status: 400
Latency: 474 ms
Response Body
"ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'field list'"
Response Headers
{"my_header":"my_value","X-Amzn-Trace-Id":"Root=1-60e95617-157c462e040f133437c0add3;Sampled=0"}
Logs
Execution log for request 2ff23dad-8233-4785-944d-1c21c4d92862
Sat Jul 10 08:11:03 UTC 2021 : Starting execution for request: 2ff23dad-8233-4785-944d-1c21c4d92862
Sat Jul 10 08:11:03 UTC 2021 : HTTP Method: POST, Resource Path: /persons
Sat Jul 10 08:11:03 UTC 2021 : Method request path: {}
Sat Jul 10 08:11:03 UTC 2021 : Method request query string: {}
Sat Jul 10 08:11:03 UTC 2021 : Method request headers: {}
Sat Jul 10 08:11:03 UTC 2021 : Method request body before transformations: {
"lastName": "Jan",
"firstName": "Semrau",
"street": "Musterstraße",
"houseNumber": 3,
"postalCode": 12345,
"city": "Musterstadt",
"dateOfBirth": "2021-04-03",
"phoneNumber": 123456789
}
Sat Jul 10 08:11:03 UTC 2021 : Endpoint request URI: https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:983309467882:function:postPerson/invocations
Sat Jul 10 08:11:03 UTC 2021 : Endpoint request headers: {x-amzn-lambda-integration-tag=2ff23dad-8233-4785-944d-1c21c4d92862, Authorization=***************************************************************************************************************************************************************************************************************************************************************************************************************************793b24, X-Amz-Date=20210710T081103Z, x-amzn-apigateway-api-id=jo9o1lyptc, X-Amz-Source-Arn=arn:aws:execute-api:eu-central-1:983309467882:jo9o1lyptc/test-invoke-stage/POST/persons, Accept=application/json, User-Agent=AmazonAPIGateway_jo9o1lyptc, X-Amz-Security-Token=IQoJb3JpZ2luX2VjELf//////////wEaDGV1LWNlbnRyYWwtMSJHMEUCIQCG1vvlhi8qDp+i7JtDFT7BNyEdYevsqonEe0z4jy2g3wIgQ1psCpqYgdQBomcXcUaufcpvVake3KzP2XXKsoH9oG8qiQQIoP//////////ARACGgw0NzQyNDAxNDY4MDIiDGNjM6AtNofwy1P8VyrdA+R4jETkW9/rFwjhU8a9dIsM6DlvJPjJ8ruBUtcl6m/VcYT+P+AkUrLNXwPR9OM2Ruj9RKWEPgfp3kEHyu/s0CDmgHmUmvxnP4eRDSOY1ruBpC4SHpdoDCXj933o1x [TRUNCATED]
Sat Jul 10 08:11:03 UTC 2021 : Endpoint request body after transformations: {"resource":"/persons","path":"/persons","httpMethod":"POST","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"resourceId":"488rd3","resourcePath":"/persons","httpMethod":"POST","extendedRequestId":"CPpjvER7FiAFS7Q=","requestTime":"10/Jul/2021:08:11:03 +0000","path":"/persons","accountId":"983309467882","protocol":"HTTP/1.1","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestTimeEpoch":1625904663802,"requestId":"2ff23dad-8233-4785-944d-1c21c4d92862","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","principalOrgId":null,"cognitoAuthenticationType":null,"userArn":"arn:aws:iam::983309467882:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.11.1030 Linux/5.4.116-64.217.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.292-b10 java/1.8.0_292 vendor/Oracle_Corpora [TRUNCATED]
Sat Jul 10 08:11:03 UTC 2021 : Sending request to https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:983309467882:function:postPerson/invocations
Sat Jul 10 08:11:04 UTC 2021 : Received response. Status: 200, Integration latency: 469 ms
Sat Jul 10 08:11:04 UTC 2021 : Endpoint response headers: {Date=Sat, 10 Jul 2021 08:11:04 GMT, Content-Type=application/json, Content-Length=153, Connection=keep-alive, x-amzn-RequestId=26ba9976-89c5-4e0d-9ae6-f7340ab0c040, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-60e95617-157c462e040f133437c0add3;sampled=0}
Sat Jul 10 08:11:04 UTC 2021 : Endpoint response body before transformations: {"statusCode":400,"headers":{"my_header":"my_value"},"body":"\"ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'field list'\"","isBase64Encoded":false}
Sat Jul 10 08:11:04 UTC 2021 : Method response body after transformations: "ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'field list'"
Sat Jul 10 08:11:04 UTC 2021 : Method response headers: {my_header=my_value, X-Amzn-Trace-Id=Root=1-60e95617-157c462e040f133437c0add3;Sampled=0}
Sat Jul 10 08:11:04 UTC 2021 : Successfully completed execution
Sat Jul 10 08:11:04 UTC 2021 : Method completed with status: 400
检查您是否已创建具有适当权限的IAM策略。
示例:
API网关的IAM角色
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
IAM调用lambda函数:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": "*"
}
]
}
参考:-https://docs.aws.amazon.com/apigateway/latest/developerguide/permissions.html
以上内容足以让API网关调用您的Lambda,如果它仍然不能调用,我建议您在最后一步尝试此操作
下面是我的cloudformation脚本: 我已经被困在这一段时间了,任何的指针将非常感谢。
我们正在尝试从C#Windows服务调用AWS API网关来执行后台作业。哪个应该触发API网关定期初始化请求? 我们使用RestSharp调用APIendpoint,该类称为AwsAuthenticator,它继承自RestSharp.Authenticators.IAAuthenticator。但当我们调用API Gateway时,收到的错误是“我们计算的请求签名与您提供的签名不匹配。请检查您
我有一个这样的设置: 浏览器->API网关->lambda:87 ms 如何快速调用Lambda函数?
问题内容: 我需要将node-XMLHttpRequest应用程序部署到使用代理访问Internet的服务器上。在该服务器上的终端中,我可以并且运行正常。我认为在这种情况下可以识别环境变量。节点应用超时,因为它看不到代理。如何获取节点应用程序以使用代理? 例如,我可以使用http.globalAgent通过代理发送请求吗? 问题答案: 该派生添加了一个配置对象,该对象可以指定要使用的代理。 htt
我的(非常简单的)代码看起来是这样的: 我将构建好的jar上传到Lambda,并通过创建一个测试事件在AWS控制台中进行测试。它返回正确的响应,正如预期的那样。 但是,我希望API网关调用这个Lambda。我既使用了Lambda代理集成,也定义了自己的主体映射模板。我似乎无法使它工作,我一直得到: 我确定这是一件很简单的事情,我只是错过了一些东西...