How do I provision a managed PostgreSQL instance on AWS RDS?
In this example, we will provision a managed PostgreSQL instance on AWS RDS. We will use essential resources such as VPC, subnet groups, and the RDS instance itself. AWS RDS (Relational Database Service) allows you to easily set up, operate, and scale a relational database in the cloud, and PostgreSQL is a powerful, open source object-relational database system.
Steps to Provision Managed PostgreSQL on AWS RDS
- Define the Provider: Configure AWS as the cloud provider.
- Create a VPC: A Virtual Private Cloud (VPC) is created to provide networking capabilities.
- Create Subnets: Subnets are created within the VPC for the RDS instance.
- Create a DB Subnet Group: This groups the subnets together for the database.
- Provision the RDS PostgreSQL Instance: Create the RDS instance with specified details.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Creating a VPC
const main = new aws.ec2.Vpc("main", {
cidrBlock: "10.0.0.0/16",
tags: {
Name: "main",
},
});
// Creating subnets for the VPC
const mainSubnet: aws.ec2.Subnet[] = [];
for (const range = {value: 0}; range.value < 2; range.value++) {
mainSubnet.push(new aws.ec2.Subnet(`main-${range.value}`, {
vpcId: main.id,
cidrBlock: [
"10.0.1.0/24",
"10.0.2.0/24",
][range.value],
availabilityZone: [
"us-west-2a",
"us-west-2b",
][range.value],
tags: {
Name: `main-subnet-${range.value}`,
},
}));
}
// Creating a DB Subnet Group
const mainSubnetGroup = new aws.rds.SubnetGroup("main", {
name: "main-subnet-group",
subnetIds: mainSubnet.map(__item => __item.id),
tags: {
Name: "main-subnet-group",
},
});
// Provisioning the RDS PostgreSQL Instance
const postgresql = new aws.rds.Instance("postgresql", {
allocatedStorage: 20,
engine: "postgres",
engineVersion: "12.5",
instanceClass: aws.rds.InstanceType.T3_Micro,
name: "mydatabase",
username: "foo",
password: "foobarbaz",
dbSubnetGroupName: mainSubnetGroup.name,
skipFinalSnapshot: true,
tags: {
Name: "mydatabase",
},
});
export const dbEndpoint = postgresql.endpoint;
export const dbInstanceId = postgresql.id;
Key Points
- VPC: A secure environment to host your AWS RDS instance.
- Subnets: Ensure the RDS instance runs in multiple availability zones for high availability.
- DB Subnet Group: Required to specify which subnets the RDS instance uses.
- RDS Instance: The PostgreSQL instance with configuration details like storage, engine version, etc.
Summary
This configuration provisions a managed PostgreSQL instance on AWS RDS within a secure VPC, along with the necessary subnets and subnet group. Exports of the database endpoint and instance ID are provided for easy reference.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.