How to Launch an EC2 Instance With Terraform
Launching an EC2 instance with Terraform comes down to one resource: aws_instance, with an ami and an instance_type. Add an instance_id output so downstream resources can reference it, run terraform apply -auto-approve, and confirm the instance state is running. Here's the full walkthrough.
The one resource that launches an instance
Everything else on a DevOps job (security groups, load balancers, auto scaling) builds on
a single primitive: the aws_instance resource. It needs two required fields - an AMI id
to boot from and an instance_type for the hardware shape:
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
The aws_instance.web label is how you reference this instance elsewhere in your config.
The AMI is the machine image (OS + preinstalled software) the instance boots from;
t2.micro is a small, cheap general-purpose type - the right default when you just need
compute to exist.
Expose the instance id as an output
A bare instance isn't much use until something else can find it. Terraform outputs surface
a resource attribute so other modules, scripts, or a terraform output call can read it.
The most useful one to publish is the instance id:
output "instance_id" {
value = aws_instance.web.id
}
Now aws_instance.web.id is available to wire into a security group attachment, a target
group, or a DNS record later. This is how real stacks compose - one resource's output
becomes another's input.
Apply and verify
With the provider already initialized, apply the plan. -auto-approve skips the
interactive confirmation so it runs unattended:
cd /workspace
terraform apply -auto-approve
Terraform prints the plan, creates the instance, and writes it to terraform.tfstate.
Read the output back with:
terraform output -raw instance_id
Then confirm the instance actually came up by querying it directly. A freshly launched
instance should report state running:
aws ec2 describe-instances \
--query 'Reservations[].Instances[].[InstanceId,InstanceType,State.Name]'
You should see your instance id, t2.micro, and running. If describe-instances returns
nothing, the apply didn't create the resource - re-run terraform apply and check for a
syntax error in main.tf.
Why declare it in code at all
You could click the same instance together in the AWS console in a minute. The reason
every team past a handful of people uses Terraform instead: the config is the source of
truth. terraform apply is repeatable and reviewable, terraform destroy tears the whole
thing down cleanly, and the diff in a pull request shows exactly what infrastructure is
changing before it changes. Console clicks leave no record and don't scale.
A production-grade version of this instance adds more: a subnet_id to place it in a VPC,
vpc_security_group_ids to control traffic, tags for cost tracking, and often
user_data to bootstrap software on first boot. Start with the two required fields, get a
green apply, then layer the rest on.
Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.
Try it in a workspace →What you'll practice
- Declaring an aws_instance resource with a required ami and instance_type
- Publishing a resource attribute with a Terraform output block
- Applying with terraform apply -auto-approve and verifying instance state with the AWS CLI
FAQ
How do I launch an EC2 instance with Terraform?
Declare an aws_instance resource with an ami and an instance_type, then run terraform apply -auto-approve. Add an output block exposing aws_instance.web.id so other resources or terraform output can reference the new instance.
What does terraform apply -auto-approve do?
It runs the plan and creates or updates the resources without stopping to ask for interactive yes/no confirmation. It's the standard flag for running Terraform in scripts and CI where no human is at the keyboard to type 'yes'.
How do I get the id of an EC2 instance Terraform created?
Add an output block with value = aws_instance.web.id, then run terraform output -raw instance_id. You can also confirm the instance is live with aws ec2 describe-instances and check that State.Name is running.
What is the minimum required to create an aws_instance in Terraform?
Just two arguments: ami (the machine image to boot from) and instance_type (the hardware shape, like t2.micro). Everything else - subnet, security groups, tags, user_data - is optional and layered on for a production setup.
Keep learning
Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.
Open the workspace →