Sub topic 5
Creating a DB Instance
This chapter walks through provisioning a private, encrypted PostgreSQL instance with the AWS CLI. It assumes you know basic SQL, can use the AWS CLI with IAM credentials, and understand VPCs, subnets, and security groups. You also need a VPC with private subnets in at least two Availability Zones, and a security group that allows inbound port 5432 from your application.
What you need before you create an instance
RDS places an instance in your VPC through a DB subnet group. This is a named set of subnets that RDS may use. Create the group first, with subnets from at least two Availability Zones. The extra zone matters even for a single-AZ instance, because it lets you enable Multi-AZ later without moving the instance.
aws rds create-db-subnet-group \
--db-subnet-group-name demo-subnets \
--db-subnet-group-description "Private subnets for the demo database" \
--subnet-ids subnet-0123456789abcdef0 subnet-0fedcba9876543210Replace the subnet IDs with your own.
Create the instance
The next command sets the engine, size, storage, network placement, and security options in one call.
aws rds create-db-instance \
--db-instance-identifier demo-pg \
--engine postgres \
--db-instance-class db.t4g.micro \
--allocated-storage 20 \
--storage-type gp3 \
--master-username appadmin \
--manage-master-user-password \
--db-subnet-group-name demo-subnets \
--vpc-security-group-ids sg-0123456789abcdef0 \
--no-publicly-accessible \
--storage-encrypted \
--backup-retention-period 7Each option does a specific job:
| Option | Effect |
|---|---|
| `--db-instance-class db.t4g.micro` | A small burstable class, suitable for learning and light workloads. |
| `--allocated-storage 20` with `--storage-type gp3` | 20 GiB of general-purpose SSD storage. |
| `--manage-master-user-password` | RDS generates the master password and stores it in AWS Secrets Manager. |
| `--no-publicly-accessible` | The instance gets no public IP address, so it is reachable only from inside the VPC or connected networks. |
| `--storage-encrypted` | Encrypts the storage, its backups, and its snapshots. |
| `--backup-retention-period 7` | Keeps automated backups for seven days. |
Encryption and the subnet group cannot be changed casually after creation. Enabling encryption on an existing unencrypted instance means restoring from an encrypted snapshot copy, so set it at creation time.
Wait for the instance and read its details
Creation takes several minutes while the instance moves through the creating status. Rather than polling by hand, use a waiter. When the instance is available, read its endpoint and the ARN of the secret that holds the credentials.
import boto3
rds = boto3.client("rds")
rds.get_waiter("db_instance_available").wait(DBInstanceIdentifier="demo-pg")
instance = rds.describe_db_instances(DBInstanceIdentifier="demo-pg")["DBInstances"][0]
endpoint = instance["Endpoint"]
print(endpoint["Address"], endpoint["Port"])
print(instance["MasterUserSecret"]["SecretArn"])The endpoint is a DNS name. Applications should always connect through that name and never through an IP address, because the address behind it can change during failover or maintenance.
Common errors
- The subnet group error "must contain at least two Availability Zones": add a subnet from another zone.
- The instance is
availablebut you cannot connect: check that the security group allows the client's source on the database port, and remember that a private instance is not reachable from the internet. - An instance class is rejected: not every class is offered for every engine, version, and Region. Check the combinations your Region supports.
- Costs keep accruing: a stopped or idle instance still bills for storage, and backups can outlive the instance. A later chapter covers cleanup.
Exercise
You run create-db-instance with --no-publicly-accessible, and the instance becomes available. Your laptop, outside the VPC, cannot connect to the endpoint. Is the instance broken? What would you check, and what would you not change?
Show answer
The instance is working as designed. With no public access, it has no public IP address, so clients outside the VPC cannot reach it. Connect from inside the VPC, or over a VPN or Direct Connect link, or through a bastion host. Also confirm that the security group allows the client's source on the database port. Do not make the instance public just to test, because that widens exposure for a database that should stay private.
Exercise
Why does the DB subnet group need subnets in two Availability Zones if the instance runs in only one?
Show answer
RDS requires at least two zones in the group. This lets it place the instance in any of them and lets you turn on Multi-AZ later, which puts a standby in a second zone, without rebuilding the networking.