How to use AWS Lambda with API Gateway

Posted on November 10, 2021 at 01:56 PM

Before talking about AWS Lambda function, let us understand the term serverless.

What is serverless?

Serverless is a cloud based development approach. The developer focuses more on writing code and solving their business problems while the server is managed by AWS professionals. The advantage of a serverless application could be seen when auto-scaling the application. Not too long ago, developers had to anticipate application usage and add servers accordingly.

But, with the advent of serverless approach, bandwidth required to support the operation is managed by AWS. Resources could be scaled up or down depending on the computing needs at the time and you pay only for the resources you use.

The pay-as-you-go billing model helps teams to save time and money and be more focussed on customers’ needs.

Now get back to our main topic.

What is AWS Lambda?

Definition as given on AWS, “AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers”, describes a serverless approach in a nutshell.

Using AWS Lambda has its own merits and demerits. Here I’m listing some of them.

Benefits of using AWS Lambda

  1. It follows serverless approach
  2. The AWS Lambda code could be called directly from a web or mobile application
  3. It could also be triggered by another AWS service, for example, SQS Queue, API Gateway, SNS, Cloudwatch, etc.
  4. AWS supports a variety of programming languages to write the code. It includes C#, Java, Node Js and Python.
  5. Developers can also use code compiling tools such as Maven or Gradle, and packages to build the functions.

Limitations of using AWS Lambda

AWS Lambda limit to amount of compute and storage resources. The limits are applied in each region and could be increased.

Its limitations could be categorized into two categories:

  1. Hard limit
  2. Soft limit

Soft Limits

Soft limits are those that are set as default values. They vary according to the region and could be increased by putting requests to the AWS support team. These limits are related to

  1. Concurrent Executions Limit
  2. Function and layer storage limit
  3. Elastic Network Interface per VPC
soft limits

Hard Limits

Hard limits are those which could not be modified even by the AWS support team. These limits are related to the

  1. function configuration
  2. function deployment
  3. function execution
hard limits

Some common terms related to AWS Lambda

Function

A function is a program that is invoked by the Lambda. Lambda sends invocation events into the function which then processes the events and returns their responses.

Runtime

It is the function execution environment selected to run the code. Runtime matches the language selected for the function.

Event source

Event source is an AWS service that triggers the function. Eg: HTTP request through API Gateway, SQS message available event, etc.

Lambda layer

The Lambda function imports several libraries and dependencies and zip them as a part of the build and deployment process. A Lambda layer is an archive that could be used to store additional code, such as dependencies and libraries, separated from the main build.

Prerequisites

  1. AWS account
  2. IAM permissions to use.

How to deploy AWS Lambda function

Steps are given as below:

  1. Login to your AWS account.
  2. Visit https://aws.amazon.com/lambda/
  3. Click: Get Started with AWS Lambda
  4. How to deploy AWS Lambda function - Image 1
  5. Click: Create function
  6. How to deploy AWS Lambda function - Image 2
  7. Fill in the basic information
  8. How to deploy AWS Lambda function - Image 3
    1. Select Author from scratch.
    2. Enter function name.
    3. Select the runtime environment.
    4. Click ‘Create function’. Leave other configuration to its default setting for the sake of simplicity.
  9. Add your code in the space provided in the section named ‘Code source’.
  10. How to deploy AWS Lambda function - Image 4

    Alternatively, instead of manually entering code in the editor, you could zip the files and upload the zip file by clicking the button ‘Upload from’ as seen in the image at the top-right corner.

    [Note: Size of total un-zipped files and folders could not be more than 250MB. See the chart shown above in the section ‘Limitations of using AWS Lambda’. To upload the code of size more than 250MB you could use an Elastic Container Register(ECR) image which is the topic for some other day. ]

    Now, your code is ready to test from the AWS console.

  11. Test your code, by clicking ‘Test’. A new tab will appear with the response. If everything is OK then you will get the response as shown below:
How to deploy AWS Lambda function - Image 5

Add API Gateway as Lambda trigger

As discussed above, Lambda could be triggered by the events from other AWS services. Lets create an API gateway to trigger the lambda function.

Steps to add API Gateway as a trigger:

  1. Select the lambda function to which trigger is to be added
  2. How to deploy AWS Lambda function - Image 6
  3. Click ‘Add trigger’
  4. Add API Gateway as Lambda trigger Image 2
  5. Select the trigger: ‘API Gateway’
  6. Add API Gateway as Lambda trigger Image 3
  7. Select
    1. HTTP API
    2. Security: Open
    3. Submit the form by clicking the ‘Add’ button.
    Add API Gateway as Lambda trigger Image 4
  8. Click the ‘Configuration’ tab and find the API Gateway details. Copy the API endpoint.
  9. Add API Gateway as Lambda trigger Image 5
  10. Click the endpoint and get the response.
  11. Add API Gateway as Lambda trigger Image 6

Conclusion

AWS lambda is a serverless compute service which has the benefit of worry free auto-scaling. An event source in AWS could be used to trigger the Lambda function.

There are few restrictions with Lambda functions, but they have some thought behind them. It is meant to protect the developer from unintentional behaviour. To make most of AWS Lambda, the developer must be aware of these limitations.

Related Posts

Start a Project

We could talk tech all day. But we’d like to do things too,
like everything we’ve been promising out here.