RDS security (encryption, security groups) in AWS - Commands & Configuration
provider "aws" { region = "us-east-1" } resource "aws_security_group" "rds_sg" { name = "rds-security-group" description = "Allow access to RDS from app servers" vpc_id = "vpc-0abc123def456ghij" ingress { description = "Allow MySQL from app servers" from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = ["10.0.1.0/24"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_db_instance" "example" { identifier = "example-db" engine = "mysql" instance_class = "db.t3.micro" allocated_storage = 20 name = "exampledb" username = "adminuser" password = "StrongPass123!" parameter_group_name = "default.mysql8.0" skip_final_snapshot = true publicly_accessible = false vpc_security_group_ids = [aws_security_group.rds_sg.id] storage_encrypted = true backup_retention_period = 7 }
This Terraform file creates a security group that allows MySQL traffic only from a specific private network range (10.0.1.0/24). It then creates an RDS MySQL database instance that uses this security group to restrict access. The database storage is encrypted to protect data at rest. The database is not publicly accessible, meaning it cannot be reached from the internet.
aws_security_group: Defines network rules for who can connect to the database.
aws_db_instance: Creates the database with encryption and security group attached.
terraform init
terraform plan
terraform apply -auto-approve
-auto-approve - Automatically approve the apply step without asking for confirmationaws rds describe-db-instances --db-instance-identifier example-db
--db-instance-identifier - Specifies which database instance to describeIf you remember nothing else from this pattern, remember: encrypt your database storage and restrict network access with security groups to keep your data safe.
Practice
storage_encrypted on an AWS RDS instance do?Solution
Step 1: Understand storage encryption purpose
Storage encryption protects data saved on disk, making it unreadable without keys.Step 2: Differentiate encryption types
Encryption at rest means data on disk is encrypted; encryption in transit protects data moving over network.Final Answer:
It encrypts the data stored on the disk to protect it at rest. -> Option AQuick Check:
Storage encryption = data safe at rest [OK]
- Confusing encryption at rest with encryption in transit
- Thinking storage_encrypted controls network access
- Assuming it automatically backs up data
Solution
Step 1: Identify correct protocol and port for MySQL
MySQL uses TCP protocol on port 3306 for inbound connections.Step 2: Confirm security group rule for inbound access
Security groups must allow inbound TCP traffic on port 3306 from trusted IPs to permit database access.Final Answer:
Allow inbound TCP traffic on port 3306 from the required IP range. -> Option CQuick Check:
Inbound TCP 3306 = MySQL access allowed [OK]
- Using wrong protocol like UDP or ICMP
- Allowing outbound instead of inbound traffic
- Opening port 22 (SSH) instead of 3306
Inbound: TCP, Port 5432, Source: 192.168.1.0/24Which client IP can connect successfully?
Solution
Step 1: Understand CIDR range 192.168.1.0/24
This range includes all IPs from 192.168.1.0 to 192.168.1.255.Step 2: Check which IP falls inside this range
192.168.1.50 is inside the range; others are outside.Final Answer:
192.168.1.50 -> Option AQuick Check:
192.168.1.0/24 includes 192.168.1.50 [OK]
- Not understanding CIDR notation
- Assuming any 192.168.x.x IP is allowed
- Confusing inbound and outbound rules
storage_encrypted: true but the console shows encryption as disabled. What is the likely cause?Solution
Step 1: Understand encryption behavior on RDS creation
Encryption must be enabled when creating the instance or from an encrypted snapshot.Step 2: Identify snapshot encryption impact
If created from an unencrypted snapshot, encryption cannot be enabled later.Final Answer:
The RDS instance was created from a snapshot that was not encrypted. -> Option DQuick Check:
Unencrypted snapshot blocks encryption [OK]
- Thinking encryption can be toggled anytime
- Confusing security group issues with encryption status
- Assuming IAM permissions control encryption flag
Solution
Step 1: Enable storage encryption for data safety
Settingstorage_encryptedensures data is encrypted at rest.Step 2: Restrict access via security group to private subnet
Allowing inbound only from private subnet CIDR limits access to trusted app servers.Final Answer:
Enablestorage_encryptedon RDS and configure security group to allow inbound from private subnet CIDR only. -> Option BQuick Check:
Encryption + restricted SG = secure RDS [OK]
- Allowing open access (0.0.0.0/0) to RDS
- Disabling encryption for convenience
- Using public subnet exposing RDS to internet
Ready to prove it?
Pass the test and this pattern moves unknown
