fbpx

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. Install the Spotinst Terraform Provider

Please follow the instructions here and install the latest Terraform binary and the download Spotinst Plugin.

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

# Configure the Spotinst provider
provider "spotinst" {
  token = "${var.spotinst_personal_access_token}"
}
Please read the documentation for more details.

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

3. Create a new Spotinst resource.

# 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
  }   
}
For more detailed info about each property – please use the documentation.

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.

 

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!