Serverless w/ AWS Lambda Functions

If you are like me then you are always working in environments where everything wants to increase speed & efficiency while lowering cost.  Serverless compute functions or functions as a service unlock this ability for end users like developers.  No longer do developers have to manage servers but they just manage code. 

Benefits of functions:

  • No servers to manage
  • Built for short run times
  • Can be run on-demand
  • Scaling is automatic
  • Cheap

The walkthrough – why use serverless computing & Lambda

AWS Lambda overview explaining serverless code execution with a simple function example.

In the example, we have a function that logs something and returned hello from lambda. Simple code and we can click run and just run it from Lambda. 

AWS Lambda walkthrough showing a short Hello from Lambda function and a Run button.

The function ran and we got the return message

AWS Lambda walkthrough diagram showing mobile, streaming, and data processing events invoking a Lambda function.

As you can see many different event sources can send events to Lambda and cause Lambda will respond.

AWS Lambda walkthrough diagram illustrating automatic scaling across invocations and usage-based cost.

From the earlier example you can see I haven’t been billed until I hit my 1 millionth request.

Cost grows linearly based on usage and load

Let’s create the first function

AWS Lambda Create function page showing author from scratch, blueprint, container image, and serverless repository options.

If I had my own custom code to deploy I would run through author from scratch.  However, to keep things simple for this how to I will use a blueprint instead.

Create function page with Use a blueprint selected and the hello-world-python blueprint chosen.

I searched by hello to find an easy python blueprint for hello world

Then click configure in the bottom right hand

Lambda blueprint configuration with the function name and a new basic execution role selected.

Provide your function a name

Lambda needs to have an IAM role to interact with other AWS service offerings.  I am just selecting create a new role and it will be named

My-First-Hello-World-role-6pp7q8or

Lambda blueprint code showing a Python handler that reads and prints three values from the event.

The default python language here is basically saying the handler will print three values and return a value

Create function

AWS Lambda function page confirming My-First-Hello-World was created successfully.

As you can see my function was created and I have the ability to test the function.

Configure test event dialog with a hello-world template, event name, and sample JSON payload.

Provide an event name and click create

AWS Lambda function page showing the Designer after a test event has been configured.

Now that my test event has been created I now can test

AWS Lambda execution result showing the test succeeded, with response details and log output.

Expand details for more information

What if we want to make changes?

AWS Lambda Designer with the My-First-Hello-World function selected for editing.

Make sure your function is selected in Designer

AWS Lambda code editor showing the original Python handler that prints event values and returns the first value.

Our original code

AWS Lambda code editor with an added exception and a notice that the changes have not yet been deployed.

Made a change to now return an exception that something went wrong – need to click deploy to save

AWS Lambda code editor showing the exception change successfully deployed to the function.

Changes saved – now let’s run the test again

AWS Lambda execution result showing the test failed after the function raised a Something went wrong exception.

As expected we got an error that something went wrong

Now you can scroll down and changed many different settings and get as granular as you need based on your thought out design.

Summary:
I kept the Lambda examples simple for a reason.  This was intended as a quick walkthrough but it also displays how powerful Lambda functions and serverless computing can be.  As always, I hope y’all found this useful.

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading