The purpose of this tutorial is to create 4 identical Suse Linux Enterprise 15.2 virtual machines in AWS that we will deploy only with Terraform.
With the goal of using this infrastrucutre to deploy a kubernetes cluster .
Architecture
We will create 4 VMs :
master-node-0
worker-node-0
worker-node-1
worker-node-2
Prerequisites
Before you get started, you’ll need to have these things:
Terraform > 0.13.x
kubectl installed on the compute that hosts terraform
The first thing to set up is your Terraform. We will create an AWS IAM users for Terraform.
In your AWS console, go to the IAM section and create a user named “Terraform”. Then add your user to a group named “FullAccessGroup”. Attaches to this group the following rights:
After these steps, AWS will provide you a Secret Access Key and Access Key ID. Save them preciously because this will be the only time AWS gives it to you.
In your own console, create a ~/.aws/credentials file and put your credentials in it:
Clone the repository and install the dependencies:
We will immediately create a dedicated ssh key pair to connect to our AWS EC2 instances.
We now have two files id_rsa_aws and id_rsa_aws.pub in our ssh-keys directory.
Take a closer look at the Terraform configuration files.
We have a first file main.tf with the following content. This file will contain general information about Terraform and its relationship with AWS:
Let’s detail the contents of this file:
The file vpc.tf which contains the information of our virtual private cloud (vpc), namely a logical and independent organization of our infrastructure in the AWS cloud.
We will define a VPC and its Subnet and then define the routing table for the VPC
In the security.tf file we will define the security rule to allow ssh access only from some specific ips (for security reasons) and allow the vm to access anywhere:
In the master_instence.tf and worker_instance files we will define the description of our instances :
master_instance.tf file :
worker_instance.tf file :
This master_instance file contains two blocks of code starting with the resource keyword. Let’s detail the first block of code in this file :
resource : this keyword indicates that we will define a resource, which is the basic unit of Terraform. Here it is of type aws_key_pair, defining a key pair to use to connect to our EC2 instances.
key_name : the name of the key pair, which we will use to identify it in other resources.
public_key: the SSH public key, which will be deposited in your instances, allowing the connection via SSH.
Let’s now detail the second resource of the file which is identical in worker_instance file.
resource : contains a resource of type aws_instance named “master-nodes” or “worker-nodes”.
ami : the ami indicated here is the official image of the Suse Linux Enterprise 15.2 distribution for the chosen region (it varies according to the region).
instance_type : the AWS instance type, which defines the performance of the virtual machine, here a very modest template.
key_name : the name of the key pair to SSH into the instance, defined by the previous resource.
count : The count meta-argument accepts a whole number, and creates that many instances of the resource or module.(only use in worker_instance)
subnet_id : the subnet of the instance defined in the vpc.tf file
security_groups : the security group defined in the security.tf file
In the file variables.tf we will define the default values for the instances:
the instance type
the number of instances worker
the number of master instances
In the file outpu.tf fwe will define the variables which are shared between the modules
Usage
Let’s deploy our infrastructure :
Use terraform init command in terminal to initialize terraform and download the configuration files.
After a few minutes our instances are up running
Tear down the whole Terraform plan with :
Resources can be destroyed using the terraform destroy command, which is similar to terraform apply but it behaves as if all of the resources have been removed from the configuration.
Let’s have a look at the console:
We have our 4 instances created.
Remote control
Now we are now going to connect via SSH to one of the newly created EC2 instances, for example the master-node-0 instance. For this we use the previously generated SSH key and the user ec2-user and with the Public IPv4 DNS :
We get a shell on the newly created EC2 instance.
Conclusion
With Terraform, it easy and fast it is to create an AWS Virtual Machines infrastructure.
Terraform is one of the most popular Infrastructure-as-code (IaC) tool used by DevOps teams to automate infrastructure tasks. It is used to automate the provisioning of your cloud resources.. It is currently the best tool to automate your infrastructure creation.
It supports multiple providers such as AWS, Azure, Oracle, GCP, and many more.