Auto-generate CloudFormation with no-code and no commitment 🚀
Try Now

Build reliable and scalable applications with Amazon Aurora Serverless

Altostra supports Aurora Serverless to replace long, complex CloudFormation files with a no-code interface. Go from hours of work to minutes.
Yev Krupetsky

April 14 2021 · 6 min read

SHARE ON

Build reliable and scalable applications with Amazon Aurora Serverless

Managing database servers is very difficult and requires DBA and DevOps skillsets. That's why we love managed database services so much. Services like AWS RDS simplify the management of databases in the cloud considerably. To make it even simpler, AWS Aurora lets us use our favorite kind of cloud-native tech—serverless.

AWS Aurora Serverless makes it easy to deploy a database cluster. It allows for auto-scalability and provides DataAPI that enables querying the database over HTTP.

What is DataAPI?

DataAPI is a feature of Aurora Serverless that lets us connect to the Aurora cluster over HTTP (instead of TCP), which has benefits for serverless applications.

With DataAPI, we don't need to put Lambda functions inside VPCs to access the database. The most significant benefit here is avoiding longer Lambda cold-starts when a Lambda function is placed in a VPC.

Jeremy Daly published an open-source library that makes working with Aurora Serverless over DataAPI very easy. It abstracts away the HTTP interface and takes care of the connection technicalities. You can find his library data-api-client package on GitHub.

Build and deploy with Altostra

Setting up an Aurora Serverless Cluster in Altostra is easy and adds flexibility for deploying across multiple environments.

In addition to the Aurora cluster, I also created an API and a Lambda that we’ll use to access the database. You can modify the design to suit your needs.

Build

In the Altostra extension for VSCode, click the Aurora Serverless Cluster resource and provide the required information.

The resource comes with default values predefined to get started quickly, and the configuration of 2 capacity units is more than enough right now. You can adjust these values at a later time as you see fit. For now, let's just turn on DataAPI for our cluster.

Aurora Database properties

To allow a Lambda function access to the database, we simply connect the Lambda resource to the Aurora Cluster resource. During deployment, Altostra generates access policies and injects the information you need to connect to the database as Lambda environment variables. In this case, with DataAPI on, it is the cluster ARN that we'll pass to the data-api-client library.

Full project infrastructure

Deploy

Now let’s deploy the first version of our project, using the Altostra CLI:

$ alto deploy dev-1 --push --env Lab

This will create a new instance of our project in the Lab environment. We can then create more instances as needed, in the same and other environments.

I created the Lab environment in advance in the Altostra Web Console, making it easy for me to deploy without memorizing the AWS account ID and region. The environment also defines a timeout policy that auto-deletes inactive instances after a few hours.

Full infrastructure with terminal

Create a secret for the database

To connect to the database using DataAPI, you must create a secret in AWS Secrets Manager to use for authentication, and you must allow the Lambda function to read the secret's value.

Go to the AWS Console for Secrets Manager and create a new secret. The process is simple. Enter the same username and password you provided when creating the Aurora Cluster and select the DB instance from the list.

AWS create secret

Give the secret a name and description so that you know what it is.

AWS secret name

Once you finish creating the secret, copy the secret's ARN for the next step.

Add permission to access the secret

Now that you have created the secret add the following policy to your Lambda function in Altostra to grant it access to read the secret's value. Set the secret's ARN you copied earlier as the value of the Resource property in the policy.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "secretsmanager:GetSecretValue"
      ],
      "Resource": "arn:aws:secretsmanager:us-east-1:814******242:secret:lab/db-LcFSBE"
    }
  ]
}
Lambda policy

The Policies section of the Lambda Function resource allows you to add additional policies to your function, adding to the policies created automatically by Altostra.

Querying the database

Disclaimer: there are many methodologies for working with SQL databases, and it's out of the scope of this post. For simplicity of demonstration, we'll use a simple and naive approach to access the database. In production, you should adhere to best practices and security guidelines.

The function code

The code is very straightforward for demonstration purposes. It queries the database for the version and returns it. Notice the use of the environment variable DB_DATABASE01 that Altostra injects into the function.

Function code

Deploy and test

Now that we added the code and the policy, let’s deploy our project again and test that it works:

$ alto deploy dev-1 --push
Deploy to update

After the deployment completes, run:

$ alto endpoints dev-1 --text

to get the API endpoint URL. Then, use it as an argument to the curl command (don’t forget to add the API endpoint path at then end: /version).

Test with curl

Summary

In this post we saw how to get an AWS Aurora Serverless cluster up and running with Altostra and how to use DataAPI to make our solution serverless and fast with the help of an open-source library.

Aurora Serverless is an excellent solution for serverless projects that need a relational database. Although it's easy to get an Aurora Serverless Cluster up and running, you should still give it the proper attention it deserves and configure it for security, backups and monitoring.

We will cover more use cases and more complex examples of working with Aurora Serverless databases in future posts.

Next steps

Want to give it a try? We provide a free-forever plan for developers. Create your free account today at https://app.altostra.com.

Want to stay up to date with the latest Altostra news? Join our community on Discord.

We want to hear from you! Share your Altostra experience with us on Twitter @AltostraHQ and LinkedIn and any suggestions that can make your work with Altostra even better.

Happy coding!

By submitting this form, you are accepting our Terms of Service and our Privacy Policy

Thanks for subscribing!

Ready to Get Started?

Get Started for Free

Copyright © 2021 Altostra. All rights reserved.