Skip to content

Commit a961d22

Browse files
committed
Enables configurable EBS volume types
Allows users to specify different EBS volume types for both the root and data volumes, providing flexibility and cost optimization. Adds a validation script to verify the Terraform configuration and required variables.
1 parent b331947 commit a961d22

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

terraform/aws/main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ provider "aws" {
2525
data "aws_ami" "ubuntu" {
2626
most_recent = true
2727
owners = ["099720109477"] # Canonical
28-
28+
2929
filter {
3030
name = "name"
3131
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
3232
}
33-
33+
3434
filter {
3535
name = "virtualization-type"
3636
values = ["hvm"]
3737
}
38-
38+
3939
filter {
4040
name = "root-device-type"
4141
values = ["ebs"]
@@ -135,7 +135,7 @@ resource "aws_security_group" "main" {
135135
resource "aws_ebs_volume" "data" {
136136
availability_zone = aws_subnet.main.availability_zone
137137
size = var.data_volume_size
138-
type = "gp3"
138+
type = var.data_volume_type
139139
encrypted = true
140140

141141
tags = {
@@ -155,7 +155,7 @@ resource "aws_instance" "main" {
155155

156156
root_block_device {
157157
volume_size = 30
158-
volume_type = "gp3"
158+
volume_type = var.root_volume_type
159159
encrypted = true
160160
}
161161

terraform/aws/terraform.tfvars.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ instance_type = "t3.medium"
2222
# EBS volume size for data (GiB)
2323
data_volume_size = 50
2424

25+
# Storage types
26+
# data_volume_type = "gp3" # gp3 (SSD), st1 (HDD throughput optimized, min 125 GiB), sc1 (HDD cold, min 125 GiB)
27+
# root_volume_type = "gp3" # gp3 (SSD), gp2 (older SSD)
28+
2529

2630
# SECURITY
2731
# -------------------------

terraform/aws/validate.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
set -e
3+
4+
RED='\033[0;31m'
5+
GREEN='\033[0;32m'
6+
YELLOW='\033[1;33m'
7+
NC='\033[0m'
8+
9+
echo "Validating Terraform configuration..."
10+
echo
11+
12+
# Check terraform
13+
if ! command -v terraform &> /dev/null; then
14+
echo -e "${RED}ERROR: Terraform not installed${NC}"
15+
exit 1
16+
fi
17+
echo -e "${GREEN}OK${NC} Terraform $(terraform version -json | grep -o '"version":"[^"]*' | cut -d'"' -f4)"
18+
19+
# Check AWS CLI
20+
if command -v aws &> /dev/null && aws sts get-caller-identity &> /dev/null 2>&1; then
21+
ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
22+
echo -e "${GREEN}OK${NC} AWS credentials (Account: $ACCOUNT)"
23+
else
24+
echo -e "${YELLOW}WARN${NC} AWS credentials not configured"
25+
fi
26+
27+
# Init
28+
terraform init -backend=false > /dev/null 2>&1 || { echo -e "${RED}ERROR: Terraform init failed${NC}"; exit 1; }
29+
echo -e "${GREEN}OK${NC} Terraform init"
30+
31+
# Validate
32+
terraform validate > /dev/null 2>&1 || { echo -e "${RED}ERROR: Validation failed${NC}"; terraform validate; exit 1; }
33+
echo -e "${GREEN}OK${NC} Configuration valid"
34+
35+
# Check terraform.tfvars
36+
if [ ! -f "terraform.tfvars" ]; then
37+
echo -e "${RED}ERROR: terraform.tfvars not found${NC}"
38+
echo "Run: cp terraform.tfvars.example terraform.tfvars"
39+
exit 1
40+
fi
41+
42+
# Check required variables
43+
grep -q "ssh_key_name.*=" terraform.tfvars && ! grep -q 'ssh_key_name.*=.*""' terraform.tfvars || \
44+
{ echo -e "${RED}ERROR: ssh_key_name not set in terraform.tfvars${NC}"; exit 1; }
45+
46+
echo -e "${GREEN}OK${NC} Required variables configured"
47+
48+
# Plan
49+
echo
50+
echo "Running terraform plan..."
51+
if terraform plan -out=tfplan > /tmp/tfplan.log 2>&1; then
52+
RESOURCES=$(terraform show -json tfplan 2>/dev/null | grep -o '"to_create":[0-9]*' | cut -d: -f2)
53+
echo -e "${GREEN}OK${NC} Plan successful (${RESOURCES} resources to create)"
54+
else
55+
echo -e "${RED}ERROR: Plan failed${NC}"
56+
cat /tmp/tfplan.log
57+
exit 1
58+
fi
59+
60+
echo
61+
echo "Validation complete. Ready to deploy."
62+
echo "Run: terraform apply tfplan"
63+
echo

terraform/aws/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ variable "data_volume_size" {
2222
default = 50
2323
}
2424

25+
variable "data_volume_type" {
26+
description = "EBS volume type for data disk (gp3 for SSD, st1 for HDD throughput optimized, sc1 for HDD cold)"
27+
type = string
28+
default = "gp3"
29+
}
30+
31+
variable "root_volume_type" {
32+
description = "EBS volume type for root disk (gp3 for SSD, gp2 for older SSD)"
33+
type = string
34+
default = "gp3"
35+
}
36+
2537
variable "ssh_key_name" {
2638
description = "Name of SSH key pair for EC2 access"
2739
type = string

0 commit comments

Comments
 (0)