
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 calledlayerform.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.
eks folder within your terraform folder. Within that folder, you’ll add a vpc.tf file.
vpc.tf file creates a VPC for the EKS cluster you’ll add next.
eks.tf file within the eks folder.
eks.tf file will use the terraform-aws-modules/eks module to create an EKS instance which uses the VPC in vpc.tf.
outputs.tf file to export your cluster’s cluster_id so that you can use it in the layers above.
outputs.tf file, add the HCL code below.
eks.tf file outside the eks folder.
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 alayerform.json file in your project’s root folder. You’ll use this file to store all layer definitions.
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.
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 inlayerform.json.
Creating Terraform files for the Kibana and Elasticsearch layer
Start by creating aninputs.tf file to receive the EKS cluster_id you’ll use to configure the Kubernetes provider.
cluster_id.
providers.tf file and configure the kubernetes provider to connect to the cluster whose cluster_id you will receive as an input.
pods.tf file in which you’ll create a namespace for the Kibana and Elasticsearch pods and the pods themselves.
kubernetes_namespace and kubernetes_pod resources, as shown below.
elastic_stack.tf file in the layerform folder which declares the module passing the necessary cluster_id variables to it.
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 yourlayerform.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.
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 useterraform apply with a main.tf file that instantiates both module’s layers.
