---
title: "Advantages of Terraform and AWS RDS in Database Instance"
description: "Learn how to quickly provision and configure an AWS database with Terraform and AWS RDS in this step-by-step guide."
canonical: "https://adamtheautomator.com/terraform-and-aws-rds/"
---

# Advantages of Terraform and AWS RDS in Database Instance

> Learn how to quickly provision and configure an AWS database with Terraform and AWS RDS in this step-by-step guide.

Source: https://adamtheautomator.com/terraform-and-aws-rds/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Advantages of Terraform and AWS RDS in Database Instance](https://adamtheautomator.com/wp-content/uploads/2021/10/How-To-Build-a-Database-Instance-with-Terraform-and-AWS-RDS.jpg)

# Advantages of Terraform and AWS RDS in Database Instance

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)26 October 20215 min. read

Categories: [Cloud](/category/cloud/)

Tags:[AWS](/tag/aws/)[AWS RDS](/tag/aws-rds/)[Terraform](/tag/terraform/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Building the Terraform and AWS RDS Database Configuration](#building-the-terraform-and-aws-rds-database-configuration)
*   [Creating the AWS RDS Instance with Terraform](#creating-the-aws-rds-instance-with-terraform)
*   [Verifying the AWS RDS Was Created with Terraform](#verifying-the-aws-rds-was-created-with-terraform)
*   [Conclusion](#conclusion)

One of Amazon Web Services (AWS)’s database products [Amazon Relational Database Service (RDS)](https://aws.amazon.com/rds/) typically requires a lot of clicking around in the AWS Management Console. This effort can take several minutes and if you’re DevOps or simply need to automate the process, marrying [Terraform](https://developer.hashicorp.com/terraform/enterprise/install/pre-install-checklist) and AWS RDS is a match made in heaven!

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

In this tutorial, you will learn, step-by-step, how to create a Terraform configuration for an AWS RDS instance and deploy it to the AWS cloud.

Let’s dive in!

## **Prerequisites**

This post will be a step-by-step tutorial. If you’d like to follow along, ensure you have the following in place:

*   An [Amazon Web Service (AWS) account](https://repost.aws/knowledge-center/create-and-activate-aws-account).
*   A code editor – Even though you can use any text editor to work with Terraform configuration files, you should consider [Visual Studio (VS) Code](https://code.visualstudio.com/) as it understands the HCL Terraform language well.
*   [Terraform](https://developer.hashicorp.com/terraform/enterprise/install/pre-install-checklist) – This tutorial will use Terraform v0.14.9 running on Ubuntu 18.04.5 LTS, but any operating system with [Terraform](https://adamtheautomator.com/install-terraform/) should work.
*   [Terraform authenticated to your AWS account](https://blog.gruntwork.io/authenticating-to-aws-with-the-credentials-file-d16c0fbcbf9e)

Related:[How to Install Terraform on Linux and Windows](https://adamtheautomator.com/install-terraform/)

## Building the Terraform and AWS RDS Database Configuration

Terraform is an infrastructure as code tool that allows you to build, change, and version infrastructure safely and efficiently. Terraform uses different types of configuration files. Each file is written either in [Hashicorp Control Language (HCL)](https://developer.hashicorp.com/terraform/language/files) or [JSON](https://developer.hashicorp.com/terraform/language/syntax/json). This tutorial will use HCL.

To create an AWS RDS instance with Terraform, this tutorial will use three different files which, when combined, will contain all of the instructions Terraform needs.

*   _main.tf_ – The Terraform configuration file that will contain the Terraform resource declaration and variable placeholders for the RDS instance attributes.
*   _vars.tf_ – A [Terraform variables file](https://developer.hashicorp.com/terraform/language/values/variables) containing all variables that _main.tf_ will reference. Splitting configuration items into variables is a best practice especially if you make larger Terraform configurations.
*   _provider.tf_ – A configuration file that defines the [AWS provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs). Terraform uses AWS providers to connect with Amazon account to manage or deploy/update dozens of AWS services.

Let’s get started and first create the Terraform configuration file that will create an AWS RDS database instance.

1\. Open a terminal/console on your computer with Terraform installed.

2\. Create a folder named _~/terraform-db-demo,_ then change (`cd`) the working directory to that folder. This folder will contain all of the configuration files you’ll be working with.

```bash
mkdir ~/terraform-db-demo
cd ~/terraform-db-demo
```

2\. Copy and paste the configuration below in your favorite code editor, and save it as _main.tf_ in the _~/terraform-db-demo_ directory. The _main.tf_ file is a Terraform configuration file that contains all the resources that need to be provisioned.

You’ll see that the configuration file uses the [`aws_db_instance`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance) resource. This resource contains all of the instructions to make calls to AWS APIs to provision an RDS instance given a few attributes.

You’ll also notice many of the values start with `var.`. These values are placeholders for [Terraform input variables](https://developer.hashicorp.com/terraform/language/values/variables) which you’ll define actual values for in the next step.

```bash
resource "aws_db_instance" "default" {
# Allocating the storage for database instance.
  allocated_storage    = 10
# Declaring the database engine and engine_version
  engine               = var.engine
  engine_version       = var.engine_version
# Declaring the instance class
  instance_class       = var.instance_class
  name                 = var.name
# User to connect the database instance 
  username             = var.username
# Password to connect the database instance 
  password             = var.password
  parameter_group_name = var.parameter_group_name
}
```

4\. Now, create another file inside _~/terraform-db-demo_ directory, name it _vars.tf_ and paste the content below. This variables file is necessary to declare all of the variables you’ll be referencing in the main configuration file.

You’ll see that the _main.tf_ configuration file created above references each of these variables below.

```bash
variable "engine" {}
variable "engine_version" {}     
variable "instance_class" {}
variable "name"  {}       
variable "username" {}  
variable "password" {} 
variable "parameter_group_name" {}
```

> _It is possible to include all configuration values in a single configuration file. To keep things clear and make it easier for developers and admins, it is important to break things up._

Create one more file inside the _~/terraform-db-demo_ directory, name it _terraform.tfvars,_ and paste the code below. This variables file contains the values that Terraform will use to replace the variable references inside of the configuration file.

When Terraform creates this AWS RDS instance, the instance will be:

*   A MySQL v5.7 database
*   Running on a t3.micro engine
*   With an admin username of `user1` with a password of `password`

```bash
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t3.micro"
  name                 = "mydb"
  username             = "user1"
  password             = "password"
  parameter_group_name = "default.mysql5.7"
```

6\. Finally, create one more file _~/terraform-db-demo_ directory, paste in the following code, and name it as _provider.tf_ to define the [AWS provider](https://registry.terraform.io/providers/hashicorp/aws/latest/docs).

Related:[Getting Started with the Terraform AWS Provider](https://adamtheautomator.com/terraform-aws/)

The tutorial will be creating resources in the `us-east-2` region.

```bash
provider "aws" {
   region = "us-east-2"
 }
```

7\. Now, verify all of the required files below are contained in the folder by running the **[`tree`](https://en.wikipedia.org/wiki/Tree_\(command\))** command.

![Terraform files that are required to run the terraform code](https://adamtheautomator.com/wp-content/uploads/2021/10/image-231.png)

Terraform files that are required to run the terraform code

## Creating the AWS RDS Instance with Terraform

Now that you have the Terraform configuration files ready to go, it’s time to initiate Terraform and create your first AWS RDS database instance!

To provision a Terraform configuration, Terraform typically uses a three-stage approach **[terraform init](https://developer.hashicorp.com/terraform/cli/commands/init)** → **[terraform plan](https://developer.hashicorp.com/terraform/cli/commands/plan)** → **[terraform apply](https://developer.hashicorp.com/terraform/cli/commands/apply)**. Let’s walk through each stage now.

Assuming you’re still in the _~\\terraform-db-demo_ directory:

1\. Run the `terraform init` command in the same directory. The **[`terraform init`](https://developer.hashicorp.com/terraform/cli/commands/init)** command initializes the [plugins](https://developer.hashicorp.com/terraform/plugin/how-terraform-works) and providers which are required to work with AWS resources that need to be provisioned.

```powershell
terraform init
```

If all goes well, you should see the message `Terraform has been successfully initialized` in the output, as shown below.

![Running the terraform init command](https://adamtheautomator.com/wp-content/uploads/2021/10/image-232.png)

Running the terraform init command

2\. Now, run the **[`terraform plan`](https://developer.hashicorp.com/terraform/cli/commands/plan)** command. Terraform plan command gives you an overview of which resources will be provisioned in your infrastructure. You will also see every AWS resource Terraform intends to create.

```powershell
terraform plan
```

![Running the Terraform plan command](https://adamtheautomator.com/wp-content/uploads/2021/10/image-234.png)

Running the Terraform plan command

3\. Next, tell Terraform actually to provision the database instance using `terraform apply`. When you invoke `terraform apply`, Terraform will read the configuration (_main.tf_) and the other files to compile a configuration and then send that configuration up to AWS as instructions to build the database instance.

```powershell
terraform apply
```

As you can see below Terraform has successfully created the database instance in the AWS account using Terraform.

![Running the terraform apply command](https://adamtheautomator.com/wp-content/uploads/2021/10/image-235.png)

Running the terraform apply command

## Verifying the AWS RDS Was Created with Terraform

By now, you should have created the database instance with Terraform. Let’s verify by manually checking for the database instance in the AWS Management Console.

1\. Open your favorite web browser and navigate to the **[AWS Management Console](https://aws.amazon.com/console/)** and log in.

2\. While in the Console, click on the search bar at the top, search for RDS and click on the RDS menu item.

![RDS](https://adamtheautomator.com/wp-content/uploads/2021/10/image-236.png)

RDS

3\. Once on the RDS page, click on DB Instances. You should see the DB instance created.

![DB Instances](https://adamtheautomator.com/wp-content/uploads/2021/10/image-237.png)

DB Instances

Below you can see that the `mydb` database instance has been successfully created!

![mydb database instance created](https://adamtheautomator.com/wp-content/uploads/2021/10/image-238.png)

mydb database instance created

If you dig in more, you’ll also see that the RDS instance has all of the same attributes as you had defined in your variables files.

```bash
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t3.micro"
  name                 = "mydb"
  username             = "user1"
  password             = "password"
  parameter_group_name = "default.mysql5.7"
```

## Conclusion

In this tutorial, you learned how to create your first database using Terraform and AWS RDS using the most widely used automation tool, Terraform.

Now that you have a database created on the AWS RDS, what do you plan to store in your newly created database?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-and-aws-rds%2F&text=Advantages%20of%20Terraform%20and%20AWS%20RDS%20in%20Database%20Instance)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fterraform-and-aws-rds%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-and-aws-rds%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/08/featured_image-1.webp)

### [Survive the Senior Cloud Engineer AWS Interview Loop](/senior-cloud-engineer-interview-loop/)

Prepare for a senior cloud engineer interview with AWS EKS and Control Tower system design reasoning, Terraform locking, and salary negotiation tactics.

![](https://adamtheautomator.com/wp-content/uploads/2021/09/Terraform-AWS.jpg)

### [Terraform AWS Guide: Building Robust Amazon Infrastructure](/terraform-aws/)

Master Terraform AWS with our comprehensive guide: Learn step-by-step how to manage and optimize your Amazon Infrastructure effectively and efficiently.

![](https://adamtheautomator.com/wp-content/uploads/2026/08/featured_image-7.webp)

### [Build Azure Landing Zones That Prevent Cloud Sprawl](/build-azure-landing-zones/)

Build governed Azure landing zones end to end with the IaC accelerator: management group hierarchies, the eight design areas, Terraform and Bicep deployments,

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
