• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

spot

GCP Spot VM

December 8, 2021

Spot VMs are preemtible, they can be reclaimed by the Cloud provider.

However you can get deep discounts using them.

Launch a spot instance in GCP using Terraform.

provider "google" {
  project = "airy-totality-151318"
  zone    = "us-central1-c"
}
 
resource "google_compute_instance" "test" {
  name         = "test"
  machine_type = "e2-micro"
 
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
 
  scheduling {
    preemptible       = true
    automatic_restart = false
  }
 
  network_interface {
    network = "default"
    access_config {
    }
  }
}

provider "google" { project = "airy-totality-151318" zone = "us-central1-c" } resource "google_compute_instance" "test" { name = "test" machine_type = "e2-micro" boot_disk { initialize_params { image = "debian-cloud/debian-9" } } scheduling { preemptible = true automatic_restart = false } network_interface { network = "default" access_config { } } }

Filed Under: Cloud Tagged With: gcp, spot, terraform, vm

GCP Spot Instance

November 25, 2021

You can save anywhere from 60-91% using a spot instance. The downside is, the instance can be preempted anytime.

Create a spot instance.

gcloud beta compute instances create spot-example \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--image-project=ubuntu-os-cloud \
--image-family=ubuntu-2004-lts \
--machine-type=e2-micro \
--project=your-project-id \
--zone=us-central1-a

gcloud beta compute instances create spot-example \ --provisioning-model=SPOT \ --instance-termination-action=STOP \ --image-project=ubuntu-os-cloud \ --image-family=ubuntu-2004-lts \ --machine-type=e2-micro \ --project=your-project-id \ --zone=us-central1-a

Delete instance.

gcloud compute instances delete spot-example \
--project=your-project-id \
--zone=us-central1-a

gcloud compute instances delete spot-example \ --project=your-project-id \ --zone=us-central1-a

Filed Under: Cloud Tagged With: create, delete, gcp, instance, preempted, spot

AWS Spot Instances Running Docker

February 14, 2021

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

Here’s my Terraform script.

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 = <<-EOF
              #!/bin/bash
              hostnamectl set-hostname docker
              EOF
  tags = {
        Name = "docker-0.1"
  }
}
resource "aws_ec2_tag" "tagging" {
  resource_id                   = aws_spot_instance_request.docker.spot_instance_id
  key                           = "Name"
  value                         = "docker-0.1"
}

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 = <<-EOF #!/bin/bash hostnamectl set-hostname docker EOF tags = { Name = "docker-0.1" } } resource "aws_ec2_tag" "tagging" { resource_id = aws_spot_instance_request.docker.spot_instance_id key = "Name" value = "docker-0.1" }

I use “aws_ec2_tag” resource to tag the instance properly.

In addition, I use user_data to run a script, to set the hostname.

To launch via Terraform, I run the following commands.

terraform init
terraform apply

terraform init terraform apply

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

terraform destroy

terraform destroy

It’s not bad deal for an instance that costs only $0.0031 per hour.

Filed Under: Cloud Tagged With: aws, docker, instances, spot, tags, terraform

  • Home
  • About
  • Archives

Copyright © 2023