Voiced by Amazon Polly |
Introduction
In this blog, we will walk through setting up an Aurora cluster using Terraform, including configuration, file structure, and deployment steps.
Prerequisites
Before starting, ensure you have:
- Basic knowledge of Terraform and AWS services.
- Terraform is installed on your local machine.
- AWS CLI configured with appropriate credentials.
- Access to an AWS account with permissions to create Amazon RDS clusters.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
File Structure and Configuration
- main.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
The main.tf file contains the primary configuration for your Aurora cluster and instances. resource "aws_rds_cluster" "aurora" { cluster_identifier = "${var.environment}-aurora-cluster" engine = "aurora-mysql" database_name = var.db_name master_username = var.db_username master_password = var.db_password engine_mode = "provisioned" backup_retention_period = 7 preferred_backup_window = "09:00-10:00" preferred_maintenance_window = "Tue:07:00-Tue:09:00" serverlessv2_scaling_configuration { max_capacity = var.max_capacity min_capacity = var.min_capacity } vpc_security_group_ids = [var.security_group_id] db_subnet_group_name = var.db_subnet_group_name tags = { Name = "${var.environment}-aurora-cluster" Description = var.description } } resource "aws_rds_cluster_instance" "writer" { cluster_identifier = aws_rds_cluster.aurora.id identifier = var.environment == "dev" ? "test" : "${aws_rds_cluster.aurora.id}-instance" instance_class = "db.serverless" engine = aws_rds_cluster.aurora.engine preferred_maintenance_window = "sat:08:00-sat:08:30" publicly_accessible = false tags = { Name = "${aws_rds_cluster.aurora.id}-instance" Environment = var.environment "Stoppable" = "False" "Restart" = "False" } } resource "aws_rds_cluster_instance" "reader" { cluster_identifier = aws_rds_cluster.aurora.id identifier = var.environment == "dev" ? "test" : "${aws_rds_cluster.aurora.id}-reader" instance_class = "db.serverless" engine = aws_rds_cluster.aurora.engine preferred_maintenance_window = "sat:08:00-sat:08:30" publicly_accessible = false tags = { Name = "${aws_rds_cluster.aurora.id}-instance" Environment = var.environment "Stoppable" = "False" "Restart" = "False" } } |
- variables.tf
The variables.tf file defines the variables used in the Terraform configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
variable "environment" { description = "The environment (dev, staging, prod, etc.)" type = string } variable "db_name" { description = "The name of the database" type = string } variable "db_username" { description = "The username for the database" type = string } variable "db_password" { description = "The password for the database" type = string } variable "security_group_id" { description = "The security group ID for the Aurora cluster" type = string } variable "db_subnet_group_name" { description = "The name of the DB subnet group" type = string } variable "min_capacity" { description = "The minimum capacity for Aurora Serverless" type = number } variable "max_capacity" { description = "The maximum capacity for Aurora Serverless" type = number } variable "auto_pause_timeout" { description = "The timeout in seconds before automatically pausing Aurora Serverless" type = number } variable "description" { description = "Description tag for resources" type = string } |
- outputs.tf
The outputs.tf file defines the output values for the Terraform configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
output "aurora_cluster_id" { description = "The ID of the Aurora cluster" value = aws_rds_cluster.aurora.id } output "aurora_writer_instance_id" { description = "The ID of the Aurora writer instance" value = aws_rds_cluster_instance.writer.id } output "aurora_reader_instance_id" { description = "The ID of the Aurora reader instance" value = aws_rds_cluster_instance.reader.id } |
- terraform.tfvars
The terraform.tfvars file contains the variable values.
1 2 3 4 5 6 7 8 9 10 |
environment = "dev" db_name = "mydatabase" db_username = "admin" db_password = "password123" security_group_id = "security-grp-id" db_subnet_group_name = "my-db-subnet-group" min_capacity = 2 max_capacity = 8 auto_pause_timeout = 300 description = "Aurora cluster for dev environment" |
Deploying the Aurora Cluster
- Initialize Terraform: Run terraform init to initialize the Terraform configuration and download the necessary provider plugins.
- Plan the Deployment: Execute terraform plan to review the planned changes and ensure everything is configured correctly.
- Apply the Configuration: Use terraform apply to create the Aurora cluster and instances based on your configuration.
Conclusion
In this blog, we covered creating an Amazon Aurora database cluster using Terraform. We outlined the necessary Terraform configuration files, provided code examples, and completed the deployment steps. Following this guide, you can efficiently set up an Amazon Aurora cluster tailored to your environment and requirements.
Drop a query if you have any questions regarding Amazon Aurora and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. Can I use this setup for MySQL and PostgreSQL Aurora clusters?
ANS: – Yes, this example uses aurora-mysql as the engine, but you can change it to aurora-postgresql if you need PostgreSQL compatibility.
2. How can I manage different environments with this configuration?
ANS: – You can manage different environments by specifying different values in terraform.tfvars for each environment (e.g., dev, staging, prod).
WRITTEN BY Yamini Reddy
Click to Comment