如何复制包含API Gateway触发器的AWS Lambda函数?
复制带API Gateway触发器的AWS Lambda函数
Got it, let's break this down clearly—Lambda's "Create Version" feature is built for immutable snapshots of your function, and it doesn't carry over triggers like API Gateway integrations. To fully replicate your function with its trigger, follow these actionable steps:
Step 1: Duplicate the Lambda Function itself
- Head to the Lambda console, locate your target function
- Click the Actions menu, then select Duplicate function
- In the setup window, name your new function (pick something distinct from the original), confirm the runtime matches, and configure permissions—you can reuse the original function's IAM role or create a new one if you need isolated permissions
- Once confirmed, your new function will spin up with all the original code, environment variables, layers, and core configurations intact
Step 2: Replicate the API Gateway Trigger
API Gateway triggers are actually separate integration resources linked to your Lambda function, so you have two options depending on whether you want to reuse the existing API or create an independent one:
Option A: Reuse the existing API, update the integration
- Open the API Gateway console and navigate to the API connected to your original Lambda
- Find the specific resource and method (e.g.,
GET /your-endpoint) and click Integration Request - In the Lambda function field, replace the original function ARN with your new Lambda's ARN
- Save the changes, then redeploy the API to your desired stage (like
prod) to apply the update
Option B: Create a fully independent API copy
If you don't want to modify the original API (to avoid disrupting existing traffic):
- In the API Gateway console, open your original API, go to Actions > Export API and export it as a Swagger/OpenAPI 3.0 file
- Click Create API, select Import, upload the exported file, and set up a new API
- For every method in the new API, update the Integration Request to point to your new Lambda function
- Deploy the new API to a stage, and you'll have a separate, fully functional trigger
Step 3: Verify the Setup
- Call your new API endpoint to confirm the new Lambda function triggers correctly and returns the expected response
- Check CloudWatch Logs for the new function to ensure there are no permission errors or configuration glitches
Bonus: Automate with CLI/IaC
If you prefer scripted or infrastructure-as-code workflows:
- Use the AWS CLI command
aws lambda create-functionto spin up the new function using the original's configuration details - For API Gateway, use
aws apigateway put-integrationto update the integration, or export the original API's CloudFormation template, tweak the Lambda ARN, and deploy it as a new stack
内容的提问来源于stack exchange,提问作者Rajarshi Das
相关产品推荐
相关产品推荐

