Bird
Raised Fist0
AWScloud~5 mins

RDS security (encryption, security groups) in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you run a database in the cloud, you want to keep its data safe and control who can access it. RDS security helps you protect your database by encrypting its data and setting rules about which computers can connect to it.
When you want to protect sensitive data stored in your cloud database from unauthorized access.
When you need to control which servers or applications can connect to your database.
When you want to meet security rules that require data encryption at rest.
When you want to isolate your database from the public internet using network rules.
When you want to monitor and manage access to your database securely.
Config File - rds-security.tf
rds-security.tf
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.

Commands
This command sets up Terraform in the current folder by downloading necessary plugins and preparing to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will create or change before actually doing it. It helps you check your configuration.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.rds_sg will be created + resource "aws_security_group" "rds_sg" { + description = "Allow access to RDS from app servers" + id = (known after apply) + name = "rds-security-group" + vpc_id = "vpc-0abc123def456ghij" ... } # aws_db_instance.example will be created + resource "aws_db_instance" "example" { + allocated_storage = 20 + engine = "mysql" + id = (known after apply) + instance_class = "db.t3.micro" + name = "exampledb" + password = (sensitive value) + publicly_accessible = false + storage_encrypted = true + username = "adminuser" ... } Plan: 2 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.
This command creates the security group and the encrypted RDS database with the rules defined. The -auto-approve flag skips manual confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_security_group.rds_sg: Creating... aws_security_group.rds_sg: Creation complete after 2s [id=sg-0a1b2c3d4e5f6g7h8] aws_db_instance.example: Creating... aws_db_instance.example: Still creating... [10s elapsed] aws_db_instance.example: Creation complete after 1m30s [id=example-db] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
→
-auto-approve - Automatically approve the apply step without asking for confirmation
This AWS CLI command checks the details of the created RDS database to confirm encryption and security group settings.
Terminal
aws rds describe-db-instances --db-instance-identifier example-db
Expected OutputExpected
{ "DBInstances": [ { "DBInstanceIdentifier": "example-db", "DBInstanceStatus": "available", "StorageEncrypted": true, "VpcSecurityGroups": [ { "VpcSecurityGroupId": "sg-0a1b2c3d4e5f6g7h8", "Status": "active" } ], "PubliclyAccessible": false } ] }
→
--db-instance-identifier - Specifies which database instance to describe
Key Concept

If you remember nothing else from this pattern, remember: encrypt your database storage and restrict network access with security groups to keep your data safe.

Common Mistakes
Not enabling storage_encrypted in the RDS instance configuration
This leaves your database data unprotected at rest, risking exposure if storage is accessed directly.
Always set storage_encrypted = true when creating your RDS instance to enable encryption.
Setting the security group to allow access from 0.0.0.0/0 (anywhere)
This exposes your database to the entire internet, increasing risk of unauthorized access.
Limit ingress rules to only trusted IP ranges or VPC subnets that need access.
Making the RDS instance publicly_accessible = true without a strong reason
This allows anyone on the internet to attempt to connect to your database, increasing attack surface.
Keep publicly_accessible = false and use private network access via security groups.
Summary
Create a security group that allows database access only from trusted network ranges.
Create an RDS instance with storage encryption enabled and attach the security group.
Use Terraform commands to initialize, plan, and apply the configuration.
Verify the database encryption and security group settings using AWS CLI.

Practice

(1/5)
1. What does enabling storage_encrypted on an AWS RDS instance do?
easy
A. It encrypts the data stored on the disk to protect it at rest.
B. It encrypts data only during transfer between client and database.
C. It automatically backs up the database to another region.
D. It restricts access to the database based on IP addresses.

Solution

  1. Step 1: Understand storage encryption purpose

    Storage encryption protects data saved on disk, making it unreadable without keys.
  2. Step 2: Differentiate encryption types

    Encryption at rest means data on disk is encrypted; encryption in transit protects data moving over network.
  3. Final Answer:

    It encrypts the data stored on the disk to protect it at rest. -> Option A
  4. Quick Check:

    Storage encryption = data safe at rest [OK]
