Use a Lambda function to connect to an Amazon RDS database (2024)

I want to configure my AWS Lambda function to connect to an Amazon Relational Database Service (Amazon RDS) database using Amazon RDS Proxy.

Short description

You can create an Amazon RDS Proxy database for your Lambda function. A database proxy manages a pool of database connections and relays queries from a function. This lets a function reach high concurrency levels without exhausting database connections.

For more information, see Configuring database access for a Lambda function.

Resolution

Follow these instructions to configure Amazon RDS Proxy with a Lambda function.

Note: Amazon RDS Proxy must be in the sameAmazon Virtual Private Cloud (Amazon VPC) as the Amazon RDS database. Configure the Lambda function with Amazon VPC to access the Amazon RDS Proxy.

Step 1: Create database credentials in AWS Secrets Manager

1.Open the Secrets Manager console, and then choose Store a new secret.

2.For Secret type, choose Credentials for RDS Database.

3.Enter the user name and password for your Amazon RDS database instance.

4.For Encryption key, choose the AWS Key Management Service (AWS KMS) key that Secrets Manager uses to encrypt the secret value**.**

5.For Database, choose your database, and then choose Next.

6.For Secret name, enter a name, choose Next, choose Next again, and then choose Store.

7.In Secrets, choose the Secrets Manager secret that you created.

8.In Secret ARN, copy the ARN to use in another step.

For more information, see Create an AWS Secrets Manager database secret.

Step 2: Create an AWS Identity and Access Management (IAM) policy and role for Amazon RDS Proxy

Create an IAM role with permission to use the secret, and then create a trust policy that allows Amazon RDS to assume the role.

Example IAM Secrets Manager policy

Note: Replace Secret_ARN with the Secrets Manager ARN that you previously copied**.**

{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "secretsmanager:GetResourcePolicy", "secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret", "secretsmanager:ListSecretVersionIds" ], "Resource": [ "[Secret_ARN]" ] }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "secretsmanager:GetRandomPassword", "secretsmanager:ListSecrets" ], "Resource": "*" } ]}

Example IAM RDS trust policy

{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "rds.amazonaws.com" }, "Action": "sts:AssumeRole" } ]}

Step 3: Create and attach an RDS proxy to a Lambda function

1.Open the Functions page in the Lambda console.

2.In Functions, choose your Lambda function.

3.Choose Configuration, and then choose Add database proxies.

4.Enter the following variables:
Proxy identifier: The name of the proxy.
RDS DB instance: A supported MySQL or PostgreSQL DB instance or cluster.
Secret: The Secrets Manager that you created.
IAM role: The IAM role that you created.
Authentication: Choose Password to connect with database credentials or choose Execution role to use the function's IAM credentials for authentication.

5.Choose Add.

The proxy creation takes a few minutes to complete. When the proxy is available, configure your function to connect to the proxy endpoint instead of the database endpoint. For more information, see Creating a database proxy (console).

Step 4: (Optional) Confirm that your Amazon RDS Proxy is using IAM authentication

The following steps are required only if you're using the Lambda execution role to authenticate to Amazon RDS Proxy.

1. Open the Amazon RDS console.

2. In the navigation pane, choose Proxies, and then choose your proxy.

3. Choose Actions, and then choose Modify.

4. In Connectivity module, make sure that IAM Authentication is set to Required.

Note: If you experience connection issues, then seeWhy can't I connect to my Amazon RDS DB or Amazon Aurora DB instance using RDS Proxy?For more information, see Using Amazon RDS Proxy with AWS Lambda.

Related information

How do I troubleshoot connection timeout errors from Lambda when trying to access an Amazon RDS DB instance?

How do I configure a Lambda function to connect to an RDS instance?

How do I resolve the "Lambda could not update the function's execution role" error when attaching Amazon RDS Proxy to a Lambda function?

Use a Lambda function to connect to an Amazon RDS database (2024)

