0PricingLogin
Terraform Infrastructure as Code · Lesson

Provisioning EC2 Instances

Write Terraform configurations to deploy and manage Amazon EC2 virtual machines, including network and security group settings.

EC2 Instances with Terraform

Welcome! In this lesson, you'll learn to provision and manage Amazon EC2 virtual machines using Terraform.

EC2 (Elastic Compute Cloud) provides scalable virtual servers in the cloud. Using Infrastructure as Code (IaC) with Terraform for EC2 means you can define your servers in code, ensuring consistent, repeatable deployments.

Defining an EC2 Instance

The core resource for an EC2 instance in Terraform is aws_instance. You need to specify at least two key arguments:

  • ami: The Amazon Machine Image (OS, software) to use.
  • instance_type: The hardware configuration (e.g., t2.micro for free tier eligible instances).

Here's how a basic aws_instance block looks:

resource "aws_instance" "web_server" {
  ami           = "ami-053b04dcd42d5397e" # Example: Amazon Linux 2 in us-east-1
  instance_type = "t2.micro"

  tags = {
    Name = "MyWebServer"
  }
}

All lessons in this course

  1. AWS Provider Configuration
  2. Provisioning EC2 Instances
  3. Managing S3 Buckets and IAM
  4. Configuring VPC and Networking
← Back to Terraform Infrastructure as Code