Hint: Storage encryption means data on disk is protected [OK]
Common Mistakes:
  • Confusing encryption at rest with encryption in transit
  • Thinking storage_encrypted controls network access
  • Assuming it automatically backs up data
2. Which of the following is the correct way to allow inbound traffic on port 3306 (MySQL) in an RDS security group?
easy
A. Allow inbound ICMP traffic on port 3306 from 0.0.0.0/0.
B. Allow outbound UDP traffic on port 3306 to any IP.
C. Allow inbound TCP traffic on port 3306 from the required IP range.
D. Allow inbound TCP traffic on port 22 from the required IP range.

Solution

  1. Step 1: Identify correct protocol and port for MySQL

    MySQL uses TCP protocol on port 3306 for inbound connections.
  2. 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.
  3. Final Answer:

    Allow inbound TCP traffic on port 3306 from the required IP range. -> Option C
  4. Quick Check:

    Inbound TCP 3306 = MySQL access allowed [OK]
Hint: Inbound TCP on port 3306 opens MySQL access [OK]
Common Mistakes:
  • Using wrong protocol like UDP or ICMP
  • Allowing outbound instead of inbound traffic
  • Opening port 22 (SSH) instead of 3306
3. Given this security group rule for an RDS instance:
Inbound: TCP, Port 5432, Source: 192.168.1.0/24
Which client IP can connect successfully?
medium
A. 192.168.1.50
B. 192.168.2.10
C. 10.0.0.5
D. 172.16.0.1

Solution

  1. 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.
  2. Step 2: Check which IP falls inside this range

    192.168.1.50 is inside the range; others are outside.
  3. Final Answer:

    192.168.1.50 -> Option A
  4. Quick Check:

    192.168.1.0/24 includes 192.168.1.50 [OK]
Hint: Check if IP fits CIDR range to allow connection [OK]
Common Mistakes:
  • Not understanding CIDR notation
  • Assuming any 192.168.x.x IP is allowed
  • Confusing inbound and outbound rules
4. You created an RDS instance with storage_encrypted: true but the console shows encryption as disabled. What is the likely cause?
medium
A. Encryption can only be enabled after instance creation.
B. The security group does not allow inbound traffic on the database port.
C. The IAM role lacks permissions to enable encryption.
D. The RDS instance was created from a snapshot that was not encrypted.

Solution

  1. Step 1: Understand encryption behavior on RDS creation

    Encryption must be enabled when creating the instance or from an encrypted snapshot.
  2. Step 2: Identify snapshot encryption impact

    If created from an unencrypted snapshot, encryption cannot be enabled later.
  3. Final Answer:

    The RDS instance was created from a snapshot that was not encrypted. -> Option D
  4. Quick Check:

    Unencrypted snapshot blocks encryption [OK]
Hint: Encryption depends on snapshot encryption at creation [OK]
Common Mistakes:
  • Thinking encryption can be toggled anytime
  • Confusing security group issues with encryption status
  • Assuming IAM permissions control encryption flag
5. You want to secure an RDS instance so only your application servers in a private subnet can connect, and data is encrypted at rest. Which combination is best?
hard
A. Disable encryption and allow inbound from 0.0.0.0/0 for easy access.
B. Enable storage_encrypted on RDS and configure security group to allow inbound from private subnet CIDR only.
C. Enable encryption but allow inbound from all IPs to avoid connection issues.
D. Use a public subnet for RDS and restrict access by username only.

Solution

  1. Step 1: Enable storage encryption for data safety

    Setting storage_encrypted ensures data is encrypted at rest.
  2. Step 2: Restrict access via security group to private subnet

    Allowing inbound only from private subnet CIDR limits access to trusted app servers.
  3. Final Answer:

    Enable storage_encrypted on RDS and configure security group to allow inbound from private subnet CIDR only. -> Option B
  4. Quick Check:

    Encryption + restricted SG = secure RDS [OK]
Hint: Encrypt data and restrict SG to private subnet [OK]
Common Mistakes:
  • Allowing open access (0.0.0.0/0) to RDS
  • Disabling encryption for convenience
  • Using public subnet exposing RDS to internet
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard