This Terraform repository creates an EC2 instance that matches the configuration from AWS Trusted Advisor's "Amazon EC2 cost optimization recommendations for instances" check. It demonstrates various rightsizing scenarios and cost optimization opportunities.
This repository is designed to:
- Demonstrate AWS Trusted Advisor rightsizing recommendations
- Provide a practical example of cost optimization opportunities
- Show how to implement monitoring for rightsizing decisions
- Serve as a testing ground for different instance types and configurations
Based on the AWS Trusted Advisor data, this instance represents several optimization scenarios:
Scenario | Current Type | Recommended Type | Action | Monthly Savings |
---|---|---|---|---|
Rightsize | t3.large | t2.large | Downgrade | $48.00 (60%) |
Upgrade | t2.large | t3.large | Upgrade | $48.00 (60%) |
Stop | Any | N/A | Stop unused | $48.00 (60%) |
Graviton Migration | m4.4xlarge | x8g.xlarge | Migrate | $48.00 (60%) |
Trusted Advisor Check ID: c1z7kmr00n
Current Monthly Cost: ~$80.00
Potential Savings: ~$48.00 (60%)
This Terraform configuration creates:
- VPC with public subnet and internet gateway
- EC2 Instance (t3.large by default) with detailed monitoring
- Security Group with SSH, HTTP, and HTTPS access
- CloudWatch Alarms for CPU utilization monitoring
- Key Pair for SSH access
- Web Server with rightsizing information dashboard
- AWS CLI configured with appropriate credentials
- Terraform >= 1.0 installed
- SSH key pair for EC2 access
-
Clone and navigate to the repository:
git clone <repository-url> cd test-repo
-
Configure variables:
cp terraform.tfvars.example terraform.tfvars
Edit
terraform.tfvars
and update:public_key
: Your SSH public keyaws_region
: Your preferred AWS region (default: us-east-1)instance_type
: Instance type to deploy (default: t3.large)
-
Initialize and deploy:
terraform init terraform plan terraform apply
-
Access the instance:
# SSH access ssh -i ~/.ssh/your-key ec2-user@<public-ip> # Web dashboard http://<public-ip>
The configuration includes CloudWatch alarms for:
- High CPU utilization (>80%) - indicates potential need for larger instance
- Low CPU utilization (<10%) - indicates rightsizing opportunity
A monitoring script is installed at /home/ec2-user/monitor.sh
that provides:
- CPU usage statistics
- Memory utilization
- Disk usage
- Network statistics
- Top processes
Run manually or check hourly logs at /var/log/instance-monitoring.log
.
Access the web dashboard at http://<instance-public-ip>
to view:
- Instance information and current configuration
- Rightsizing recommendations
- Key metrics (simulated)
- Best practices for cost optimization
To test rightsizing recommendations, modify the instance_type
variable:
# In terraform.tfvars
instance_type = "t2.large" # Test downgrade recommendation
instance_type = "t3.medium" # Test smaller alternative
instance_type = "t3.xlarge" # Test larger alternative
instance_type = "m5.large" # Test different family
Then apply the changes:
terraform plan
terraform apply
- Monitor CPU, memory, and network utilization over 2-4 weeks
- Look for consistent patterns of low utilization (<10% CPU)
- Consider burstable instances (T3/T4g) for variable workloads
- T3/T4g: Burstable performance for variable workloads
- M5/M6i: Balanced compute, memory, and networking
- C5/C6i: Compute-optimized for CPU-intensive applications
- R5/R6i: Memory-optimized for memory-intensive applications
- Consider ARM-based Graviton processors for up to 40% better price performance
- Test application compatibility before migration
- Use instance types like M6g, C6g, R6g
- Use AWS Instance Scheduler for dev/test environments
- Implement auto-scaling for variable workloads
- Consider Spot Instances for fault-tolerant workloads
After deployment, Terraform provides:
terraform output
Key outputs include:
instance_id
: EC2 instance identifierinstance_public_ip
: Public IP for SSH/web accessssh_connection_command
: Ready-to-use SSH commandrightsize_recommendations
: Summary of optimization opportunitiescloudwatch_alarms
: Monitoring alarm ARNs
To avoid ongoing costs:
terraform destroy
- AWS Trusted Advisor Cost Optimization
- AWS Compute Optimizer
- EC2 Instance Types
- AWS Cost Optimization Hub
- CloudZero Cost Intelligence
All resources are tagged with:
Environment
: demoProject
: rightsize-exampleManagedBy
: terraformCostCenter
: engineeringOwner
: cloudzero-demo
Additional instance-specific tags include rightsizing metadata for cost tracking and optimization analysis.
- Security: The security group allows SSH access from anywhere (0.0.0.0/0). In production, restrict this to your IP range.
- Costs: Remember to destroy resources when not needed to avoid charges.
- Monitoring: Enable detailed monitoring in production for better rightsizing decisions.
- Backup: Consider EBS snapshots for important data.
This is a demonstration repository. For improvements or issues:
- Fork the repository
- Create a feature branch
- Submit a pull request
Disclaimer: This is a demonstration environment. Always follow your organization's security and compliance requirements in production deployments.