Skip to content

AWS Lambda connection strings

Learn how to securely use your PlanetScale MySQL connection strings with AWS Lambda Functions

Introduction

In this guide, you'll learn how to properly store and use PlanetScale MySQL connection strings for use in AWS Lambda Functions. We'll use a pre-built NodeJS app for this example, but you can follow along using your own application as well.

Prerequisites

Set up the database

Note

If you already have a database with a production branch, skip to the next section.

Let's start by creating the database. In the PlanetScale dashboard, click the "New database" button followed by "Create new database". Name the database lambda-connection-strings, or any other name that you prefer. Click "Create database".

The PlanetScale dashboardThe PlanetScale dashboard

The new database modalThe new database modal

Once your database has finished initializing, access the console of the main branch by heading to Branches in the top nav, followed by main, then Console.

The dashboard branches tabThe dashboard branches tab

The consoleThe console

Create a simple table & insert some data using the following script:

Copied
CREATE TABLE Tasks(
Id int PRIMARY KEY AUTO_INCREMENT,
Name varchar(100),
IsDone bit
);
INSERT INTO Tasks (Name) VALUES ('Clean the kitchen');
INSERT INTO Tasks (Name) VALUES ('Fold the laundry');
INSERT INTO Tasks (Name) VALUES ('Watch the sportsball game');

You may run SELECT * FROM Tasks to ensure the data was properly added from the console.

Records from the consoleRecords from the console

Now we need to promote the main branch to production. Click the Overview tab, then Promote a branch to production. Since there is only one branch, it will be selected by default in the confirmation modal. Click on Promote branch.

The option to promote a branchThe option to promote a branch

The modal to promote to production branchThe modal to promote to production branch

Before moving on from the PlanetScale dashboard, grab the connection details to be used in the next step. Click on the Connect button in the upper right, select NodeJS from the Connect with dropdown, and note the details in the .env tab of the modal. These details will be required to connect to the database.

The dashboard after the database has been promotedThe dashboard after the database has been promoted

The connection string for this databaseThe connection string for this database

Configure the Lambda function

Secrets in AWS Lambda functions, which include database connection strings, are often stored as environment variables with the Lambda function. We’ll be uploading a sample NodeJS app that has been provided and storing the connection string from the previous section as an environment variable to test.

Start by cloning the following Git repository:

Copied
git clone https://github.com/planetscale/aws-connection-strings-example.git

Log into the AWS Console, use the universal search to search for ‘Lambda’, and select it from the list of services.

Search for Lambda in the AWS ConsoleSearch for Lambda in the AWS Console

Create a new function using the Create function button in the upper right of the console.

The default view of Lambda functionsThe default view of Lambda functions

Name your function lambda-connection-strings (or any other name that suits you) and select NodeJS under Runtime. The other fields can be left as default. Click Create function to finish the initial setup of your Lambda.

The view to create a Lambda functionThe view to create a Lambda function

On the next view, about halfway down the page you’ll see a section called Code source. Click the Upload from button, then .zip file.

The default NodeJS Lambda functionThe default NodeJS Lambda function

Click the Upload button which will display a file browser. Select the aws-connection-strings-example.zip file from the dist folder of the provided repository. Click Save once it’s been selected.

The modal to upload codeThe modal to upload code

The contents of the code editor under Code source should have updated to show the code stored in the zip file.

The code of the Lambda function that was uploadedThe code of the Lambda function that was uploaded

Configure environment variables

Next, you need to set the PlanetScale DATABASE_URL environment variable that you copied earlier. Select the Configuration tab, and click Edit.

The configuration tabThe configuration tab

You’ll be presented with a view to add or update environment variables. Click Add environment variable and the view will update with a row to add an environment variable. Set the Key field to DATABASE_URL and the Value to the connection string taken from the previous section. Click Save once finished.

The view to manage environment variablesThe view to manage environment variables

Finally, test the function by selecting the Test tab, and then clicking the Test button.

The test tabThe test tab

An Execution results box will display above the Test event section. If the box is green, it likely means everything executed as expected. Click the dropdown next to Details to see the results of the query. Since the results of the query were logged out to the console, they will be displayed in the Log output section.

The execution resultsThe execution results

Need help?

Get help from the PlanetScale support team, or join our GitHub Discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on August 1, 2022
Help us improve this page
PrivacyTerms© 2022 PlanetScale Inc.