DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Managing In-Production AWS Lambda Functions With API Gateway

Managing In-Production AWS Lambda Functions With API Gateway

Let's take a look at a tutorial that explains managing in-production AWS Lambda functions with API Gateway.

Zeeshan Baig user avatar by
Zeeshan Baig
·
Oct. 11, 18 · Tutorial
Like (4)
Save
Tweet
Share
13.80K Views

Join the DZone community and get the full member experience.

Join For Free

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. With Lambda, you can run code for virtually any type of application or backend service — just set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

Amazon API Gateway is a fully managed service that allows you to create, publish, maintain, monitor, and secure APIs at any scale. With API Gateway you can create an API that acts as a “front door” for AWS Lambda functions.

Together AWS Lambda and API Gateway are commonly used in N-Tier architecture applications, for example, the front end UI developed in Angular, ReactJS or Vu.js and calls API gateway endpoints which eventually triggers Lambda functions.

The benefits of using N-Tier architecture is that you don’t have to manage any servers or EC2 instances, you can deploy your app on AWS S3 and rest is taking care by AWS API gateway and AWS Lambda as shown in the slide.

Another benefit is to debug or deployment of new features is relatively easier as compared to other architectures.

Problem

The problem or challenge is when working in multiple environments with Lambda functions. Typically, organizations have Development, QA and Production environments. So how would you implement new features or changes to your existing Lambda functions without impacting when one function is currently running in Production or QA?

One solution could be to have different AWS accounts but that also add some administrative overhead.

Solution

To solve the above problem AWS Lambda Versioning and Aliases comes into action. Using versioning in AWS Lambda, you can publish one or more versions of your Lambda function and work with different variations of your function in your development workflow, such as Development, QA or Production.

AWS Lambda also supports creating aliases for each of your Lambda function versions. Conceptually, an AWS Lambda alias is a pointer to a specific Lambda function version.

To learn more about AWS Lambda versioning and aliases, check out the AWS documentation.

In our example, we have a single AWS Lambda function behind AWS API Gateway. The API gateway has 2 different stages deployed named as DEV and PROD. By calling these stages you will see a different Lambda function response from a single Lambda function respectively.

This will allow us to test the API and Lambda function in DEV environment independently of Production API.

We have to do the following:

  1. Create Versions and Aliases in AWS Lambda Function
  2. Create Dynamic Resources in AWS API Gateway
  3. Create Deployment Stages and Variables in AWS API Gateway
  4. Test the API

To summarize, when you call the DEV staged API endpoint the function would “Hello from Lambda World” and in case of PROD endpoint the function would return “Hello from Lambda”

The following slide gives an overview of our example:

Versioning and Aliases in AWS Lambda Function

As shown in the slide, we have 2 different versions names 1 and 2respectively of the same lambda function called hello-lambda.

You can create a new version of lambda function by clicking Publish New Version.

Following is the version 1 we will be using for our PROD API

The following is the version 2 we will use for our DEV API:

Similarly, you can create aliases by clicking Aliases. In our case, we have dev and prod aliases defined, which point to version2 and version1 as shown in the slide:

Dynamic Resources in AWS API Gateway

Once we have versions and aliases defined, you can call your Lambda function using the following method:

mylamda-function:my-alias

e.g. hello-lambda:prod or hello-lambda:dev

In order to make it efficient, we need to pass the aliases dynamically so in case of any change our APIs aren’t impacted.

You can do this using Stage Variables in AWS API Gateway, in our case we are passing the alias to Lambda function value by ${stageVariables.env}. On runtime, AWS API gateway would replace the value of the “env” with the value we define.

Note: When defining Stage Variable in the resources you need to add permissions to use stage variables. API gateway would prompt you to run the following command:

You need to run this command on AWS CLI with a proper setup as many times as stages in our case we had to run this twice with dev and prod.

Deployment Stages and Variables in AWS API Gateway

You also need to define multiple stages in AWS API gateway as shown in the slide we have 2 stages defined as dev and prod.

Click on the Stages then click on Stage Variables and enter the name of your variable and value you like to pass on this API endpoint. In our case, we have variable defined env which is we are using in our Lambda Integration point (see the previous paragraph)

Stage Variable for DEV

Stage Variable for PROD

Test the API

Copy the API endpoint from the stages as shown in the slide

Now test the dev stage endpoint in Postman the API would return “Hello from Lambda World”

Now try the prod stage endpoint, you will see the “Hello from Lambda”

Summary

AWS LambdaVersioning and Aliases are powerful features allow us to develop multiple versions of the same function and use for different use cases. AWS API Gateway Stage variables provide us to pass dynamic URLs and resources to avoid duplicates and complexity when developing your APIs.

Hope you find this post useful, please leave a comment for any questions or suggestions.

AWS API AWS Lambda

Published at DZone with permission of Zeeshan Baig. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Create and Edit Excel XLSX Documents in Java
  • A Brief Overview of the Spring Cloud Framework
  • How To Check Docker Images for Vulnerabilities
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: