Provisioning AWS RDS & S3 Bucket using Terraform
With reference to my previous article, “Infrastructure as Code (IaC): Provsion EC2 Instance and VPC on AWS Using Terraform,” that was published on AWStip, I am writing this new piece of technical information regarding the Rational Database System, i.e., RDS, and storage service, i.e., S3 bucket, of AWS using Terraform. In this tutorial, we discussed how to provision the RDS instance and S3 bucket through the Terraform template.
Prerequisites:
You can follow all the necessary steps for installation and setup in my previous article, “Infrastructure as Code (IaC): Provsion EC2 Instance and VPC on AWS Using Terraform.”
Create a Terraform Configuration Template for Amazon RDS Instance:
Create a new folder called ‘rds’ inside the ‘Terraform’ directory and create a new file’main.tf’ under the ‘rds’ folder. Put the below snippet inside the main.tf template.
provider "aws" {
region = "eu-central-1" // your AWS region
}
resource "aws_db_instance" "myRDS" {
db_name = "myDB"
identifier = "my-first-db" // name of the database
instance_class = "db.m5.large" // instance class
engine = "mariadb" // specify the SQL database
engine_version = "10.11.7" // database version
username = "bob" // authentication username
password = "password123" // authentication password
port = 3306 // port number on which Db instance is running
allocated_storage = 20
skip_final_snapshot = true
}
Run terraform INIT, PLAN and APPLY command:
Open the terminal and run the below command on terminal one by one.
terraform init
terraform plan
terraform apply
RDS Instance on AWS:
To verify the DB instance, open the Amazon RDS and click on “Databases” option and you will find the “my-first-db” instance running with the same configuration that is initialized in terraform template.
Delete the RDS instance:
Run the below command to delete the database instance that is provision using terraform.
terraform destroy
Setup s3 bucket in AWS account:
Go into the AWS account and search the s3 storage service. Go into the ‘Buckets’ option and click ‘Create bucket’. Enter the name of the bucket. I typed “mu-remote-backend-2024” as bucket name. At the end of the page, click on ‘Create bucket’ button. It will create the s3 storage bucket.
Create a Terraform Configuration Template for Amazon s3 Bucket:
Create a new folder called ‘s3’ inside the ‘Terraform’ directory and create new file ‘main.tf’ under ‘s3’ folder. Put the below snippet inside the main.tf template.
In the provider, you don’t need to mention the aws region because s3 bucket is the global service.
provider "aws" {
//region = "eu-central-1"
}
resource "aws_vpc" "test_vpc" {
cidr_block = "10.0.0.0/16"
}
Create new file under the same folder ‘s3’ named ‘backend.tf’.
terraform {
backend "s3" {
key = "image/Visum.jpeg"
bucket = "mu-remote-backend-2024"
}
}
Here in the ‘key’, I mentioned the name of the folder ‘image’ and image file ‘Visum.jpeg’. You can change it with any directory name & file name or only filename.
Run terraform INIT, PLAN and APPLY command:
Open the terminal and run the below command on terminal one by one.
terraform init
terraform plan
terraform apply
The ‘terraform init’ command ask you regarding the AWS region on the command prompt. Enter your region. I am located in Germany so I enter the region name ‘eu-central-1’, which is Frankfurt. You can enter you nearest AWS region according to your location.
Note: If you faced error’s on command prompt. Please follow the instruction on the below Github link.
Verification on AWS s3 bucket interface:
At the end, you can open the “mu-remote-backend-2024” bucket and you will find the ‘Image’ folder and inside Image folder, you will get the ‘Visum.jpeg’ file as shown in the image.
Conclusion
In summary, leveraging Terraform alongside AWS services such as RDS instances and S3 buckets provides a powerful and efficient way to manage infrastructure as code. By defining infrastructure components using Terraform configuration files, teams can easily create, modify, and version control their AWS resources in a reproducible and automated manner. The combination of Terraform and AWS services like RDS and S3 empowers teams to achieve infrastructure automation, consistency, and reliability, leading to improved agility, cost-effectiveness, and scalability in cloud environments. By embracing Terraform’s infrastructure as a code paradigm, organizations can streamline their workflows, reduce manual interventions, and accelerate the delivery of applications and services on AWS.