This repository contains Terraform code for setting up an Amazon Elastic Kubernetes Service (EKS) cluster on AWS. The Terraform configuration files in this repository automate the provisioning of the necessary infrastructure resources, including the EKS cluster, worker nodes, VPC, and security groups.
Before you begin, ensure that you have the following prerequisites in place:
- AWS account credentials with appropriate permissions to create EKS clusters, IAM roles, and other required resources.
- Terraform installed on your local machine. You can download Terraform from the official website (https://www.terraform.io/downloads.html) and follow the installation instructions.
To set up the EKS cluster using Terraform, follow these steps:
- Clone this repository to your local machine.
- Configure your AWS credentials by setting the AWS access key and secret key as environment variables or using the AWS CLI (
aws configure). - Modify the variables.tf file in the
eksandsg_eksdirectories to customize your cluster configuration, such as VPC settings, subnet IDs, and security group rules. - Initialize Terraform by running
terraform initin each directory to download the necessary provider plugins. - Run
terraform applyto create the EKS cluster, worker nodes, VPC, and security groups. Review the planned changes and confirm by typing "yes" when prompted. - Wait for Terraform to provision the resources. This process may take several minutes.
- Once the provisioning is complete, Terraform will display the outputs, including the EKS cluster endpoint for accessing the Kubernetes API.
To interact with the EKS cluster, you can use the Kubernetes command-line tool (kubectl). Configure kubectl to connect to the EKS cluster by following these steps:
-
Install
kubectlon your local machine by downloading it from the official Kubernetes website or using a package manager. https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html -
Retrieve the cluster's endpoint and authentication details by running
terraform outputin theeksdirectory. -
Set the cluster configuration in
kubectlusing the obtained details by runningkubectl config set-cluster <cluster-name> --server=<endpoint>andkubectl config set-credentials <cluster-name> --token=<authentication-token>. -
Set the current context to the EKS cluster by running
kubectl config use-context <cluster-name>. -
Verify your connection to the cluster by running
kubectl get nodesand confirming that the worker nodes are listed.
To destroy the EKS cluster and associated resources created by Terraform, run terraform destroy in each directory. Review the planned actions and confirm by typing "yes" when prompted. This will remove all the provisioned resources from your AWS account.
Note: Destroying the resources is irreversible, and it will delete all data stored within the cluster. Make sure to back up any important data before proceeding.
Contributions to this project are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request.
This code is released under the MIT License.