AWS Lambda With MySQL (RDS) and API Gateway
AWS Lambda With MySQL (RDS) and API Gateway
In this article, we discuss the limitations of Lambda and how to design simple micro-service using AWS API gateway, and RDS.
Join the DZone community and get the full member experience.Join For Free
I have been working on Lambda for some projects, and recently, I had to design and write an FAS service using Lambda functions and read/write data into RDS (MySQL). I'm sharing what I've learned here.
Before We Begin
This tutorial assumes that you have some knowledge of basic Lambda operations and the AWS console, RDS, IAM, and Node.js.
To understand whether a serverless application is good for you or not, you need to understand the limitations of Lambda, what a cold start is, and how concurrency works in Lambda. The Lambda execution model is, of course, a different architecture than traditional web applications in Spring.
What Is a Cold Start?
When a user starts their first request, Lambda has a slight delay while it spins up a new container and loads our code and dependencies in memory. This is called a cold start. A cold start only happens if there is no idle container available or waiting to run our code. Once a connection is timed out, AWS kills the container, and future requests will again spin up a new container. The first request will always be a bit slower due to the cold start, but subsequent requests will hit the same instance quickly.
You may also enjoy: Querying RDS MySQL DB With NodeJS Lambda Function
We can use a Cloud Watch Timer/dummy request to keep lambdas warm.
Concurrent requests will trigger the creation of new AWS Lambda instances. For example, if you have five concurrent requests to the same Lambda function, AWS will create five containers to serve the requests. In short, when you first invoke a function, AWS Lambda creates an instance of the function and runs its handler method to process the event. If you invoke the function again while the first event is being processed, Lambda creates another instance.
You can configure a function with reserved concurrency to guarantee that it can always reach a certain level of concurrency. This function can run with this maximum amount of concurrency without interrupting or waiting.
The Lambda currency limit is 1000 by default. Take note that one Lambda function that has scaled up can hit the performance of every other Lambda function in the same region.
Reserve concurrency doesn't start until 20 instances on the first Lambda call. Its reserve capacity for a function can scale up or down. As soon as the concurrent executions limit is reached, the next execution gets throttled.
Lambda functions will retry three times before they fail due to an uncaught application error. If you added
context.succeed and did not handle exceptions properly, Lambda will not retry the function and instead, will assume that the function was successful. If you have added
context.fail, Lambda will retry the function.
Common Invocation Errors
- Request – The request event is too large or isn't valid JSON, the function doesn't exist, or a parameter value is of the wrong type.
- Caller – The user or service doesn't have permission to invoke the function.
- Account – The maximum number of function instances are already running or requests are being made too quickly.
The following are two approaches to debug Lambda on a local machine:
1. Choose Test at the top right of your Lambda function to bring up a screen that allows you to configure a new test that can be run to verify success/failure and you can also add console.log for debugging.
2. Another most popular option is Cloudwatch for debugging on Aws console .When you review CloudWatch log files or metrics when you're troubleshooting errors, be aware that they are displayed or stored in the Region closest to the location where the function executed.
Lambda-local lets you test Amazon Lambda functions on your local machine, by providing a simplistic API and command-line tool.
You can use Lambda-local as a command line tool.
You can find further information from the following link:
You can also use SAM Local to develop and test Lambda functions locally with Docker.
You can find further information on following link
Lambda Functions Versioning and Deployment
If you want to publish a new version of a function for QA testing without affecting the stable production version, then you can use Lambda version option. When you choose a new version, the system creates a new version of your Lambda function each time that you publish the function. We can publish multiple versions of a function. Each version can be invoked in parallel in a separate container. By default, the version would be
We can configure blue-green /canary deployment for continuous delivery:
The first step to start code is to provision an RDS instance using AWS Console. Then, try AWS Free Tier with Amazon RDS.
MySQL 750 hours of Amazon RDS Single-AZ db.t2.micro Instance usage running MySQL 20 GB of General Purpose (SSD) DB Storage and 20 GB of backup storage for your automated database backups and any user-initiated DB Snapshots
Write Your First Lambda Function
When you invoke a function, you can choose to invoke it synchronously or asynchronously. With synchronous invocation, you wait for the function to process the event and return a response. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately. For asynchronous invocation, Lambda handles retries and can send failed events to a dead-letter queue.
You need to create a role for Lambda. As a best practice, define policies that follow the principle of granting least privilege. In other words, the policies include only the permissions that users require to perform their tasks, for learning purpose adding broad role but you can narrow as per your requirements.
To create a Lambda function, go to AWS Console and select Lambda.
Click create function and choose Node.js 12.x. Then, choose existing role we created above.
In order to support a MySQL dependency, we need to create code first on your local computer and then upload it to the Lambda console.
Create a folder and create package.json
Once the file is created, you can find the following JSON in the folder.
You need to add MYSQL node dependency in project using following command
Now, add a handler into the app, create index.js (index.handler), and add Lambda logic.
In Lambda, first, we are going to create a connection object and then call our database.
Then, we need to call our Lambda handler.
Now, create a zip file and upload it.
After this, upload and click on index.js. For further changes, use inline editor.
Create a lambda env variable with its key-value pairs.
Now, configure Lambda with RDS and VPC, go to the Security group of the RDS instance. After selecting the default security group, click on it. On the EC2 page, scroll down to find inbound and outbound settings.
In inbound settings, click edit. You can change the IP here.
- 0.0.0.0/0 — this makes RDS open to the world and is not recommended.
- Configure VPC inside the Lambda function network to Lambda function work. First, go to the Lambda function, click on Network, and then select the VPC and copy the IP address. If no VPC is selected, select a VPC that's the as the DB function and copy the IP address. Add this IP in RDS inbound settings.
Save it and test your Lambda function.
The following are a few Lambda handlers to understand how CRUD operations work in Lambda.
Create a database using lambda
Create a table using lambda
Insert records using lambda
Select records using lambda
Select records with criteria using lambda
Now, we're down to the actual meat-and-potatoes of what we're trying to do:
Create a Table
Create a Lambda
Create a Test Event
Create an API Gateway
The number of APIs per account limit is 600 regional APIs, 600 private APIs, and 120 edge APIs. Now, you can create and import regional and private APIs at a rate of one request every three seconds and deploy APIs at a rate of one request every five seconds.
Note, when you deploy an API to API Gateway, throttling is enabled by default in the stage configurations.
For the time being, in this tutorial, we are not implementing security on the API gateway, but it's mandatory to add security on gateway.
The following mechanisms can be used for authentication and authorization of the API gateway:
- Resource policies let you create resource-based policies to allow or deny access to your APIs and methods from specified source IP addresses or VPC endpoints. For more information, see Control Access to an API with Amazon API Gateway Resource Policies.
- Standard AWS IAM roles and policies offer flexible and robust access controls that can be applied to an entire API or individual methods. IAM roles and policies can be used for controlling who can create and manage your APIs as well as who can invoke them. For more information, see Control Access to an API with IAM Permissions.
- IAM tags can be used together with IAM policies to control access. For more information, see Using Tags to Control Access to API Gateway Resources.
- Endpoint Policies for Interface VPC Endpoints allow you to attach IAM resource policies to interface VPC endpoints to improve the security of your private APIs. For more information, see Use VPC Endpoint Policies for Private APIs in API Gateway.
- Lambda authorizers are Lambda functions that control access to REST API methods using bearer token authentication as well as information described by headers, paths, query strings, stage variables, or context variables request parameters. Lambda authorizers are used to control who can invoke REST API methods. For more information, see Use API Gateway Lambda Authorizers.
- Amazon Cognito user pools let you create customizable authentication and authorization solutions for your REST APIs. Amazon Cognito user pools are used to control who can invoke REST API methods. For more information, see Control Access to a REST API Using Amazon Cognito User Pools as Authorizer
- Secure key for API
Create a Resource
Create Method add GET
Deploy an API
Create a new stage and deploy the API. After successfully deploying, you can see the URL.
Test Lambda with API gateway
Download Postman or use the Postman Chrome extension to call the API.
In this post, we discussed how to create applications quickly to run in AWS Lambda, which provides low cost, zero maintenance compute, and automatically scales. We also discussed the limitations of Lambda and how to design simple micro-service using AWS API gateway, and RDS. In the next post, we will try to cover AWS Cognito for Authentication/Authorization and how to sync Cognito data into an RDS table.
Dealing With Serverless Cold Starts, Once and For All!
A Simple API Using AWS RDS, Lambda, and API Gateway
Published at DZone with permission of Vaquar Khan . See the original article here.
Opinions expressed by DZone contributors are their own.