FAQs

How to connect Amazon RDS to a Lambda function? ›

Go to the AWS Lambda console and select the Lambda function you want to enable RDS Proxy. The Lambda function must have a configuration that allows access to the same subnets and VPC as the RDS database. Go to the bottom of the Lambda configuration page and select Add database proxy.

Can AWS Lambda connect to a database? ›

You can connect a Lambda function to an Amazon Relational Database Service (Amazon RDS) database directly and through an Amazon RDS Proxy.

How to query RDS MySQL from AWS Lambda? ›

Connecting to RDS MySQL from Lambda: Within your Lambda function code, use the programming language's MySQL library or a MySQL connector to establish a connection with the RDS MySQL database. Pass the RDS endpoint, username, password, and database name to establish the connection.

Which steps are required to allow the Lambda function to access the Amazon RDS database? ›

Resolution
  • Step 1: Create database credentials in AWS Secrets Manager. ...
  • Step 2: Create an AWS Identity and Access Management (IAM) policy and role for Amazon RDS Proxy. ...
  • Step 3: Create and attach an RDS proxy to a Lambda function. ...
  • Step 4: (Optional) Confirm that your Amazon RDS Proxy is using IAM authentication.

Which of the following should you implement to connect the AWS Lambda function to its RDS instance? ›

To connect a Lambda function to an RDS instance, set the networking configurations to allow the connection. There are different configuration settings for each of the following connection types: A Lambda function and RDS instance in the same VPC. A Lambda function and RDS instance in different VPCs.

How to connect Lambda to sql server? ›

Create an AWS Lambda Function
  1. Download the CData JDBC Driver for SQL Server installer, unzip the package, and run the JAR file to install the driver.
  2. Create a new AWS Lambda Java Project in Eclipse using the AWS Toolkit for Eclipse. ...
  3. Add the CData JDBC Driver for SQL Server JAR file (cdata.

What is the difference between RDS and Aurora? ›

One of the biggest differences between RDS and Aurora is that RDS is an overlay technology based on existing databases such as Oracle, MySQL, and Microsoft SQL Server, while Aurora is a separate option that has been built from the ground up by AWS.

What allows a Lambda function to access AWS resources? ›

A Lambda function's execution role is an AWS Identity and Access Management (IAM) role that grants the function permission to access AWS services and resources. For example, you might create an execution role that has permission to send logs to Amazon CloudWatch and upload trace data to AWS X-Ray.

How to connect to AWS Lambda? ›

Go to the Amazon Connect console, at https://console.aws.amazon.com/connect/ .
  1. Choose your Amazon Connect instance alias.
  2. On the navigation menu, choose Flows.
  3. In the AWS Lambda section, use the Lambda Functions dropdown box to select MyFirstConnectLambda.
  4. Choose Add Lambda Function.

How does Lambda function work in AWS? ›

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. These events may include changes in state or an update, such as a user placing an item in a shopping cart on an ecommerce website.

How to setup RDS proxy for Lambda? ›

Create and attach a proxy to a Lambda function

Scroll to the bottom of your Lambda configuration page and choose Add Database Proxy. Follow the Add database proxy wizard, and fill in the Proxy Identifier and select your RDS Database. Then choose the Secrets Manager secret and the IAM role you created earlier.

Can Amazon SQS trigger a lambda function? ›

You can use an AWS Lambda function to process messages in an Amazon SQS queue. Lambda polls the queue and invokes your Lambda function synchronously with an event that contains queue messages.

How to connect API to Lambda function? ›

Adding an endpoint to your Lambda function
  1. Open the Functions page of the Lambda console.
  2. Choose a function.
  3. Under Function overview, choose Add trigger.
  4. Select API Gateway.
  5. Choose Create an API or Use an existing API. New API: For API type, choose HTTP API. ...
  6. For Security, choose Open.
  7. Choose Add.

Top Articles
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 6416

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.