Provisioning the Amazon EKS cluster using Terraform
10 July, 2023
4
4
0
Contributors
๐โโ๏ธ Introduction
Hi folks, this is Ankit jodhani I recently graduated from university and currently exploring and learning DevOps and cloud with AWS. I have participated in the#10WeeksOfCloudOps challenge launched by Piyush Sachdeva.
This blog is 2nd part of "Deploying Dockerized App on AWS EKS Cluster using ArgoCD and GitOps methodology with CircleCI" Link
if you just want to provision the EKS cluster using Terraform then you can follow this blog independently.
โ Prerequisites
- โ Just need time to invest in learning
- it would be great if you go through part 1 Link
๐ถ Note (Imp)
this is the 2nd blog of the Deploying Dockerized App on AWS EKS Cluster using ArgoCD and GitOps methodology with CircleCI
Blog 1 link: Click here
this is blog 2
Blog 3 link: Click here
Blog 4 link: Click here
๐ก Plan of Execution
- ๐ฏ Architecture
- ๐ฅ๏ธ local setup
- ๐จ IAM secret key
- โ๏ธ Writing terraform file
- ๐คฉ Best practices
- ๐จ๏ธ outputs
- ๐ถ Note (Please checkout once)
- ๐ Resources
๐ฏ Architecture
let's see the architecture that we are going to build as part of this blog. I request you please go through it once. it helps you a lot while building this project.
๐ฅ๏ธ Local setup
๐งโ๐ป VS Code
As a code editor, we'll utilize VS Studio. Please use this link to download it. It is incredibly portable and simple to use. Now, we're going to install the Terraform plugin in Visual Studio Code to increase your productivity. and the extension is called HashiCorp Terraform.
โ๏ธ Terraform
Please install Terraform on your machine from this link. it's very easy to install and make sure to restart your system after installation.
๐ฅ๏ธ AWS CLI
To access the full functionality of AWS from your terminal, you must install AWS-cli. The download link is here.
๐จ IAM secret key
I'm assuming you've set up all the programs and equipment. Time to launch the AWS console now. kindly visit the IAM service.
๐ Create USER
Please select the user tab from the menu on the left. to add a user, click the button in the upper right corner add user
. You must now attach the user's policy. Although we should adhere to the principle of least privilege, we need to use many services to here you can give AdministratorAccess. however, it is not at all advisable. Instead, you should choose the list of services and attach the policy appropriately. click the next
button now. , then select Create User
button.
๐ Create a Secret key
select the user that you have created just now. click on the security credentials tab. below you will find an option with the name Create Acess key just click on it. after that select CLI and mark the checkbox below and click on the Next button, giving some description about it. and then click on the Create Access key button here you will see your Access Key ID and Secret Key. make sure you download it because you won't be able to see it once you close your window. and remember DO NOT SHARE THE KEY with anyone.
๐ Configure AWS-CLI
open the terminal on your system and type aws configure
. it will ask for your Acess key ID and secret key id. please enter what we have just created. furthermore, it will ask output format JSON
default region us-east-1
. you can enter the region where you want to deploy your app. Follow the below images to get more ideas.
โ๏ธ Writing terraform file
Writing your infrastructure is now the final step. however, think twice before you act. To be clear, we're going to discuss best practices for developing code.
๐คฉ Best practices
- store state files on a remote location (Amazon S3 service)
- try to keep versioning for backups (Amazon S3 service)
- state-locking (AmazonDynamoDB service)
please clone the repository. here is the link to the repo. or hit the below command.
git clone https://github.com/AnkitJodhani/kube_terraform-4thWeekOfCloudOps.git
let's understand the folder structure of the repository.
As you can see in the above image, we have two main folders inside the repository. We have the 'module' folder where all the modules are stored, and the 'todo-list-app' folder where I've imported and utilized all the required modules to build the infrastructure.
Let's set up the backend and state-locking. (You must have s3 bucket and Dynamodb table)
open todo-list-app/backend.tf
terraform {
backend "s3" {
bucket = "GIVE-YOUR-S3-BUCKET-NAME"
key = "backend/GIVE-ANY-NAME-TO-YOUR-BACKEND-FILE.tfstate"
region = "us-east-1"
dynamodb_table = "GIVE-DYNAMODB-TABLE-NAME"
}
}
Now you need to create one file in the todo-list-app folder with the name terraform.tfvars And paste the below content in the file. you can change the below configuration depending on your need.
REGION = "us-east-1"
PROJECT_NAME = "Todo-App-EKS"
VPC_CIDR = "192.168.0.0/16"
PUB_SUB_1_A_CIDR = "192.168.0.0/18"
PUB_SUB_2_B_CIDR = "192.168.64.0/18"
PRI_SUB_3_A_CIDR = "192.168.128.0/18"
PRI_SUB_4_B_CIDR = "192.168.192.0/18"
Please take note that the above file is crucial for setting up the infrastructure, so pay close attention to the values you enter for each variable.
it's time to build the infrastructure
let install dependency for the terraform. go inside the 'todo-list-app' folder and open the terminal. hit this command to initialize and install all the dependencies.
terraform init
The below command will tell you what terrafrom is going to create for you.
terraform plan
โจFinally, HIT the below command to create the infrastructure...
terraform apply
type yes
, it will prompt you for permission.
To view the source code, kindly go to my Github repository. and there you'll find directions on how to clone the repository and run it on your personal computer.
๐จ๏ธ Outputs
It's time to see the outputs. what terraform created for us on AWS.
๐น VPC
๐น Subnets
๐น Internet gateways
๐น NAT gateway
๐น Route table and associations
๐นIAM role for EKS cluster and Node Group
๐นEKS Cluster
๐น Node group with t3.small type instance
Note: I've selected the t3.small instance type for the worker node but you can choose depending on your need. you can edit that in modules/Node-group/main.tf
the file.
๐ Resources
Terraform: https://developer.hashicorp.com/terraform/tutorials/kubernetes/eks
YouTube: https://www.youtube.com/watch?v=nIIxexG7_a8&list=PLiMWaCMwGJXkeBzos8QuUxiYT6j8JYGE5
Part 1:
๐ Thank you for reading this blog
๐โโ๏ธ Ankit Jodhani.
๐จ reach me at ankitjodhani1903@gmail.com
๐ LinkedIn https://www.linkedin.com/in/ankit-jodhani/
๐ Github project repo https://github.com/AnkitJodhani/kube_terraform-4thWeekOfCloudOps.git
๐บ Github https://github.com/AnkitJodhani
๐ฆ Twitter https://twitter.com/Ankit__Jodhani
aws
azure
docker
git
github
devops
kubernetes
linux
yaml
developer
jenkins
learninpublic
cloud
terraform
flux
kyverno
fleet
100daysdevops
pulumi
linkerd
growtogether
eks