在AWS API Gateway克隆时,Lambda触发器不会自动复制。您需要手动将Lambda触发器添加到新的API Gateway克隆中。
以下是一个示例代码,演示了如何使用AWS SDK为JavaScript(Node.js)将Lambda触发器添加到API Gateway:
const AWS = require('aws-sdk');
// 创建API Gateway实例
const apiGateway = new AWS.APIGateway();
// 克隆API
const cloneApi = async (sourceApiId, targetApiName) => {
try {
// 克隆API
const cloneResponse = await apiGateway.cloneRestApi({ restApiId: sourceApiId }).promise();
const clonedApiId = cloneResponse.cloneResponseId;
// 获取Lambda函数的ARN
const lambdaFunctionArn = 'arn:aws:lambda:us-west-2:123456789012:function:your-lambda-function';
// 创建Lambda触发器
await apiGateway.putIntegration({
restApiId: clonedApiId,
resourceId: 'your-resource-id', // 替换为克隆的API的资源ID
httpMethod: 'GET', // 替换为适当的HTTP方法
type: 'AWS',
integrationHttpMethod: 'POST',
uri: `arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/${lambdaFunctionArn}/invocations`
}).promise();
console.log('Lambda触发器已成功添加到克隆的API Gateway');
} catch (error) {
console.error('添加Lambda触发器时出错:', error);
}
};
// 使用API Gateway的源API ID和克隆API的名称调用该函数
cloneApi('your-source-api-id', 'your-cloned-api-name');
在上面的示例代码中,您需要替换以下值:
'arn:aws:lambda:us-west-2:123456789012:function:your-lambda-function'
:替换为您的Lambda函数的ARN。'your-resource-id'
:替换为克隆的API的目标资源的ID。'GET'
:替换为适当的HTTP方法。请确保您已配置AWS SDK的凭证,以便可以访问API Gateway服务。