1. Answers
  2. What Is AWS Aurora Serverless V2 In TypeScript?

What Is AWS Aurora Serverless V2 in TypeScript?

Introduction

In this guide, we will create an AWS Aurora Serverless v2 database cluster using Pulumi in TypeScript. AWS Aurora Serverless v2 is an on-demand, auto-scaling configuration for Amazon Aurora. It automatically adjusts the database’s capacity based on the application’s needs, providing cost efficiency and high availability without manual intervention.

Step-by-Step Explanation

Step 1: Set Up Pulumi Project

First, ensure you have Pulumi installed. If not, you can install it using npm:

npm install -g pulumi

Next, create a new Pulumi project:

mkdir pulumi-aurora-serverless-v2
cd pulumi-aurora-serverless-v2
pulumi new typescript

Step 2: Install AWS SDK and Pulumi AWS Package

Install the necessary packages for AWS and Pulumi:

npm install @pulumi/aws @pulumi/awsx

Step 3: Configure AWS Provider

Set up the AWS provider in your index.ts file:

import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

const provider = new aws.Provider("aws", {
    region: "us-west-2",
});

Step 4: Create a VPC

Aurora Serverless v2 requires a VPC. Create a new VPC with subnets:

const vpc = new awsx.ec2.Vpc("aurora-vpc", { provider });

Step 5: Create Aurora Serverless v2 Cluster

Create the Aurora Serverless v2 cluster:

const cluster = new aws.rds.Cluster("aurora-cluster", {
    engine: "aurora-mysql",
    engineMode: "serverless",
    scalingConfiguration: {
        autoPause: true,
        minCapacity: 2,
        maxCapacity: 16,
    },
    masterUsername: "admin",
    masterPassword: "password",
    skipFinalSnapshot: true,
    vpcSecurityGroupIds: [vpc.vpc.defaultSecurityGroup.id],
    dbSubnetGroupName: vpc.vpc.privateSubnetIds,
}, { provider });

Step 6: Export the Cluster Endpoint

Finally, export the cluster endpoint:

export const clusterEndpoint = cluster.endpoint;

Summary

In this guide, we created an AWS Aurora Serverless v2 database cluster using Pulumi in TypeScript. We set up a new Pulumi project, configured the AWS provider, created a VPC, and then created the Aurora Serverless v2 cluster. This setup allows for an auto-scaling, cost-efficient database solution that adjusts to your application’s needs. Pulumi makes it easy to manage and deploy cloud infrastructure using familiar programming languages.

Full Code Example

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

// Create a new VPC
const vpc = new awsx.ec2.Vpc("aurora-vpc");

// Create a security group for the Aurora cluster
const securityGroup = new aws.ec2.SecurityGroup("aurora-sg", {
    vpcId: vpc.vpcId,
    ingress: [{
        protocol: "tcp",
        fromPort: 3306,
        toPort: 3306,
        cidrBlocks: ["0.0.0.0/0"],
    }],
    egress: [{
        protocol: "-1",
        fromPort: 0,
        toPort: 0,
        cidrBlocks: ["0.0.0.0/0"],
    }],
});

// Create an Aurora Serverless v2 cluster
const cluster = new aws.rds.Cluster("aurora-cluster", {
    engine: "aurora-mysql",
    engineMode: "serverless",
    scalingConfiguration: {
        autoPause: true,
        minCapacity: 2,
        maxCapacity: 16,
    },
    masterUsername: "admin",
    masterPassword: "password",
    skipFinalSnapshot: true,
    vpcSecurityGroupIds: [securityGroup.id],
    dbSubnetGroupName: new aws.rds.SubnetGroup("aurora-subnet-group", {
        subnetIds: vpc.privateSubnetIds,
    }).name,
});

// Create an auto-scaling target
const scalingTarget = new aws.appautoscaling.Target("aurora-scaling-target", {
    maxCapacity: 16,
    minCapacity: 2,
    resourceId: pulumi.interpolate`cluster:${cluster.id}`,
    scalableDimension: "rds:cluster:ReadReplicaCount",
    serviceNamespace: "rds",
});

// Create an auto-scaling policy
const scalingPolicy = new aws.appautoscaling.Policy("aurora-scaling-policy", {
    policyType: "TargetTrackingScaling",
    resourceId: scalingTarget.resourceId,
    scalableDimension: scalingTarget.scalableDimension,
    serviceNamespace: scalingTarget.serviceNamespace,
    targetTrackingScalingPolicyConfiguration: {
        targetValue: 50.0,
        predefinedMetricSpecification: {
            predefinedMetricType: "RDSReaderAverageCPUUtilization",
        },
    },
});

// Export the cluster endpoint
export const clusterEndpoint = cluster.endpoint;

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up