Crea un file per la tua funzione Lambda - AWS CodeDeploy

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Crea un file per la tua funzione Lambda

Creare il file per la funzione da aggiornare e distribuire più avanti in questo tutorial.

Nota

Una funzione Lambda può utilizzare qualsiasi runtime supportato da. AWS Lambda Per ulteriori informazioni, consulta Tempi di esecuzione AWS Lambda.

Per creare la tua funzione Lambda
  1. Creare un file di testo e salvarlo come myDateTimeFunction.js nella directory SAM-Tutorial.

  2. Copiare il seguente codice Node.js in 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 AWS 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; } };

La funzione Lambda restituisce il giorno, il mese e l'anno di ieri, oggi o domani. Più avanti in questo tutorial, si decomprimerà il codice che aggiorna la funzione per restituire informazioni sul giorno o sull'ora specificati (ad esempio, giorno, mese e anno o ora, minuto e secondo correnti). Il framework creato da AWS SAM rileva e distribuisce la versione aggiornata della funzione.

Nota

Questa funzione Lambda viene utilizzata anche in un AWS Cloud9 tutorial. AWS Cloud9 è un ambiente di sviluppo integrato basato sul cloud. Per informazioni su come creare, eseguire, aggiornare ed eseguire il debug di questa funzione in AWS Cloud9, consulta AWS Lambda il tutorial per. AWS Cloud9