Bash Script : How to Create a k8s Cluster on AWS EKS using eksctl.
Tags: kubernetes, aws, eks, cluster
Published: 2023-02-19 20:27:25

This article is a simple shell script tutorial about automating your kubernetes cluster setup on Amazon Elastic Kubernetes Service (EKS).

**AWS EKS:** _Amazon Elastic Kubernetes Service (EKS) is a managed service and certified Kubernetes conformant to run Kubernetes on AWS and on-premises._

**Eksctl:** _is a simple CLI tool for creating and managing clusters on EKS - Amazon's managed Kubernetes service for EC2._

**Prerequisite**
- [Kubectl](https://kubernetes.io/docs/tasks/tools/) is installed
- [eksctl](https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html)  is installed
- [AWS CLI version 2](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) is installed
- [AWS account](https://aws.amazon.com/free/)
- IAM role with EKS permission.
- Have a Key Pair on AWS


**_Create an access key for your IAM user_**
- Login into your AWS account
- Go to IAM dashboard
- Select _**Users**_ and click on your IAM user name
- Click on _**Security Credential Tab**_
- Scroll down to Access keys and Click on _**Create access key**_
- Select _**Command Line Interface (CLI)**_, agree with recommendation click Next and follow the prompts.

![access key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1of7co69ry5fce8hdngl.png)

**Configure AWS CLI**
```bash
# In your CLI, configure AWS cli with your access keys
aws configure
```
![aws configure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3v00piqxt678nzt4sgs.png)

Create a file with permission

```bash
# Create file
vi scriptfile.sh

# Make execute permission
chmod +x scriptfile.sh
```

Copy and paste the code snippet below. Change the variables to your preferred names and values. Also change the values under the _**Creation of EKS cluster**_ to preferred cluster requirements.

```bash
# Variables
CLUSTER_NAME=my-cluster
REGION=us-east-1
NODE_NAME=Linux-nodes
KEY_NAME=instance

# Set AWS credentials before script execution

aws sts get-caller-identity >> /dev/null
if [ $? -eq 0 ]
then
  echo "Credentials tested, proceeding with the cluster creation."

  # Creation of EKS cluster
  eksctl create cluster \
  --name $CLUSTER_NAME \
  --version 1.22 \
  --region $REGION \
  --nodegroup-name $NODE_NAME \
  --nodes 2 \
  --nodes-min 1 \
  --nodes-max 4 \
  --node-type t3.micro \
  --node-volume-size 8 \
  --ssh-access \
  --ssh-public-key $KEY_NAME \
  --managed

  if [ $? -eq 0 ]
  then
    echo "Cluster Setup Completed with eksctl command."
  else
    echo "Cluster Setup Failed while running eksctl command."
  fi
else
  echo "Please run aws configure & set right credentials."
  echo "Cluster setup failed."
fi

```

> Make sure the key pair is in the same directory with the script or you specify.

![script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmuq9z9d6w02f8mzmxwa.png)

`aws sts get-caller-identity` - checks if the AWS identity is created correctly. `if [ $? -eq 0 ]` - check if the last command, in this case `aws sts get-caller-identity` succeeded without no error. If it equals 0, it should go ahead and create the cluster. After the cluster is created, print a _created message_ else inform us the AWS credential was not configured correctly.


![eks cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzw09nbrwe6gnos0rvng.png)

Cluster Nodes on EC2 dashboard

![ec2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdzso3j9h5na5vktoxzv.png)

**Clean Up**
  ```bash
  # Delete EKS Cluster
  eksctl delete cluster my-cluster
  ```

![delete cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrpjxqj6o3k6oxi1o9vn.png)

As always, I look forward to getting your thoughts on this article. Please feel free to leave a comment!