Lambda Lab

Step 1

Step 2

Step 3

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    }
  ]
}
'use strict';

console.log('Loading function');

exports.handler = (event, context, callback) => {
    //console.log('Received event:', JSON.stringify(event, null, 2));
    console.log('value1 =', event.key1);
    console.log('value2 =', event.key2);
    console.log('value3 =', event.key3);
    callback(null, event.key1);  // Echo back the first key value
    //callback('Something went wrong');
};

Step 4

Step 5

We'll now want to test the lambda function. And to do this, we want to set up and configure a few test events (inputs) and fire off the Lambda function.

The test event should look a little something like:

{
  "key3": "value3",
  "key2": "value2",
  "key1": "value1"
}

Step 6

'use strict';

console.log('Loading function');

exports.handler = (event, context, callback) => {
    //console.log('Received event:', JSON.stringify(event, null, 2));
    console.log('value1 =', event.productPrice);
    console.log('value2 =', event.taxRate);
    console.log('value3 =', event.surchargeRate);
    callback(null, `Product price: ${event.productPrice}, Tax: ${event.taxRate}% and surchage: ${event.surchargeRate}%`);
};

Step 7

{
  "productPrice": 20,
  "taxRate": 10,
  "surchargeRate": 1
}

Step 8

AWS Lambda functions now allow you to version your functions (where previously, as recently as a week ago, you couldn't). Let's explore this a bit more. To do this, let's set this known to work function aside as Version 1

We have now created Version 1 of our Lambda function. This currently just spits out the values that are given to it. But we'll be modifying this shortly to do a bit more.

Step 9

AWS Lambda functions have another new feature now with called Aliases.

We'll now create an Alias and point it at the version that we just created.

We now have our prod alias. Let's also go ahead and create a dev alias so we can continue working on our Lambda.

[Let's do a sidebar on Additional Versions if needed]

Step 10

'use strict';

console.log('Loading tax calculator function...');

exports.handler = (event, context, callback) => {
    console.log('Received event: ', JSON.stringify(event, null, 2));
    
    try {
        console.log(`Product price: $${event.productPrice}`);
        console.log(`Tax rate: ${event.taxRate}%`);
        console.log(`Surcharge rate: ${event.surchargeRate}%`);
        
        let tax = event.productPrice * (event.taxRate / 100.00);
        let surcharge = event.productPrice * (event.surchargeRate / 100.00);
        let finalPrice = event.productPrice + tax + surcharge;

        console.log(`Final price with tax: $${finalPrice}`);

        // On success, invoke the callback like so (2 arguments)
        // first one being null.
        callback(null, finalPrice); // Return calculated tax
    }
    catch(e) {
        console.log(e);
        
        // On failure, invoke the callback like so (a single argument)
        // with a helpful error message.
        callback('ERROR: Something went wrong!');
    }
    
    console.log("Done calculating tax.");
};

Step 11

It's easy to see the logs and output while developing the Lambda function. But once it's deployed and being used in the wild, you may still want/need to access the execution logs. To do this, we can always fallback on CloudWatch Logs. Just to show this really quick: