Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

AWS Logo
Menu
Q-Bits: Provisioning RDS with Terraform and Q Developer

Q-Bits: Provisioning RDS with Terraform and Q Developer

This blog post explores how Amazon Q Developer enhances Terraform workflows by providing real-time explanations, suggesting best practices, and generating code.

Dr. Rahul Gaikwad
Amazon Employee
Published Jan 25, 2025
Last Modified Feb 17, 2025
Welcome to another installment of Q-Bits, our regular series showcasing cool ways Amazon employees are leveraging Amazon Q Developer. Today, we're diving into how Q Developer can assist with provisioning RDS instance using Terraform.

A Transformative Journey

Few years back, I transitioned myself from Database Administration (DBA) to DevOps, where I got opportunity to dive into the world of infrastructure automation with Terraform. As a newcomer, I found myself navigating the complexities of HashiCorp Configuration Language (HCL) to provision AWS resources, particularly focusing on RDS database. Although my focus was solely on database resources, the IaC devlopmenet process took a significant investment of time and effort.
Now, as I started my new role as a Solutions Architect for Financial Service Industry (FSI) customers, I struggled with forgotten RDS configurations. Now, with Amazon Q Developer at my side, I was no longer alone in navigating the complexities of Terraform and AWS configurations.

RDS Provisioning re-imagined with Terraform and Q Developer

While revisiting my old backup, I dove into my legacy RDS Terraform configurations and curious about parameters like parameter_group and option_group. Rather than digging through Terraform documentation again, I took a smarter approach. I highlighted the configuration lines, right-clicked, and chose "Amazon Q > Explain" for instant clarity.
Image not found
Amazon Q Developer Explain
Amazon Q Developer swiftly provided a clear explanation, significantly saving my both time and effort.
Image not found
Amazon Q Developer Response
As you can see, my legacy code does not adhere to best practices and lacks advanced configurations. Let's see how we can refactor it with the assistance of Amazon Q Developer.

Enhancing the Configurations

To stay current with the latest Infrastructure as Code (IaC) standards and improve my existing code, I leveraged Amazon Q Developer to refactor my legacy Terraform configurations. My goal was to ensure high availability through multi-Availability Zone deployments and boost read performance by implementing read replicas. I used the following prompt:
refactor my legacy Terraform configuration to ensure high availability using multi-AZ and improve read performance with read replicas
Amazon Q Developer quickly analysed my code in the editor and refactored it, generating new Terraform configurations that implement Multi-AZ deployment for enhanced high availability and incorporate read replicas to improve read performance. Below is the upgraded 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
resource "aws_db_instance" "primary" {
allocated_storage = 10
identifier = "test-rds-primary"
engine = "mysql"
engine_version = "8.0.39"
instance_class = "db.m5d.large"
db_name = "testdb"
username = "admin"
password = "XXXXXXXXX"
skip_final_snapshot = true

# Enable Multi-AZ deployment
multi_az = true

# Enable enhanced monitoring
monitoring_interval = 60

# Add tags
tags = {
Name = "Primary DB Instance"
}
}

# Create a read replica
resource "aws_db_instance" "read_replica" {
instance_class = "db.m5d.large"
identifier = "test-rds-replica"

# Specify the source database
replicate_source_db = aws_db_instance.primary.id

# You can optionally enable Multi-AZ for the read replica too
multi_az = true

# Auto minor version upgrade
auto_minor_version_upgrade = true

# Add tags
tags = {
Name = "Read Replica"
}
}
I noticed several significant enhancements implemented by Amazon Q Developer, including :
  • High Availability:
    • Added multi_az = true to enable Multi-AZ deployment
    • The primary instance will now have a synchronous standby replica in a different AZ
    • Automatic failover protection in case of infrastructure failure
  • Read Performance:
    • Added a read replica configuration
    • The read replica is asynchronously updated and can handle read-only queries
    • You can create multiple read replicas by duplicating the read replica resource block
In addition to refactoring the code, Amazon Q Developer provided valuable insights and enhancements focused on security best practices, as outlined below:
  1. Replace the password with a secure method like AWS Secrets Manager
  2. Adjust the instance class and storage based on your workload
  3. Configure appropriate security groups and subnet groups
  4. Consider enabling encryption at rest if handling sensitive data

Configuring backup and maintenance

To finalize my infrastructure setup, I needed to configure automated backups and maintenance windows. Instead of manually writing the configuration, I leveraged Amazon Q Developer's intelligent code generation capabilities. By simply providing a below description of my needs in the chat, it promptly generated the complete and accurate configurations.
Configure a backup solution with a 7-day retention policy. Additionally, configure maintenance windows to minimize service interruption
Image not found
Amazon Q Developer suggesting DB Backup Configuration
Beyond the primary database, Amazon Q Developer intelligently configured backups for the Read Replica as well. This demonstrates its understanding of best practices for data protection in a replicated environment. The recommendation was so quick, saved my time and effort on searching syntax on Terraform documentation.
Image not found
Amazon Q Developer suggesting DB Backup Configuration for Read Replica

Adding new resource

As part of Database Administrator routine, I wanted to add a new resource to manage RDS database instance snapshots. I simply asked Amazon Q Developer for assistance by adding a single line comment in the chat. Within seconds, it generated the required code on the fly, making the process incredibly efficient.
Image not found
Amazon Q Developer generates in-line code
After completing the code refactoring, I proceeded to deploy my RDS instance by simply executing the command terraform deploy. To my satisfaction, the process was swift and efficient, with Terraform immediately provisioning my RDS infrastructure exactly as specified in the configuration files.
Image not found
RDS in AWS Console
Ultimately, Amazon Q Developer allowed me to achieve more in less time, resulting in significant cost savings and a more efficient use of resources.

Conclusion

Amazon Q Developer has significantly transformed my Terraform workflow. It not only provided immediate explanations for configurations I was unfamiliar with but also recommended modern best practices tailored to my needs. Impressively, it anticipated my infrastructure requirements and generated code for new resources seamlessly. This powerful tool has not only saved me valuable time but also ensured that my AWS setup is both optimized and secure. With its deep integration into the AWS ecosystem, Amazon Q Developer has become an essential asset in enhancing my productivity and confidence in managing cloud infrastructure.
As we continue to explore the capabilities of Amazon Q Developer, it’s evident that it is becoming an essential resource for developers, whether working on personal projects or professional tasks. Stay tuned for more Q-Bits, where we will showcase how Amazon Q Developer is transforming our development processes. If interested, check out this AWS blog on Accelerate your Terraform development with Amazon Q Developer and hands-on workshop Terraform expertise: Accelerate AWS deployments with modular IaC & AI.
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments

Log in to comment