Terraform AWS EC2 Instance Config Builder

Generate Terraform HCL for an EC2 instance with security groups

Creates a Terraform config for an AWS EC2 instance with AMI id, instance type, key pair, a security group allowing chosen ports, user data script, and resource tags ready for terraform apply.

What is an AMI id?

An Amazon Machine Image id identifies the base operating system image the instance boots from. It is region specific, so an AMI from us-east-1 will not work in eu-west-1.

Terraform AWS EC2 Instance Config Builder

This builder generates Terraform HCL to launch an AWS EC2 instance along with a security group that governs inbound access. It produces a self-contained configuration you can terraform apply immediately, with sensible defaults for SSH access, an optional first-boot script, and tags for cost tracking.

How it works

The output defines an aws_security_group with one ingress block per port you allow (protocol tcp, source 0.0.0.0/0) and a permissive egress block. It then defines an aws_instance referencing that security group via vpc_security_group_ids. Key fields:

  • ami — the base image id for your region.
  • instance_type — CPU and memory class, for example t3.micro.
  • key_name — the SSH key pair, included only when provided.
  • user_data — a first-boot shell script, base64 encoded with base64encode().
  • tags — a Name tag and any environment label.

Tips and example

After copying, initialize and launch:

terraform init
terraform plan
terraform apply

Always look up the correct AMI id for your target region, since AMIs are region scoped. Restrict the SSH ingress source to your own IP rather than 0.0.0.0/0 in production. Keep user_data idempotent so re-runs do not break a booted instance, and tag every resource so it is easy to find and clean up.