Lets create a couple of terraform files.
1st file is going to be our
providers.tf
This file will setup our Terraform providers. Please copy and paste into the file and dont forget to save.
#Configure Providers
terraform {
required_providers {
spotinst = {
source = "spotinst/spotinst"
version = "1.139.0"
}
aws = {
source = "hashicorp/aws"
version = "4.62.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.19.0"
}
local = {
helm = {
source = "hashicorp/helm"
version = "2.9.0"
}
}
}
# SPOT
provider "spotinst" {
token = var.spotinst_token
account = var.spotinst_account
}
# AWS
provider "aws" {
region = var.region
}
}
# Data Resources for kubernetes provider
data "aws_eks_cluster" "cluster" {
name = module.eks.cluster_name
depends_on = [module.eks.cluster_id]
}
data "aws_eks_cluster_auth" "cluster" {
name = module.eks.cluster_name
depends_on = [module.eks.cluster_id]
}
data "aws_iam_instance_profiles" "profile" {
role_name = module.eks.eks_managed_node_groups["green"].iam_role_name
}
# KUBERNETES
provider "kubernetes" {
host = data.aws_eks_cluster.cluster.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
token = data.aws_eks_cluster_auth.cluster.token
}
# HELM
provider "helm" {
kubernetes {
# config_path = "~/.kube/config"
host = data.aws_eks_cluster.cluster.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
token = data.aws_eks_cluster_auth.cluster.token
}
}
Now lets create our second file
variables.tf
This file describes what variables Terraform should be expecting. Please copy and paste into the file and dont forget to save.
variable "region" {
}
variable "cluster_identifier" {
}
variable "subnet_ids" {
type = list(string)
}
variable "spotinst_token" {
}
variable "spotinst_account" {
}
variable "aws_access_key" {
}
variable "aws_secret_key" {
}
variable "cluster_name" {
}
variable "creator" {
}
variable "vpc_id" {
}
variable "ocean-controller-version" {
}
variable "rolearn" {
}
Now we are going to create our last configuration file with all the actual variable values, variables.auto.tfvars.
variables.auto.tfvars
Please copy and paste into the file and fill in all necessary missing variable. Dont forget to save.
cluster_identifier = "tf-helm-spot"
cluster_name = "tf-helm-spot"
vpc_id = "vpc-"
aws_access_key = " "
aws_secret_key = " "
region = "us-west-2"
subnet_ids = ["subnet-", "subnet-","subnet-"]
security_groups = ["sg-", "sg-"]
instance_types_ondemand = "t3.large"
spotinst_token = " "
spotinst_account = " "
creator = " "
rolearn = "arn:aws: "
Now onto our last and the actual main.tf file.
main.tf
Please copy and paste as is, and of course, dont forget to save!
# CREATING NEW EKS CLUSTER re: https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/latest
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 19.0"
cluster_name = var.cluster_name
cluster_version = "1.28"
cluster_endpoint_public_access = true
vpc_id = var.vpc_id
subnet_ids = var.subnet_ids
cluster_addons = {
coredns = {
most_recent = true
}
kube-proxy = {
most_recent = true
}
}
manage_aws_auth_configmap = true
aws_auth_roles = [
{
rolearn = var.rolearn
username = "Admin"
groups = [
"system:masters"
]
},
]
# EKS Managed Node Group(s)
eks_managed_node_group_defaults = {
instance_types = ["m5.large"]
}
eks_managed_node_groups = {
green = {
min_size = 0
max_size = 5
desired_size = 2
capacity_type = "SPOT"
}
}
tags = {
Creator = var.creator
}
}
Save everything, and we are going to start Terraform to create us an EKS cluster on AWS.
Make sure your terminal in the right directory. Proceed to run following commands
terraform init
terraform apply
hint: terraform apply -auto-approve
!!! Scale down the AWS EKS cluster to 0 nodes or run the importation wizard in the Spot console.
EKS creations observed time frame is about 9-13 minutes.