DevOps is about speed.
AWS Lambda and serverless architectures allow you to develop and deliver faster
than before. When used effectively, they can lower your costs and allow you to
embrace DevOps with ease.
Lambda is a serverless
computing environment which allows you to connect your code with an event. Your
code will be executed when an event fires. You can also put Lambda functions
behind a REST API, which we’ll see how to do momentarily.
Lambda supports many
different languages and execution environments. For this example, we’ll be
using Node.js since it’s pretty simple to get a Lambda function up and running
with that environment. You can also use Python, Go, C#, and Java. Let’s create
our first Lambda function!
Tools We Need
No need of local
development environment, AWS Lambda will execute our code.
Creating your first
Lambda function
Select “Lambda” from
the “Services” menu. Alternately, you can type “Lambda” in the search box on
AWS console and then select it.
Once you’re on the
Lambda screen, click the “Create a Function” button to begin the process of
creating your first function. Fill out the form as you see it below:
The role field refers
to the permissions you want your Lambda function to have within AWS. You can
further research AWS Identity and Access
Management, but we won’t cover that here. For our purposes, we’ll create a role
for our function based on the “Simple Microservice” permissions.
Click the “Create
function” button to navigate to the Lambda function creation screen. This
screen looks busy at first, so we’ll walk through it piece by piece and set
what we need as we go along. For now, close the “Designer” section of the page
by clicking on the section header. We will come back to that while integrating
our code with another AWS service in order to make it available for use. Your
screen should look like this:
In the “Function code”
section, you’ll see an integrated code editor for you to use. Let’s use simple
example to illustrate working of plumping in AWS lambda. Edit code as below:
exports.handler = (event,
context, callback) = {
const result = event.number1 + event.number2;
callback(null, result);
};
const result = event.number1 + event.number2;
callback(null, result);
};
Let’s test our Lambda
function to see if it works. The Lambda interface gives us ability to do that.
Click on dropdown next to “Test” and “Save” buttons and choose “configure test
events.” (Note: “configure test events” will be your only option.)
In the “Configure Test
Event” dialog box, select “Hello World” as the template and update the JSON
inside the box to the following:
{
"number1": 2,
"number2": 3
}
"number1": 2,
"number2": 3
}
Once done, your screen
should look like this:
Click the “Create”
button, and you’re ready to test. What we just did was create a test input to
our function. Now we can execute the function and make sure the result is what
we expect.
Click the “Save”
button if it’s active. Make sure your new test function is selected in the
dropdown and click the “Test” button. Your function will execute, and you’ll
see the results displayed at the bottom of the page. In this case, the result
returned from the function should be 5.
Congratulations! We’ve
created a Lambda function and successfully tested it so we can be confident it
will do what we think it will do.
So now we have working
lambda function. We can invoke it from Events/ Triggers. This event can be many
different things, such as database records being updated or files being
uploaded to an S3 bucket.
What's Next?
Awesome, you just
configured, wrote and executed your first code on AWS Lambda!
What else can you do
with Python and Lambda? Stay tuned to Crevise Blogs!
We can understand
the real power of AWS Lambda when we connect a trigger to it, so our code will
be executed based on the events. We'll take a look at that in the next
tutorial.
Safety and Availability: The thought of handing over your whole information to a public cloud supplier is usually a daunting due to the apparent safety and availability considerations. This is great blog. If you want to know more about this visit here AWS Cloud Certified.
ReplyDelete