Skip to main content

How to Deploy AWS Spot Instances Using Terraform

· By Pankajbhai Chavda · 3 min read

If you have used AWS in your business or you practice on it and you need to manage low billing, then Amazon EC2 Spot Instances are a great way to slash your cloud bill. This can sometimes be by up to 90%. If AWS suddenly needs that computing power back, they can pull the plug on your instance with just a minute's warning.

In production, you handle this unpredictability using Auto Scaling Groups or orchestrators like Karpenter. But if you are just starting out, the best way to understand how Spot capacity works is to spin up a single instance manually using Terraform.

Here we will write a basic Terraform configuration, launch a Spot instance, and talk about what happens when AWS runs out of capacity.

What You Need

An AWS Account. Your AWS Access Key and Secret Key. A Linux environment with Terraform installed on your computer. If it is not installed, then follow the step below; otherwise, skip one step.

Install Terraform

If you don't have Terraform installed on your machine yet, let's first install it. Run the below commands one by one on your Ubuntu/Debian machine to install Terraform.

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get install terraform

You can verify the installed Terraform version by using the below command.

terraform version

Expected Output:

Terraform version.

Export Your AWS Credentials

Terraform needs permission to talk to your AWS account. Instead of hardcoding your sensitive keys directly into your configuration files, we will export them as environment variables right in your terminal. Adding them in configuration files is risky.

export AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"

Write the Terraform Configuration

Create a new folder for this project. Open your terminal there and create a file named main.tf.

We are going to request a single t3.micro instance running the latest version of Ubuntu in the (us-east-1) region. You can change your region according to your AWS registration. Then paste the below code into your main.tf file.

provider "aws" {
  region = "us-east-1"
}

# 1. Look up the latest official Ubuntu image
data "aws_ami" "ubuntu" {
  most_recent = true
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
  }
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
  owners = ["099720109477"] # Canonical's official account ID
}

# 2. Request the Spot Instance
resource "aws_spot_instance_request" "test_spot" {
  ami                  = data.aws_ami.ubuntu.id
  instance_type        = "t3.micro"

  # The absolute maximum you are willing to pay per hour
  spot_price           = "0.01"

  # A "one-time" request means if the instance is interrupted,
  # AWS won't try to automatically spin up a replacement.
  spot_type            = "one-time"

  # Force Terraform to wait until the instance is actually provisioned
  wait_for_fulfillment = true

  tags = {
    Name = "MyFirstSpot"
  }
}

Then save the file.

Initialize, Plan, and Deploy

After your main.tf file is ready, you can deploy your infrastructure.

First, initialize Terraform. This downloads the AWS provider so Terraform knows how to talk to AWS.

terraform init

This then shows you exactly what Terraform is about to do before it actually does it.

terraform plan

Now make the request to AWS to deploy the instance using the below command.

terraform apply

Type yes when asked to confirm. It might take a few seconds, but once it finishes, you can log into your AWS EC2 Console and see your new Spot instance running.

Handling the "Capacity Not Available" Error

If you run terraform apply and you see an error message that looks like this:

Error: waiting for EC2 Spot Instance Request to be fulfilled: unexpected state 'capacity-not-available

Don't panic — you didn't break anything. This is just the reality of the Spot market. It means AWS literally has no spare t3.micro servers sitting around in that specific region at the price you offered.

When this happens, the fix is easy. Open your main.tf file and change the instance_type to something similar like t3.small, or change the region to one that might be less busy, and run terraform apply again.

Clean Up (only for tester)

Spot instances are cheap, but they aren't free. When you are done testing, always destroy what you built so you don't get a surprise on your next AWS bill.

terraform destroy

Type yes to confirm. Terraform will then handle terminating the instance and canceling the underlying Spot request.

Conclusion

Building a Spot instance with Terraform is straightforward, but it forces you to think differently about cloud compute. Unlike On-Demand servers, Spot capacity is fluid. When you create it, it may sometimes not be available in a specific region, so you can try a different instance type. Understanding this baseline behavior is the first step toward building resilient, cost-optimized cloud infrastructure.

About the author

Pankajbhai Chavda Pankajbhai Chavda
Updated on Aug 4, 2026
-