1. Answers
  2. How do I deploy an AWS Aurora Serverless v2 with Pulumi?

How Do I Deploy an AWS Aurora Serverless V2 With Pulumi?

Introduction

In this guide, we will create an AWS Aurora Serverless v2 database cluster using Pulumi. AWS Aurora Serverless v2 is an on-demand, auto-scaling configuration for Amazon Aurora. It automatically adjusts database capacity based on application demand, providing a cost-effective solution for variable workloads.

Step-by-Step Explanation

Step 1: Set Up Pulumi

  1. Install Pulumi CLI if you haven’t already:
    curl -fsSL https://get.pulumi.com | sh
    
  2. Log in to Pulumi:
    pulumi login
    
  3. Create a new Pulumi project:
    pulumi new aws-typescript
    

Step 2: Configure AWS Provider

  1. Set up AWS credentials:
    export AWS_ACCESS_KEY_ID=your-access-key-id
    export AWS_SECRET_ACCESS_KEY=your-secret-access-key
    
  2. Configure the AWS region in Pulumi.yaml:
    config:
      aws:region: us-west-2
    

Step 3: Create Aurora Serverless v2 Cluster

  1. Install the necessary Pulumi packages:
    npm install @pulumi/pulumi @pulumi/aws
    
  2. Add the following code to index.ts to create the Aurora Serverless v2 cluster:
    import * as pulumi from "@pulumi/pulumi";
    import * as aws from "@pulumi/aws";
    
    // Create a new VPC
    const vpc = new aws.ec2.Vpc("my-vpc", {
        cidrBlock: "10.0.0.0/16",
        enableDnsHostnames: true,
        enableDnsSupport: true,
    });
    
    // Create subnets
    const subnet1 = new aws.ec2.Subnet("subnet1", {
        vpcId: vpc.id,
        cidrBlock: "10.0.1.0/24",
        availabilityZone: "us-west-2a",
    });
    
    const subnet2 = new aws.ec2.Subnet("subnet2", {
        vpcId: vpc.id,
        cidrBlock: "10.0.2.0/24",
        availabilityZone: "us-west-2b",
    });
    
    // Create a security group
    const securityGroup = new aws.ec2.SecurityGroup("aurora-sg", {
        vpcId: vpc.id,
        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 cluster
    const cluster = new aws.rds.Cluster("aurora-cluster", {
        engine: "aurora-mysql",
        engineMode: "serverless",
        scalingConfiguration: {
            autoPause: true,
            minCapacity: 2,
            maxCapacity: 16,
        },
        masterUsername: "admin",
        masterPassword: pulumi.secret("password"),
        skipFinalSnapshot: true,
        vpcSecurityGroupIds: [securityGroup.id],
        dbSubnetGroupName: new aws.rds.SubnetGroup("aurora-subnet-group", {
            subnetIds: [subnet1.id, subnet2.id],
        }).name,
    });
    
    // Export the cluster endpoint
    export const clusterEndpoint = cluster.endpoint;
    
  3. Run pulumi up to create the resources:
    pulumi up
    

Summary

In this guide, we created an AWS Aurora Serverless v2 database cluster using Pulumi. We set up the necessary VPC, subnets, and security group, and then created the Aurora cluster with auto-scaling configuration. This setup provides a cost-effective and scalable database solution for variable workloads. Pulumi makes it easy to manage and automate cloud infrastructure using code.

Full Code Example

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

// Create a new VPC
const vpc = new aws.ec2.Vpc("my-vpc", {
    cidrBlock: "10.0.0.0/16",
    enableDnsHostnames: true,
    enableDnsSupport: true,
});

// Create subnets
const subnet1 = new aws.ec2.Subnet("subnet1", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: "us-west-2a",
});

const subnet2 = new aws.ec2.Subnet("subnet2", {
    vpcId: vpc.id,
    cidrBlock: "10.0.2.0/24",
    availabilityZone: "us-west-2b",
});

// Create a security group
const securityGroup = new aws.ec2.SecurityGroup("aurora-sg", {
    vpcId: vpc.id,
    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 cluster
const cluster = new aws.rds.Cluster("aurora-cluster", {
    engine: "aurora-mysql",
    engineMode: "serverless",
    scalingConfiguration: {
        autoPause: true,
        minCapacity: 2,
        maxCapacity: 16,
    },
    masterUsername: "admin",
    masterPassword: pulumi.secret("password"),
    skipFinalSnapshot: true,
    vpcSecurityGroupIds: [securityGroup.id],
    dbSubnetGroupName: new aws.rds.SubnetGroup("aurora-subnet-group", {
        subnetIds: [subnet1.id, subnet2.id],
    }).name,
});

// 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