Best practices when connecting to AWS RDS with Lambda + pymysql? - amazon-web-services

I'm trying to create a setup with AWS where users can hit an API endpoint -> Lambda function connects to Aurora serverless DB.
This guide https://levelup.gitconnected.com/aws-lambda-with-rds-using-pymysql-23ad3cde46c8 is suggesting I put
conn = pymysql.connect(host=rds_host,user=name,passwd=password,db=db_name,...)
at the start of the lambda function. My worry is that every API request will create a new connection to the database, not sure if that is ok? I know that when locally performing operations, there is such a thing as creating a connection pool. Is there a recommendation on how/when this is needed on AWS Lambda?

Please review article below to Demystifying Lambda functionality and how handle the request executions
https://harishkm.in/2020/06/30/demystifying-database-connection-pools-in-aws-lambda-functions/
Also links below explain you how AWS Aurora handles the connection
https://docs.aws.amazon.com/whitepapers/latest/amazon-aurora-mysql-db-admin-handbook/connection-management-and-pooling.html
https://docs.aws.amazon.com/whitepapers/latest/amazon-aurora-mysql-db-admin-handbook/connection-handling-in-aurora-mysql-and-mysql.html
Regarding pymysql, you could use boto3 AWS SDK library instead of pymysql, it provides you better integration with AWS Services like AWS Lambda,AWS IAM and AWS Secret Manager as best practices
import boto3
rds_client = boto3.client('rds-data')
Review link below.
https://aws.amazon.com/blogs/database/using-the-data-api-to-interact-with-an-amazon-aurora-serverless-mysql-database/
Finally if your API traffic will be high, consider use scalable solutions as AWS RDS Proxy to help you to manage your database connection at scale, review links below.
https://aws.amazon.com/blogs/database/scale-modern-serverless-applications-with-amazon-rds-proxy-for-sql-server/

Related

Query an AWS RDS Database(Mysql) through AWS API Gateway and Lambda?

So I have a RDS Database server running Mysql engine, Now I need to query into this database through my mobile app based on Flutter.
The solution after some research I came up with was to create a Lambda function and hook it up to RDS and API Gateway Service. The deployed API would be used by the app.
But I need some help on this regarding how to set this up. Any good alternatives, reference materials or videos would be helpful.
Can you help me out?

How to connect Amazon AWS API Gateway to Amazon RDS?

I have an Amazon AWS RDS (PostgreSQL) database. I am trying to connect it to Amazon API Gateway as simply as possible (AWS Service WITHOUT Lambda).
I am trying to perform a simple get request. To get all "animals" (table name "animals") from the db.
The question is which Action to select. All actions in the documentation change the db. And I need only to perform a simple GET request.
Also we need to setup the policy and specify the actions for it.
Api Gateway request:
https://i.ibb.co/2hkdVqZ/AWS.png
Api Gateway Policy:
https://i.ibb.co/vk8pLzd/AWS2.png
The AWS API is for creating/changing the DB server itself, as you have mentioned. You can't query the RDS database directly from the AWS API. You have to create a DB connection to the PostgreSQL database using traditional database drivers in order to run queries against the database.
You will need to use a Lambda function to accomplish what you are trying to achieve.

What parts of the AWS should I use for a web portal?

I am currently working on a web portal for a foundation. Applicants for a grant will receive access data in advance independently of this portal. New applications will then be created and processed in the portal itself. Once an application is complete, it is sent off. Later the application will be approved or rejected.
There are a number of technical specifications on which I have no influence. The frontend should be implemented using Html+Javascript. The backend should use the Amazon Web Services (AWS). If there is a need to program something for the backend - then C# should be used.
I know how to implement the classic client-server solution. At the moment, however, AWS offers me an unmanageable set of services. And here I'm hoping for suggestions as to which of the services I should take a closer look at. Ideally, no complete 'server solution' should run on a virtual server. Instead, Lambda functions are mentioned again and again. So would Amazon RDS and AWS Lambda be a sensible and sufficient combination? Did I miss something?
Thank you very much for your suggestions.
One solution would be to use AWS S3 to server HTML, CSS, JS, Images and other static content. You could use AWS Lambda via AWS API Gateway to serve as a backend. AWS Lambda would then connect to AWS RDS or AWS DynamoDB if you would prefer a NoSQL solution.
Image taken from AWS Github repo
You can get a more detailed description of how to set this up at
https://github.com/aws-samples/aws-serverless-workshops/tree/master/WebApplication/

AWS Lambda + API Gateway + AWS Elasticsearch experiencing timeouts

I've recently gotten into AWS Serverless Architecture with .NET Core 1.0. In my application we use Elasticsearch on its own machine in order to maintain it. What I am trying to do is use AWS Elasticsearch Service from AWS API Gateway which is being proxied by AWS Lambda. (I believe I have typed this correctly)
When ever my code accesses my Elasticsearch domain I receive a timeout error. As of right now, my Elasticsearch domain is left wide open so anyone can access the information. I would like to lock it down for only the API Gateway and Lamda function.
I've tried messing with the policies and roles to no success. Has anyone tried to do what I am trying to do, if so, how were they able to connect it? Or is there a better way?
The simple solution is to put all of your services out of the VPC they are in right now (I believe they are not in the same one, as your IO calls get timed out).
My answer here would give you a nice background on AWS Lambda with VPC and why external IO calls time out.
AWS lambda invoke not calling another lambda function - Node.js
note: the answer is not related to NodeJS.

Working with Amazon Web Services

I have to build an online bookstore using AWS using SQS, SES and RDS services as homework but Im at a standstill. I read through the documentations about these services provided by Amazon but I cannot figure out how to make them communicate with each other and how to set up instances with the named services. SQS should be the backbone of this store. RDS should contain users and products in stock and SES is used to notification for the customer. I search google as thoroughly as I could but could not find anything related to my problem. If anyone could give me some pointers or lead me to some reading I may have missed I would be most grateful.
These services talk to each other, but they are functionally separate. You connect to and populate an RDS database the same way you'd connect to and populate any remote MySQL database. SQS and SES both are driven through the AWS API, which you tap into using the Amazon API tools:
http://aws.amazon.com/developertools?_encoding=UTF8&jiveRedirect=1
You just create your Amazon AWS account, get your access credentials, put them into the environment variables (read the READMEs in the tools downloads) and start using them.
hope that helps.