Creating ComputeGroups using terraform

Creating ComputeGroups using terraform

One of the critical components of devops workflow is maintaining “infrastructure as code,”. Terraform is an open source software championing the idea of infrastructure as code.

With Lightwing’s terraform provider we can create ComputeGroups with the help of terraform.

Setup the Lightwing terraform Provider

  1. Download the lightwing terraform provider from here
  2. Move it under the ~/.terraform.d/plugins driectory under the name terraform-provider-lw
$ mkdir -p ~/.terraform.d/plugins/
$ mv  terraform-provider-lw ~/.terraform.d/plugins/terraform-provider-lw

Obtain the API token and Account Configurations

  1. In the Lightwing console go to settings page
  2. Copy the API Token and the Organization ID from here
  3. Go to accounts page and copy the cloud account ID

Lightwing API token

These variables will have to be used in the terraform script below

Sample terraform script


terraform {
  required_providers {
    lw = {
    }
  }
}

provider "lw" {
  token = "<Your API token>"
  orgid = <Your OrgID>
  apiurl = "https://api.lightwing.io"
}

resource "lw_compute_group" "api_servers" {
    name = "Sample Compute Group"
    cloud_provider = "aws"
    region = "ap-south-1"
    cloud_account_id = <Your Cloud ACCount ID>
    org_id = <Your OrgID>
    stateful = false
    enable_public_ip = false
    instance_types = ["t3.medium"]
    vpc = "vpc-20fdd448"
    
    platform = "linux"

    scripts {
      userdata = ""
    }

    capacity {
        min = 1
        max = 3
        desired = 3
        ondemand = 1
        spot = 2
    }

    aws {
        ami = "ami-0fc61ed7d17328bab"
        subnets = {
          "ap-south-1a" = "subnet-1"
          "ap-south-1b" = "subnet-2"
          "ap-south-1c" = "subnet-3"
        }
        security_groups = ["sg-123","sg-456"]
        instance_profile_arn = "arn:aws:iam::123456789:instance-profile/awesomerole"
        target_group_id = ""
        key_name = "topsecretkey"
    }

    tags = {
      "Name" = "Sample Compute GRoup"
      "Environment" = "green"
    }
}

Argument reference

The following arguments are supported in the lw_compute_group block

  • name - Name of the compute group to be created

  • cloud_provider - The cloud provider in which the computegroup has to be created, the only supported value as of now is aws

  • region - The region of computegroup eg: ap-south1

  • cloud_account_id - The lightwing cloud account id of your provider account in which the computegroup has to be created in. This value can be obtained from the cloud accounts list page

  • org_id - The lightwing organization id of your account. This can be obtained from the profile section in the settings page

  • stateful - (Optional) boolean representing the lifecycle of instances created under the computegroup

  • enable_public_ip - (Optional) boolean representing whether or not a public IP has to be associated with the instances created uder the computegroup

  • instance_types - A list of instance types which will be used while bidding for the spot instances eg:["t3.medium","t3a.medium","t2.medium"]

  • vpc - ID of the VPC under which the computegroup has to be created

  • platform - The OS type of instances being created for the computegroup. Supported values are : "linux" and "windows"

  • userdata - (Optional) User data to be executed on each of the created machine

  • capacity - The min, max, desired, ondemand and spot capacity of the computegroup

  • ami - The image ID to be used for computegroup instances

  • subnets - A key value pair representing the subnets in which the computegroup has to pan across. The key is availability_zone and value subnet_id

  • security_groups - A list of security group IDs to be used for the computegroup

  • instance_profile_arn - (Optional) The role to be associated to the instances in the computegroup

  • target_group_id - (Optional) The target group to which the instances in the computegroup are to be attached

  • key_name - (Optional) the name of the keyfile(.pem) to be attached to the computegroup instances

  • tags - (Optional) A list of key value pairs which has to be attached to the computegroup instances as tags