AWS Series (Lambda- Part 1)

Rohit Shirke
5 min readJan 14, 2023

--

Hello folks, it's been a while since I have posted so here is the first story of the year... hopefully I’ll be posting more posts in this series... Let’s explore Lambda in the first article.

This will be an introductory write-up about AWS Lambda for the beginner something that I really wanted as a starter when I first started.
(Note: The article revolves around the intro of the topic, creating & testing an AWS Lambda app locally, and finally deploying to AWS.)

let's dive in,

What is AWS Lambda?

Its a buzz in the serverless world that simply translates to, A serverless implementation (Not literally serverless. The code does run on servers😅) that allow you to run your code on someone else infrastructure without the need to worry about the provisioning and maintenance of the underneath infrastructure. here is the AWS definition of the same.

When do you need it?

When you typically think of any application as a service you need to have the infrastructure that runs your application. In the world of cloud tech, you can lease servers as a service. eg (EC2). However, when you do so, you have to pay on the basis of the computation power and duration for which you lease. Also, when your needs grow as your application grows, you need to provision more resources which will eventually cost you more.

This is the time when serverless tech comes to save you… As shared in the beginning, AWS lambda is simply one such serverless offering from the AWS stack (Google has cloud functions, Azure has Azure functions, and so on…) that allows you to lend the computation capacity on a per request basis.

Yes, per request basis! That way you only pay for the time you use the computation power. so no request no charges!

📝 Tip: AWS has simply the best documentation covering all the required topics around lambda and I’ll highly recommend you to read it.
Refer here What is AWS Lambda? — AWS Lambda (amazon.com) for more.

Enough of the intro.. let's explore further, (Hope you didn’t skip the intro)

AWS lambda supports many popular languages such as Java, JavaScript, Python, Ruby, and so on... For this article, we will choose a simple node JS app.

Requirements :

Package Manager (NPM) choose as your preference
Node (Application env yours could be different)
AWS-CLI (To deploy the app to aws)
SAM (for testing it locally.. the fun part!)
Code Editor
Docker (To run & test lambda locally)

📝 Tip: Note Below tools are needed to run and test the app locally. when you are working with an AWS account, you don’t need them as you would be interacting with AWS web management console that will provide you with all the tools.

Let's quickly create a Lambda app. Assuming you have SAM installed properly, type the below command.

> sam init
Sam invoke to create Lambda Application

Choose the custom template. This way we can choose how exactly we want to configure our app.

Choose the Sandard function next option, followed by the Runtime , and finally give your project a Name. This will create a basic lambda application with the below structure.

📝 Tip: The README.md file is an excellent self-explanatory doc AWS ships with the sample template don’t forget to read it. Below folder’s explanation is pulled from the same file.

  • src : Code for the application’s Lambda function. Your application can be a single function single file lambda or spread across multiple files.
  • events : Invocation events that you can use to invoke the function. You can define any inputs that your lambda function may need while invoking it. Just consider this as an input json file.
  • __tests__ : Unit tests for the application code.
  • template.yaml : A template that defines the application’s AWS resources.
    Like all the other services offered by aws the lambda runs on some configuration that decides the overall behavior and implementation of your Lambda function defined with the help of a configuration file.
  • buildspec.yml : Defines the build behavior for your application. how your application will be bundled and deployed on AWS.

Run your test application locally :

> sam local invoke helloFromLambdaFunction --no-event

Note: the name specified here ‘helloFromLambdaFunction’ should match with the one defined in the template.yaml file under the Resources directive.

If everything is configured properly you should see the below output.

Running lambda application locally

Hola, congratulation on running your first Lambda application. 👏🏽

Test your application :

Testing Lambda application is nothing complicated as it might appear.
As we would have multiple functions in our code returning various outputs when called. We only need to set the correct expectations in your tests and invoke the functions to test them that’s it. Below is the sample test for our application with the Jest testing framework.

// Import our function hello-from-lambda.js 
// Note we are simply importing the function as any JS file from relative path.
const lambda = require('../../../src/handlers/hello-from-lambda.js');

// define the test
describe('Test for hello-from-lambda', function () {
it('Verifies successful response', async () => {
// Invoke the function as any other normal js function and store its output
const result = await lambda.helloFromLambdaHandler();
const expectedResult = 'Hello from Lambda!';
// compare the results
expect(result).toEqual(expectedResult);
});
});

The sample application is shipped with a sample test under __test__ directory under the root folder. By default, the package.json file includes the entry for the “jest” as the testing framework. Run the below command to install all the Jest dependencies.

> npm install

Run your test

> npm run test
Lamda test

Build and deploy your application :

As we have chosen the standard lambda implementation the supported method to deploy is zip. That’s basically zipping our application along with its all dependencies and finally deploying to aws.

Run the below command to deploy the application

> sam build
> sam deploy --guided

The guided option will help you choose the stack, region, and any other roles and capacities with the lambda.

That’s it for this article hope this starter article helps… Stay tuned for further episodes…

Happy coding! 😃

--

--

Rohit Shirke
Rohit Shirke

Written by Rohit Shirke

Tech enthusiastic | Tech Lover | Software Developer

No responses yet