出现此问题的原因是API Gateway无法正确识别路由。可以在API Gateway中创建一个新的路由,然后将其与现有集成关联。或者可以改变现有路由和默认路由之间的优先级。
以下是示例代码:
// 定义路由和API endpoint
const routes = {
"GET /users": "getAllUsers",
"GET /users/:id": "getUserById",
"POST /users": "createUser",
"PUT /users/:id": "updateUser",
"DELETE /users/:id": "deleteUser"
};
// 定义API Gateway集成
const apiGateway = {
"getAllUsers": "arn:aws:lambda:getAllUsers",
"getUserById": "arn:aws:lambda:getUserById",
"createUser": "arn:aws:lambda:createUser",
"updateUser": "arn:aws:lambda:updateUser",
"deleteUser": "arn:aws:lambda:deleteUser",
"$default": "arn:aws:lambda:defaultFunction"
};
// 创建API Gateway路由
Object.keys(routes).forEach((key) => {
const [method, path] = key.split(" ");
const integration = apiGateway[routes[key]];
const route = {
"restApiId": "123456abc",
"parentId": "789012def",
"pathPart": path,
"method": method,
"authorizationType": "NONE",
"integration": {
"type": "AWS_PROXY",
"uri": `arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${integration}/invocations`
},
"apiKeyRequired": false
};
// 创建路由
const createdRoute = AWS.APIGateway.createResource(route);
console.log(`Created route ${path} with ID ${createdRoute.id}`);
});
// 将现有路由和默认路由之间的优先级更改为默认优先级
AWS.APIGateway.updateIntegration({
restApiId: "123456abc",
resourceId: "789012def",
httpMethod: