【问题标题】:AWS Lambda Function : {"message":"Internal Server Error"AWS Lambda 函数:{\"message\":\"Internal Server Error\"
【发布时间】:2022-09-23 05:04:25
【问题描述】:

我试图运行美国邮政服务的网络工具, 用于将邮政编码转换为州和城市。我在 AWS Amplify 中创建了一个 AWS Lambda 函数。 但是 Lambda 函数总是给我返回消息{\"message\":\"Internal Server Error\"}

这是我的 Lambda 函数代码。

const axios = require(\"axios\");

const BASE_URI =
  \"http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=\";
const config = {
  headers: {
    \"Content-Type\": \"text/xml\",
    \"Access-Control-Allow-Origin\": \"*\",
    \"Access-Control-Allow-Credentials\": true,
    \"Access-Control-Allow-Methods\": \"GET\",
  },
  method: \"get\",
};
exports.handler = async function (event, context, callback) {
  // The zipcode is sent by the frontend application. 
  // This is where we use it.
  const zipcode = event.queryStringParameters.zipcode;

  // The xml variable is the string we are going to send to the
  // USPS to request the information
  const xml = `<CityStateLookupRequest USERID=\"400000000\"><ZipCode ID=\"0\"><Zip5>${zipcode}</Zip5></ZipCode></CityStateLookupRequest>`;
  try {
    // Using syntactic sugar (async/await) we send a fetch request
    // with all the required information to the USPS.
    const response = await axios(`${BASE_URI}${xml}`, config);
    // We first check if we got a good response. response.ok is
    // saying \"hey backend API, did we receive a good response?\"
    if (!response.ok) {
      // If we did get a good response we store the response
      // object in the variable
      return { statusCode: response.status, body: response };
    }
    // Format the response as text because the USPS response is
    // not JSON but XML
    const data = await response.text();
    // Return the response to the frontend where it will be used.
    return {
      statusCode: 200,
      body: data,
    };
    // Error checking is very important because if we don\'t get a
    // response this is what we will use to troubleshoot problems
  } catch (err) {
    console.log(\"Error: \", err);
    return {
      statusCode: 500,
      body: JSON.stringify({ msg: err.message }),
    };
  }
};

我认为axios 工作正常。

任何帮助将不胜感激,因为我正在尝试解决这个问题好几天。

  • 您是否检查了 cloudwatch 中的功能日志?
  • \"errorType\": \"Error\", \"errorMessage\": \"无法字符串化响应正文\",

标签: amazon-web-services lambda serverless usps


【解决方案1】:

默认情况下,Lambda 函数没有对 Internet 的出站访问。 您可以将 Nat 网关添加到您的 VPC,但这并不便宜。

【讨论】:

    猜你喜欢
    • 2020-12-20
    • 2021-08-08
    • 2020-05-19
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2021-11-17
    • 2021-09-11
    相关资源
    最近更新 更多