Overview
The EC2 module creates Amazon EC2 instances running Amazon Linux 2023 with optional Application Load Balancer, automatic Tailscale installation, HTTPS support via ACM certificates, and CloudFlare DNS integration.Features
Amazon Linux 2023
Automatically uses the latest AL2023 AMI with security updates
Optional Tailscale
Automatic Tailscale installation for secure remote access
Optional ALB
Application Load Balancer with health checks and target groups
HTTPS Support
Automatic ACM certificate creation and CloudFlare DNS validation
Multiple Instances
Create multiple instances with a single module call
Security Hardening
IMDSv2 enforcement, encrypted storage, and security groups
Architecture
Without ALB
With ALB
Usage Examples
Basic EC2 Instance
Multiple Instances
With Tailscale
After the instance boots, SSH to it and run
sudo tailscale up to authenticate.With Application Load Balancer
With Custom Domain and HTTPS
Inputs
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
name | Name prefix for EC2 instances | string | n/a | yes |
instance_count | Number of EC2 instances to create | number | 1 | no |
vpc_id | VPC ID | string | n/a | yes |
subnet_id | Subnet ID for the instance(s) | string | n/a | yes |
instance_type | EC2 instance type | string | "t3.micro" | no |
ami_id | Custom AMI ID (leave empty for latest AL2023) | string | "" | no |
key_name | EC2 key pair name | string | "" | no |
associate_public_ip | Assign public IP | bool | true | no |
root_volume_size | Root volume size in GB | number | 20 | no |
root_volume_type | Root volume type | string | "gp3" | no |
root_volume_encrypted | Encrypt root volume | bool | true | no |
enable_detailed_monitoring | Enable detailed monitoring | bool | false | no |
enable_termination_protection | Enable termination protection | bool | false | no |
iam_policy_arns | Additional IAM policies | list(string) | [] | no |
ingress_rules | Ingress rules for EC2 security group | list(object) | SSH only | no |
allocate_eip | Allocate Elastic IP | bool | false | no |
enable_tailscale | Enable Tailscale installation | bool | false | no |
enable_alb | Enable Application Load Balancer | bool | false | no |
alb_subnet_ids | ALB subnet IDs (2+ AZs) | list(string) | [] | no |
alb_internal | Make ALB internal | bool | false | no |
alb_certificate_arn | ACM certificate ARN for HTTPS | string | "" | no |
application_port | Application port on EC2 | number | 80 | no |
health_check_path | Health check path | string | "/" | no |
health_check_matcher | HTTP status codes for healthy | string | "200" | no |
domain_name | Domain name for ALB (enables auto HTTPS) | string | "" | no |
cloudflare_zone_id | CloudFlare Zone ID | string | "" | no |
cloudflare_api_token | CloudFlare API token | string | "" | no |
additional_user_data | Additional setup commands | string | "" | no |
tags | Resource tags | map(string) | {} | no |
Outputs
| Name | Description |
|---|---|
instance_id | EC2 instance ID(s) |
instance_arn | EC2 instance ARN(s) |
private_ip | Private IP address(es) |
public_ip | Public IP address(es) |
ami_id | AMI ID used |
security_group_id | Security group ID |
iam_role_arn | IAM role ARN |
alb_dns_name | ALB DNS name (if enabled) |
alb_arn | ALB ARN (if enabled) |
target_group_arn | Target group ARN (if enabled) |
certificate_arn | ACM certificate ARN (if domain configured) |
domain_name | Configured domain name |
domain_url | Full HTTPS URL (if domain configured) |
certificate_status | ACM certificate status |
ssh_command | SSH command to connect |
http_endpoint | HTTP/HTTPS endpoint URL |
instance_summary | Summary of configuration |
Security Group Configuration
By default, only SSH (port 22) is allowed. Customize withingress_rules:
When ALB is enabled, the module automatically adds an ingress rule to allow traffic from the ALB to the application port.
Best Practices
Use Latest AMI
Leave
ami_id empty to automatically use the latest Amazon Linux 2023 AMI with security patches.Encrypt Storage
Root volume encryption is enabled by default. Keep it enabled for compliance.
Restrict SSH
Limit SSH access to specific IP ranges or use Tailscale for secure access.
Use ALB for Production
ALB provides health checks, auto-recovery, and HTTPS termination.
Enable Monitoring
Use
enable_detailed_monitoring = true for production workloads.Right-Size Instances
Start with smaller instances (t3.micro/small) and scale based on metrics.
Troubleshooting
Cannot SSH to Instance
Check security group:Tailscale Not Connecting
Check if installed:ALB Health Checks Failing
Check application is running:Certificate Validation Stuck
Check ACM certificate status:- Check that CNAME validation records were created
- DNS propagation can take 5-10 minutes
- CloudFlare API token missing DNS edit permissions
- Zone ID incorrect
- Domain already has conflicting records
Related Documentation
VPC Module
Create VPC and subnets for EC2
Usage Guide
Common patterns and best practices
Infrastructure Guide
Complete deployment workflow
EKS Module
Deploy Kubernetes cluster alongside EC2