Import Spot resources into Terraform

Reading Time: 2 minutes

More and more, DevOps and platform teams are using infrastructure as code to design and implement infrastructure in the cloud. Hashicorp’s Terraform is one of the most popular infrastructure as code tools. It enables you to define your desired state of infrastructure using code, and deploy those changes to your cloud. Spot by NetApp has extensive support for Terraform in Elastigroup and Ocean, our products for cloud infrastructure management. When integrated together, you can use your existing Terraform templates while Spot determines and optimizes cloud infrastructure resources for the best possible mix of compute type, size and lifecycle for the workload. 

In past posts, we’ve discussed how Terraform can help with deployment management, and we’ve walked through how to create an Ocean cluster using Terraform. If you already have an Ocean cluster running and you are starting to work with Terraform, this tutorial will help you import an Ocean cluster or any other spot resource (Elastigroup, virtual node group etc.) into Terraform. 

In this tutorial, you’ll go through the steps to create a cluster via the Ocean UI and manage it using Terraform. By importing Ocean to Terraform, you’ll automatically create the state of the resource and have a template to use to manage it. You can use this method to duplicate Ocean environments in Terraform by using the template that was created during the import process. 

You’ll also learn how to import Ocean into a specific resource. Ocean can also be imported to a specific module as well. Read about the different options in Terraform official documentation: https://www.terraform.io/docs/cli/commands/import.html

Import Ocean into Terraform

Pre-req: 

  1. A Spot by NetApp account connected to a cloud provider 
  2. Ocean running in the Spot by NetApp console (or any other Spot resource). It doesn’t matter if the cluster was created via the UI or API.

Steps:

1. Create a main.tf file where you have the following details: 

  • Spot provider: You can find the required information here. By clicking on the “Use provider” you will see all the required information that you should have in order to import the Ocean resource.
  • Empty resource: Here is an example for an empty resource:

resource "spotinst_ocean_aws" "test-import" {}

2. Run terraform init

3. Once the main.tf file is ready, use the import command

Terraform import relevant_resouce.nameOfTheResource ResouceId

Here is an example:

terraform import spotinst_ocean_aws.test-import o-20e4833a

4. After the import is done you have a state file of the resource. Next, run the terraform show command in order to get the terraform template you want to use.

Please note that in Ocean, you should remove the ID parameter in order to be able to use this template to manage your cluster. After the import is done, you have a state file of the resource. 

You are all set! You can now manage the Ocean cluster using Terraform. Read more about Ocean features in the following link:
https://docs.spot.io/ocean/features/