

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à.

# Fase 2: Aggiornare la funzione Lambda
<a name="tutorial-lambda-sam-update-function"></a>

 Questo argomento illustra come aggiornare il file `myDateTimeFunction.js`. Nella fase successiva, si utilizzerà il file per distribuire la funzione aggiornata. Ciò avvia la distribuzione CodeDeploy spostando il traffico di produzione dalla versione corrente della funzione Lambda alla versione aggiornata. 

**Per aggiornare la funzione Lambda**

1.  Aprire `myDateTimeFunction.js`. 

1.  Rimuovere i due contrassegni di commento ("`/*`" e "`*/`") e il testo esplicativo all'inizio e alla fine del `case` denominato `time` nel blocco `switch`. 

    Il codice non commentato consente di passare un nuovo parametro `time` alla funzione. Se si passa `time` alla funzione aggiornata, restituisce i `hour`, `minute` e `second` correnti. 

1.  Salva `myDateTimeFunction.js`. Avrà un aspetto simile al seguente: 

   ```
   '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;
         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;
     }
   };
   ```