Skip to content

CognitoIQ/terraform-aws-autospotting

 
 

Repository files navigation

AutoSpotting

Automatically convert your existing Auto Scaling groups to significantly cheaper spot instances with minimal (often zero) configuration changes.

See https://github.com/autospotting/autospotting for details.

Using the terraform-aws-autospotting module

Sources

This module can be used from the Terraform Registry or directly from this repository. The AutoSpotting binary defaults to the latest nightly build hosted on S3.

Using from the Terraform Registry:

module "autospotting" {
  source  = "AutoSpotting/autospotting/aws"
  # version = "0.1.2"
}

Using from this repository:

module "autospotting" {
  source = "github.com/autospotting/terraform-aws-autospotting?ref=master" # or ref=0.1.2, etc.
}

Notes:

  • The official (nightly) AutoSpotting binary is hosted in the cloudprowess S3 bucket in AWS region us-east-1. When the source of the Lambda function is an S3 bucket, both the function and the bucket must be in the same region.

    • If you are already using the AWS provider in the us-east-1 region, no additional configuration is required:

      provider "aws" {
        region  = "us-east-1"
        profile = "default"
      }
      
      module "autospotting" {
        source = "github.com/autospotting/terraform-aws-autospotting"
      }
    • If you are using the AWS provider in another region, you must either (1) alias another AWS provider in the us-east-1 region, (2) copy the binary into an S3 bucket in the region you are already using, or (3) provide a local binary. Here, we demonstrate using an alias:

      provider "aws" {
        region  = "eu-central-1"
        profile = "default"
      }
      
      provider "aws" {
        alias   = "us"
        region  = "us-east-1"
        profile = "default"
      }
      
      module "autospotting" {
        source = "github.com/autospotting/terraform-aws-autospotting"
        providers = {
          aws = aws.us
        }
      }
    • The Lambda function can run in any supported region

  • New releases of this module only work with Terraform 0.12 or newer.

Setting variables

Available variables are defined in the variables file. To change the defaults, just pass in the relevant variables:

module "autospotting" {
  source                                = "github.com/autospotting/terraform-aws-autospotting"
  autospotting_regions_enabled          = "eu*,us*"
  autospotting_min_on_demand_percentage = "33.3"
  lambda_memory_size                    = 1024
}

Or you can pass them in on the command line:

 terraform apply \
   -var autospotting_regions_enabled="eu*,us*" \
   -var autospotting_min_on_demand_percentage="33.3" \
   -var lambda_memory_size=1024

Using custom binaries

The lambda.zip file can be generated by building it locally. Further instructions are available in the AutoSpotting repo.

You can also download the latest official nightly build available here.

From local file

If you store the file locally:

module "autospotting" {
  source         = "github.com/autospotting/terraform-aws-autospotting"
  lambda_zipname = "lambda.zip"
}

From S3

Instead of using a local ZIP file you can refer to the Lambda code in a location in S3:

module "autospotting" {
  source           = "github.com/autospotting/terraform-aws-autospotting"
  lambda_s3_bucket = "lambda-releases"
  lambda_s3_key    = "lambda.zip"
}

Multiple instances

You can change the names of the resources terraform will create – or run multiple instances of autospotting that target different ASGs – by using the label variables:

module "autospotting_storage" {
  source                              = "github.com/autospotting/terraform-aws-autospotting"
  label_name                          = "autospotting_storage"
  autospotting_allowed_instance_types = "i3.*"
  autospotting_tag_filters            = "spot-enabled=true,storage-optimized=true,"
}

module "autospotting_dev_memory" {
  source                              = "github.com/autospotting/terraform-aws-autospotting"
  label_name                          = "autospotting_memory"
  label_environment                   = "dev"
  autospotting_allowed_instance_types = "r5*"
  autospotting_tag_filters            = "spot-enabled=true,memory-optimized=true,environment=dev,"
}

Logs or Troubleshooting

To check logs and troubleshoot issues, you can go to the /aws/lambda/<your_lambda_function_name> CloudWatch Log group.