I have an AMI with docker installed. Here’s how I launch a spot instance using Terraform.

Here’s my Terraform script.

<pre lang="bash">
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
provider "aws" {
  profile = "default"
  region  = "us-east-1"
resource "aws_spot_instance_request" "docker" {
  ami                           = "ami-xxxxxxxxxxxxxxxx"
  spot_price                    = "0.0031"
  wait_for_fulfillment          = "true"
  key_name                      = "servers"
  instance_type                 = "t3.micro"
  subnet_id                     = "subnet-xxxxxxxxxxxxxxxx"
  security_groups               = ["sg-xxxxxxxxxxxxxxxxxx"]
  associate_public_ip_address   = "true"
  user_data = 

<p>I use "aws_ec2_tag" resource to tag the instance properly.</p>

<p>In addition, I use user_data to run a script, to set the hostname. </p>

<p>To launch via Terraform, I run the following commands.</p>

<pre lang="bash">
terraform init
terraform apply

<p>When done, I could stop the instance to stop incurring charges. Or just simply destroy it via Terraform.</p>

<pre lang="bash">
terraform destroy

<p>It's not bad deal for an instance that costs only $0.0031 per hour.</p>