Spotinst raises $15M Series A Led By Intel Capital and Vertex Ventures Read The Official Press Release

Spotinst & Terraform Getting Started – Create Cost-Efficient Auto Scaling on AWS

How to install Spotinst plugin for Terraform

Liran Polak
Chief Architect

How to install Spotinst plugin for Terraform

In this post we will demonstrate how to create a cost-efficient Auto-Scaling in AWS using Spotinst Terraform plugin.

This post assumes that you already have a Spotinst account, and that you have connected your AWS account with Spotinst.

1. Download the binary file terraform-provider-spotinst. (Notice: the Spotinst Terraform provider has been merged with the master Terraform branch in Github).

Please download the proper binary file for your operating system and architecture and put it somewhere on your filesystem:

0.9.0 Windows :
0.9.0 Linux :
0.9.0 Darwin :
0.8.7 Windows :
0.8.7 Linux:
0.8.7 Darwin :
0.8.5 Windows :
0.8.5 Linux :
0.8.5 Darwin :

2. Configure Terraform to be able to find the binary file:

If you are on a Unix-like system, create a file named .terraformrc in your home directory:

~/.terraformrc

If you are on a Windows system, create a file named terraform.rc in the %APPDATA% directory:

%APPDATA%/terraform.rc

3. Edit the file and add the following content:

providers {
  spotinst = "/path/to/terraform-provider-spotinst"
}

4. Create a new Terraform template and configure the Spotinst provider.

Please read the documentation for more details.

# Configure the Spotinst provider
provider "spotinst" {
  token = "${var.spotinst_personal_access_token}"
}

You can provide your API details from Spotinst API Zone, or create a Personal Access Token.

5. Create a new Spotinst resource.

Please read the documentation for more details.

# Create an AWS group
resource "spotinst_aws_group" "workers" {
  name = "workers-group"
  description = "created by Terraform"
  product = "Linux/UNIX"
     
  capacity {
    target = 75
    minimum = 50
    maximum = 100
  }

  strategy {
    risk = 100
  }

  instance_types {
    ondemand = "c3.large"
    spot = ["c3.large", "c4.large"]
  }
     
  availability_zone {
    name = "us-west-2b"
    subnet_id = "subnet-7bbbf51e"
  }

  launch_specification {
    monitoring = false
    image_id = "ami-f0091d91"
    key_pair = "pemfile_name"
    security_group_ids = ["default"]
  } 
    
  scheduled_task {
    task_type = "backup_ami"
    frequency = "weekly"
  } 

  scaling_up_policy {
    policy_name = "Scaling Policy 1"
    metric_name = "CPUUtilization"
    statistic = "average"
    unit = "percent"
    threshold = 80
    adjustment = 1
    namespace = "AWS/EC2"
    period = 300
    evaluation_periods = 2
    cooldown = 300
  }

  scaling_down_policy {
    policy_name = "Scaling Policy 2"
    metric_name = "CPUUtilization"
    statistic = "average"
    unit = "percent"
    threshold = 40
    adjustment = 1
    namespace = "AWS/EC2"
    period = 300
    evaluation_periods = 2
    cooldown = 300
  }   
}

Once you have everything setup correctly, you can execute your Terraform file and apply the changes. It should trigger an API call to Spotinst, and create an Elatigroup.

If you have any question or a comment, please feel free to reach me at: liran@spotinst.com

Best,
The Spotinst Team.

Liran Polak
Chief Architect

Stay current

Sign up for our newsletter, and we'll send you the latest updates on Spotinst, tips, tutorials and more cool stuff!