Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as On-premise datacenters.
Hence It is called as Infrastructure as a Code.
In this article, We will learn to Create RDS Instances using Terraform
Installing Terraform:
If you havn’t installed terraform yet, You can go ahead and install using the below article.
aws_access_key – It makes an API call to AWS resources from your machine.
aws_secret_key – Secret Access Key that’s associated with Access Key.
aws_region – The AWS region where you want to create all your resources.
Providers.tf:
Providers are interfaces to the services that will maintain our resources.There are many cloud providers supported by terraform such as AWS, Azure and Google Cloud, IBM, Oracle Cloud, Digital Ocean.
Hence Amazon Web Services is One Provider.
The AWS Provider requires Access_Key (Which IAM user the terraform should use ) and Secret_key (Allows Authentication) and aws_region represents where the terraform should initiate creating the infrastructure.
Main.tf
You can change the name of this file as per the requirement and based on the Directory structure.
This section can be segregated into 2 parts:
Launch an RDS instance from Scratch
Launch an RDS instance from latest snapshot
Launch RDS Instance from Scratch:
When launching an RDS Instance for the First time, You need to create Subnet groups, Security Groups , Parameter groups , If you are planning to launch it in a desired VPC and Subnet group.
If you dont have any such requirements, You can use the below terraform script to launch your first RDS instance using terraform.
Once the snapshot is found from the identifier of the DB instance, We will pass snapshot_identifier in the main script to launch RDS instance from the snapshot. Here is the script for that,