Before you begin
Before starting the tutorial, do the following:- Create a CockroachDB Cloud account.
- Create an AWS account.
- Create an AWS user with administrator permissions.
- Install the AWS CLI.
Step 1. Create a CockroachDB Standard cluster
- . If this is your first CockroachDB Cloud organization, it will be credited with $400 in to get you started.
- On the Get Started page, click Create cluster.
- On the Select a plan page, select Standard.
- On the Cloud & Regions page, select a cloud provider (GCP or AWS).
- In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Standard clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
- Click Next: Capacity.
- On the Capacity page, keep the at the default value of 2 vCPUs. Click Next: Finalize.
- On the Finalize page, name your cluster. If an active free trial is listed in the right pane, you will not need to add a payment method, though you will need to do this by the to maintain your organization’s clusters. Click Create cluster. Your cluster will be created in a few seconds and the Create SQL user dialog will display.
The connection string is pre-populated with your username, cluster name, and other details, including your password. Your password, in particular, will be provided only once. Save it in a secure place (we recommend a password manager) to connect to your cluster in the future. If you forget your password, you can reset it by going to the SQL Users page for the cluster, found at
https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users.Step 2. Get the sample code
Open a terminal window and clone the sample code’s GitHub repo:- node.js
- Python
The Node.js function code is available under the This function uses the node-postgres modules to connect to CockroachDB.
examples-aws-lambda/node directory:Step 3. (Optional) Create the deployment package
This step is optional, as you do not need to create a new deployment package to deploy the sample function. The
examples-aws-lambda repo includes deployment packages that are ready to deploy.-
Navigate to the Node.js function directory:
-
Install the code dependencies:
-
Compress the project files to a ZIP file for deployment:
Step 4. Configure AWS
-
Configure the AWS CLI to authenticate with your AWS account:
Follow the prompts to authenticate as a user with administrator privileges. We do not recommend using the root user.
-
Create an execution role for the Lambda function and attach the
AWSLambdaBasicExecutionRolepolicy to the role:The Lambda function needs this role to run.
Step 5. Deploy the function to AWS Lambda
-
In the deployment package directory, use the AWS CLI to create a Lambda function:
Where:
<regionis the region closest to your CockroachDB deployment.<account-idis your AWS account ID.<connection-stringis the connection string to the CockroachDB cluster.
-
Invoke the function:
Step 3. (Optional) Create the deployment package
This step is optional, as you do not need to create a new deployment package to deploy the sample function. Theexamples-aws-lambda repo includes deployment packages that are ready to deploy.
-
Navigate to the Python function directory:
-
Download and install the
psycopg2-binaryPython library to a new directory:
To run on Amazon Linux distributions,
pscyopg2 dependencies must be compiled for Linux.-
Compress the project files to a ZIP file for deployment:
Step 4. Configure AWS
-
Configure the AWS CLI to authenticate with your AWS account:
Follow the prompts to authenticate as a user with administrator privileges. We do not recommend using the root user.
-
Create an execution role for the Lambda function and attach the
AWSLambdaBasicExecutionRolepolicy to the role:The Lambda function needs this role to run.
Step 5. Deploy the function to AWS Lambda
-
In the deployment package directory, use the AWS CLI to create a Lambda function:
Where:
<regionis the region closest to your CockroachDB deployment.<account-idis your AWS account ID.<connection-stringis the connection string to the CockroachDB cluster.
To connect to a CockroachDB Standard cluster with Psycopg2, you must provide the client with a valid CA certificate. By default, Pscyopg2 searches for the certificate at
~/.postgresql/root.crt, or in the environment variable PGSSLROOTCERT.-
Invoke the function:

