+3
Read Time:3 Minute, 12 Second

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:

  1. Must have basic knowledge about AWS, Terraform, Shell Scripting, and Ansible.
  2. Read the terraform’s blog. No need to execute anything from terraform’s blog.
  3. AWS region must be Ohio (us-east-2).
  4. Already created IAM role with admin permissions.

Let’s create an EKS cluster:

  1. 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:
    1. 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.
    2. The playbook will be run as a sudo user.
    3. We have a variable for terraform version which is set to 0.14.6.
    4. We update the yum repository.
    5. Install the dependencies like wget, AWS CLI v2, Terraform 0.14.6, AWS IAM Authenticator, and Kubectl.
    6. We then clone the repository to provision the EKS cluster.
    7. Then, we use the ansible community module to ensure that the terraform deploys the EKS cluster.
    8. In the end, we configure kubeconfig to the kubectl so that we can control the cluster via CLI.

Cleanup

  1. SSH into the EC2 instance that we created to deploy the EKS cluster.
  2. Execute the below commands:
    sudo su
    cd /aws-eks-terraform-ansible
    ansible-playbook destroy.yaml -vv
  3. 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.

About Post Author

Ashutosh

Hi, my name is Ashutosh Maheshwari and I'm a DevOps Engineer. I have 5+ years of experience in AWS, Linux, Shell Scripting and DevOps technologies. I love to learn new things and Share it with the community.
+3
AWS Blog by Ashutosh Previous post Passing HashiCorp Certified Terraform Associate

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *

14 + nineteen =