Create Functionbutton and click on it
You will now see two options - choose the Blueprints
radio option
blueprintjust to get acquainted
Click into the Blueprints
textbox / dropdown and choose Blueprint name
option
Hello World
hello-world(verbatim)
hello-worldradio button from among the narrowed down options
Configure
SimpleTaxCalculator-[YOUR_ORG_ID]
Role*drop-down, choose
Create a custom role
Createa a new IAM Role
lambda_execution_role_YOUR_ORG_ID
Show Policy Documentand expand it to view the policy that we will be associating with this Lambda
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
Allow
'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');
};
Create Function, which should take a few moments to create
Lambda function consolethat shows all the details about your Lambda function
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.
Select a test event...drop-down
Configure test eventsoption
Create new test eventis checked
Hello Worldis chosen for
Event Template
ProductPurchaseEvent1
Create
The test event should look a little something like:
{
"key3": "value3",
"key2": "value2",
"key1": "value1"
}
Test
'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}%`);
};
Save
Testagain, the function will run, but the output isn't quite what we'd like
ProductPurchaseEvent1drop-down and choose
Configure test eventsoption
Edit saved test eventsoption is chosen
Saved test event, ensure that the
ProductPurchaseEvent1is chosen
{
"productPrice": 20,
"taxRate": 10,
"surchargeRate": 1
}
Save
Testagain
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
Actionsdrop down menu from the top right-hand corner
Publish new version
Last known version that works(just something descriptive that helps us later)
Publish
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.
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.
Actionsdrop-down menu from the top right-hand corner
Create Alias
name, enter
prod
description, enter
Used by all our current customers in production
version, choose 1 (the version we just created in the previous step)
Create
We now have our prod
alias. Let's also go ahead and create a dev
alias so we can continue working on our Lambda.
Actionsdrop-down menu from the top right-hand corner
Create Alias
name, enter
dev
description, enter
Latest and greates(or anything :)
version, choose
$LATEST
[Let's do a sidebar on Additional Versions
if needed]
live editportion of the Lambda console and replace the code with the below:
'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.");
};
Save
Test
embedded editorfor the Lambda function. Either is fine.
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:
Logslink