Many people have written blogs on how to create an EKS cluster with terraform, but I have added Ansible to it. The reasons for that were:
- I love Ansible :).
- It gives structure to the execution steps.
- You can add pre-steps and post-steps to the deployment.
About this blog
If you are new to Infrastructure as Code (IaC), this will be a step in the right direction.
This is an extension to terraform’s blog. You can find the architecture over there.
We are going to deploy 3 worker nodes in this demo. We will use Amazon EC2, Ansible, Terraform and Amazon EKS for this solution.
Pre-requisites:
- Must have basic knowledge about AWS, Terraform, Shell Scripting, and Ansible.
- Read the terraform’s blog. No need to execute anything from terraform’s blog.
- AWS region must be Ohio (us-east-2).
- Already created IAM role with admin permissions.
Let’s create an EKS cluster:
- Launch Instance with the following specs:
- Instance AMI – Amazon Linux 2 AMI (HVM), SSD Volume Type.
- Instance Type – t3.micro.
- IAM Role – Created in the pre-requisites.
- Advanced Details – User data – add the below code:
#!/bin/bash
yum update
yum install git -y
amazon-linux-extras install ansible2 -y
ansible-galaxy collection install community.general
git clone https://github.com/ashutoshvct/aws-eks-terraform-ansible.git
sleep 10s
cd aws-eks-terraform-ansible && nohup ansible-playbook install_eks.yaml -vv
2. Security group should have SSH access to your IP and attach an existing key or create a new key for SSH.
3. Launch the Instance and after 2 mins SSH into the machine. Execute the below command to check the status of deployment:
sudo su
tail -f /var/log/cloud-init-output.log
4. Go grab a coffee because it is going to take a while, around 30-40 mins. After which you should see the 3 worker nodes deployed.
Unraveling the mystery
- We create an EC2 Instance to execute our ansible playbook which contains the IaC.
- In this instance, we install git, ansible2, and Ansible modules and plugins which are needed by terraform.
- We clone the GitHub repo where the ansible-playbook is stored. Then, we execute the playbook in the background. Let’s go through the playbook:
- We are going to run the playbook on the instance we created. The playbook is stored at
/aws-eks-terraform-ansible
. If you want to make any edits in the playbook the filename is install_eks.yaml. - The playbook will be run as a sudo user.
- We have a variable for terraform version which is set to 0.14.6.
- We update the yum repository.
- Install the dependencies like wget, AWS CLI v2, Terraform 0.14.6, AWS IAM Authenticator, and Kubectl.
- We then clone the repository to provision the EKS cluster.
- Then, we use the ansible community module to ensure that the terraform deploys the EKS cluster.
- In the end, we configure kubeconfig to the kubectl so that we can control the cluster via CLI.
- We are going to run the playbook on the instance we created. The playbook is stored at
Cleanup
- SSH into the EC2 instance that we created to deploy the EKS cluster.
- Execute the below commands:
sudo su
cd /aws-eks-terraform-ansible
ansible-playbook destroy.yaml -vv - This will clean up all the terraform resources created. Once the process completes, terminate the EC2 Instance created for the EKS cluster setup.
Tips
These are just guidelines for you to improve the solution.
- Store the state file of terraform on S3.
- Improve the ansible-playbook to use native modules.
- Convert this solution to a one-liner.
- Change the yaml file according to your needs for example version of terraform, AWS CLI, or any other tool.
- Write another ansible-playbook to deploy any app into the new EKS worker nodes.
Do read my other blogs and stay tuned for more blogs! Don’t forget to share and connect with me on LinkedIn and Twitter.