Creating layer definitions
Create layer definitions for Kubernetes, Elasticsearch, Kibana, and Beats.
In this part of the tutorial you’ll learn how to create your first layer definitions.
First, we’ll create a base layer definition for our Kubernetes cluster. Then, we’ll create a layer definition containing Kibana and Elasticsearch pods to go on top of base layer instances.
Before we start, make sure to install the layerform
CLI.
Creating a base layer definition
To create a layer you must first create the Terraform files that belong to it. Then, you must a JSON file called layerform.json
to be able to provision a layer definition.
Creating the base layer’s Terraform files
First, let’s create terraform files for an EKS instance (AWS’s managed Kubernetes offering).
By convention, Layerform recommends you to encapsulate each layer into its own Terraform module. That way, it’s easier to understand each layer’s inputs and outputs, and which files belong to it.
Start by creating an eks
folder within your terraform
folder. Within that folder, you’ll add a vpc.tf
file.
This vpc.tf
file creates a VPC for the EKS cluster you’ll add next.
Now that you have a VPC, go ahead and create an eks.tf
file within the eks
folder.
This eks.tf
file will use the terraform-aws-modules/eks
module to create an EKS instance which uses the VPC in vpc.tf
.
Finally, create an outputs.tf
file to export your cluster’s cluster_id
so that you can use it in the layers above.
In this outputs.tf
file, add the HCL code below.
The last thing you must do is add a eks.tf
file outside the eks
folder.
In that outer eks.tf
file, you should declare an instance of the eks
module.
It’s up to you to define how you want this EKS cluster to be accessible.
To make these examples work without too much effort, I recommend exposing services to the world using a NodePort
or configuring nginx-ingress-controller
so that your instances get an URL when you write an ingress for them in your Terraform files.
Creating the base layer’s definition
To create a layer definition, create a layerform.json
file in your project’s root folder. You’ll use this file to store all layer definitions.
Within that file, create an object within the root key layers
. This layer has a name base
and a list of files which includes all files within the eks
folder and the outer eks.tf
file, which instantiates the module.
Now, configure the back-end in which this layer will be stored.
Now, run layerform configure
in the outermost folder to create a layer definition in your machine.
The file containing the actual layer definition will be saved in the directory specified in your back-end.
Creating a layer definition for Kibana and Elasticsearch
To create a layer definition for Kibana and Elasticsearch pods, we’ll follow a similar process. First, we’ll create a Terraform module and a file declaring it. Then, we’ll create a layer entry in layerform.json
.
Creating Terraform files for the Kibana and Elasticsearch layer
Start by creating an inputs.tf
file to receive the EKS cluster_id
you’ll use to configure the Kubernetes provider.
Within that file, add a variable for the cluster_id
.
Now, create a providers.tf
file and configure the kubernetes
provider to connect to the cluster whose cluster_id
you will receive as an input.
Within that file, add the data-source for the EKS cluster and instantiate the provider.
Then, create a pods.tf
file in which you’ll create a namespace for the Kibana and Elasticsearch pods and the pods themselves.
To create the namespace and pods, you’ll use the kubernetes_namespace
and kubernetes_pod
resources, as shown below.
Finally, you should go ahead and create an elastic_stack.tf
file in the layerform
folder which declares the module passing the necessary cluster_id
variables to it.
Within that module, you’ll use the output cluster_id
from eks
as an input
to the elastic_stack
module.
The elastic_stack
layer will go on top of the base
layer. Therefore, it’s fine for elastic_stack
to use values belonging to the Terraform files of the base
layer below, like modules.eks.cluster_id
.
On the other hand, you can’t use values from layers above in the layers below. The base
layer couldn’t reference values belonging to Terraform files of the elastic_stack
layer, for example.
Creating the actual layer definition for Kibana and Elasticsearch
Now, update your layerform.json
to include a layer definition entry for the elastic_stack
layer. This layer depends on base
and contains the Terraform files in the elastic_stack
folder as well as elastic_stack.tf
.
Finally, run layerform configure
again to update your json
definitions in the back-end.
Testing your Terraform files
The files you just created are just plain Terraform files. Therefore, they should work just fine if you use terraform apply
with a main.tf
file that instantiates both module’s layers.