You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何部署在AWS Lambda的Express应用无法调用纯Lambda函数?

解决方案

1. 补全Lambda执行角色权限

本地调用用的是你本地AWS凭证(通常权限充足),但部署后的Express Lambda执行角色默认没有调用其他Lambda的权限。给Express Lambda的执行角色添加内联策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "lambda:InvokeFunction",
            "Resource": "arn:aws:lambda:<你的AWS区域>:<你的AWS账号ID>:function:stripe_payment"
        }
    ]
}

2. 调高Lambda超时时间

当前6秒超时可能不足以覆盖Lambda跨调用+Stripe API请求的总耗时:

  • 在AWS控制台分别修改Express Lambda和stripe_payment Lambda的超时时间(比如设为15秒),路径:Lambda函数→配置→常规配置→编辑→超时。

3. 修复异步逻辑冲突

被调用的stripe_payment Lambda混合使用了async/await和callback,会导致函数进程无法正确终止,引发超时。改成纯async/await写法:

const stripe = require("stripe")(process.env.STRIPE_SECRET_TEST)
exports.handler = async (event) => {
  console.log("Hello world!!!!!")
  const requestBody = event.user;
  console.log("request body !!!!!!!!!", requestBody)
  const token = requestBody.id;
  const amount = requestBody.amount;
  const currency = "USD";
  const customer = requestBody.customer
  console.log("req body", requestBody)

  try {
    const charge = await stripe.paymentIntents.create({
      amount,
      currency,
      description: 'Serverless Stripe Test charge',
      payment_method: token,
      customer,
      confirm: true
    });
    console.log("charge", charge)
    return {
      statusCode: 200,
      headers: {
        'Access-Control-Allow-Origin': 'https://stage-back.eidverse.io',
      },
      body: JSON.stringify({
        message: `Charge processed succesfully!`,
        charge,
      }),
      "isBase64Encoded": false,
    };
  } catch (err) {
    console.log("err charge", err)
    return {
      statusCode: 500,
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
      body: JSON.stringify({
        error: err.message,
      }),
    };
  }
};

4. 排查VPC配置冲突

如果一个Lambda在VPC内,另一个不在,会导致跨调用失败:

  • 确保两个Lambda要么都在同一个VPC(配置好安全组和NAT网关),要么都不在VPC内。若Express Lambda在VPC里,需配置NAT网关让它能访问外网和其他Lambda资源。

5. 检查目标Lambda的日志

只看Express Lambda的超时日志不够,去CloudWatch查看stripe_payment的日志:

  • 确认它是否被触发;
  • 检查是否有Stripe调用的错误日志;
  • 验证函数是否正常返回响应。

内容的提问来源于stack exchange,提问作者Blockchain Kid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 11:21:07