首页 > 解决方案 > 为什么 lambda 函数在测试中不输出我的标头?

问题描述

我创建 lambda 函数:

exports.handler = (event, context, callback) => {
  const { response } = event.Records[0].cf;

  response.headers["x-my-header"] = [{ value: "test" }];

  return callback(null, response);
};

我点击保存并点击“测试”。

但是我看不到x-my-header。为什么?我在这里想念什么?

标签: amazon-web-servicesaws-lambda

解决方案


我已经运行了您的 LAmbda 并在使用cloudfront-modify-response-header.

{
  "status": "200",
  "statusDescription": "OK",
  "headers": {
    "vary": [
      {
        "key": "Vary",
        "value": "*"
      }
    ],
    "last-modified": [
      {
        "key": "Last-Modified",
        "value": "2016-11-25"
      }
    ],
    "x-amz-meta-last-modified": [
      {
        "key": "X-Amz-Meta-Last-Modified",
        "value": "2016-01-01"
      }
    ],
    "x-my-header": [
      {
        "value": "test"
      }
    ]
  }
}

看起来它运行正常,我相信测试和测试事件之间的区别存在混淆。测试事件本身是 Lambda 函数的输入,当您测试函数时,您将查看输出以查看基于此事件输入的响应,如下面的屏幕截图所示。

在此处输入图像描述


推荐阅读