- Build a dedicated Terraform module for EC2 provisioning.
- Create Terragrunt configurations for each environment (development, production).
- Provide environment-specific variables (instance type, AMI ID, networking).
- Run
terragrunt init,plan, andapply. - Validate security groups, key pairs, and network settings.

1. Custom Terraform Module
Create a folder structure undermodules/ec2/:
| File | Purpose |
|---|---|
main.tf | Defines aws_instance, security group attachments, user data, etc. |
variables.tf | Declares inputs such as instance_type, ami_id, vpc_security_group_ids, key_name. |
outputs.tf | Exposes attributes like public IP and instance ID. |
Example: variables.tf
Make sure your AMI IDs match the target AWS region. You can automate lookup via the AWS Systems Manager Parameter Store.
2. Terragrunt Configuration per Environment
Under each environment folder (environments/development/, environments/production/), define a terragrunt.hcl that points to the EC2 module and sets inputs:
production, adjusting values accordingly.
Avoid committing sensitive data (e.g., private SSH keys) to version control. Store critical secrets in a secure vault or use Terragrunt’s
read_terragrunt_config to pull from encrypted sources.