为 Lambda 函数创建文件 - Amazon CodeDeploy
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

为 Lambda 函数创建文件

本教程稍后将为更新和部署的函数创建文件。

注意

Lambda 函数可以使用 Amazon Lambda支持的任何运行时。有关更多信息,请参阅 Amazon Lambda 运行时

创建 Lambda 函数
  1. 创建文本文件,并以 myDateTimeFunction.js 文件形式保存到 SAM-Tutorial 目录中。

  2. 将以下 Node.js 代码复制到 myDateTimeFunction.js 中。

    'use strict'; exports.handler = function(event, context, callback) { if (event.body) { event = JSON.parse(event.body); } var sc; // Status code var result = ""; // Response payload switch(event.option) { case "date": switch(event.period) { case "yesterday": result = setDateResult("yesterday"); sc = 200; break; case "today": result = setDateResult(); sc = 200; break; case "tomorrow": result = setDateResult("tomorrow"); sc = 200; break; default: result = { "error": "Must specify 'yesterday', 'today', or 'tomorrow'." }; sc = 400; break; } break; /* Later in this tutorial, you update this function by uncommenting this section. The framework created by Amazon SAM detects the update and triggers a deployment by CodeDeploy. The deployment shifts production traffic to the updated version of this function. case "time": var d = new Date(); var h = d.getHours(); var mi = d.getMinutes(); var s = d.getSeconds(); result = { "hour": h, "minute": mi, "second": s }; sc = 200; break; */ default: result = { "error": "Must specify 'date' or 'time'." }; sc = 400; break; } const response = { statusCode: sc, headers: { "Content-type": "application/json" }, body: JSON.stringify( result ) }; callback(null, response); function setDateResult(option) { var d = new Date(); // Today var mo; // Month var da; // Day var y; // Year switch(option) { case "yesterday": d.setDate(d.getDate() - 1); break; case "tomorrow": d.setDate(d.getDate() + 1); default: break; } mo = d.getMonth() + 1; // Months are zero offset (0-11) da = d.getDate(); y = d.getFullYear(); result = { "month": mo, "day": da, "year": y }; return result; } };

Lambda 函数返回昨天、今天或明天的日期、月份和年份。在本教程后面的部分中,您将取消注释更新函数的代码,以返回有关您指定的日期或时间的信息(例如,日期、月份和年份,或当前小时、分钟和秒)。由创建的框架 Amazon SAM 会检测并部署该函数的更新版本。

注意

教程中也使用了此 Lambda 函数。 Amazon Cloud9 Amazon Cloud9 是一个基于云的集成开发环境。有关如何在中创建、执行、更新和调试此函数的信息 Amazon Cloud9,请参阅的Amazon Lambda 教程 Amazon Cloud9