0
0
AWScloud~5 mins

Launching an RDS instance in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need a database that is easy to set up and manage in the cloud. Amazon RDS helps you launch a ready-to-use database without worrying about hardware or software installation.
When you want to create a new database for your web application quickly.
When you need a managed database service that handles backups and updates automatically.
When you want to test your app with a real database without setting up servers.
When you want to scale your database easily as your app grows.
When you want to avoid managing database software and focus on your app.
Config File - rds-instance.tf
rds-instance.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_db_instance" "example" {
  allocated_storage    = 20
  engine               = "mysql"
  engine_version       = "8.0"
  instance_class       = "db.t3.micro"
  name                 = "exampledb"
  username             = "adminuser"
  password             = "StrongPass123!"
  parameter_group_name = "default.mysql8.0"
  skip_final_snapshot  = true
  publicly_accessible  = true
  identifier           = "example"
}

This Terraform file sets up an AWS RDS MySQL database instance.

  • provider: Defines AWS region.
  • aws_db_instance: Creates the database with storage, engine type, version, instance size, and credentials.
  • skip_final_snapshot: Avoids creating a snapshot when deleting the instance for simplicity.
  • publicly_accessible: Allows access from outside AWS for easy testing.
  • identifier: Sets the DB instance identifier to "example" for referencing.
Commands
This command prepares Terraform to work with AWS by downloading necessary plugins and setting up the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.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 creates the RDS instance as defined in the configuration file without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_db_instance.example: Creating... aws_db_instance.example: Still creating... [10s elapsed] aws_db_instance.example: Creation complete after 1m30s [id=example] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips manual approval to speed up deployment
This command checks the status and details of the RDS instance to confirm it is running.
Terminal
aws rds describe-db-instances --db-instance-identifier example
Expected OutputExpected
{ "DBInstances": [ { "DBInstanceIdentifier": "example", "DBInstanceStatus": "available", "Engine": "mysql", "DBInstanceClass": "db.t3.micro", "Endpoint": { "Address": "example.abcdefgh.us-east-1.rds.amazonaws.com", "Port": 3306 }, "AllocatedStorage": 20 } ] }
--db-instance-identifier - Specifies which RDS instance to describe
Key Concept

If you remember nothing else from this pattern, remember: Terraform lets you define and launch an RDS database with simple code, and AWS CLI helps you check its status.

Common Mistakes
Using a weak or simple password for the database user
This can cause security risks and AWS may reject the creation if the password does not meet complexity requirements.
Use a strong password with letters, numbers, and special characters as shown in the example.
Not running 'terraform init' before 'terraform apply'
Terraform will not have the necessary plugins and will fail to apply the configuration.
Always run 'terraform init' first to prepare the environment.
Forgetting to specify the DB instance identifier when describing the instance
The AWS CLI command will return an error or no information because it doesn't know which instance to check.
Always include '--db-instance-identifier example' to target your specific RDS instance.
Summary
Write a Terraform file to define your RDS instance with storage, engine, and credentials.
Run 'terraform init' to set up Terraform with AWS.
Run 'terraform apply -auto-approve' to create the RDS instance automatically.
Use AWS CLI 'describe-db-instances' to check the status and details of your database.