DevOps Task: Provisioning Infrastructure as a code with terraform for Web Applications and Databases (MongoDB, SQL Server) and Redis for caching, with Domain Name, Certificate, and Load Balancer, Using docker and Kubernetes.
This Terraform project automates the provisioning and deployment of an Amazon Elastic Kubernetes Service (EKS) cluster in AWS. It also includes the deployment of Kubernetes manifests to configure the cluster according to your application's requirements. Below is an overview of the project's files and their functionalities:
.
├── domain.tf
├── eks.tf
├── kubernetes_manifests/
│ ├── web_app_manifest.yaml
│ ├── web_app_service.yaml
│ ├── sqlserver_statefulset.yaml
│ ├── sqlserver_service.yaml
│ ├── redis_statefulset.yaml
│ ├── redis_service.yaml
│ ├── mongodb_statefulset.yaml
│ └── mongodb_service.yaml
├── locals.tf
├── manifest.tf
├── obtain_ssl_cert.sh
├── providers.rf
├── security.tf
├── ssl.tf
├── variables.tf
└── vpc.tf
Before running this Terraform project, ensure you have the following prerequisites:
- An AWS account with appropriate permissions to create resources like VPC, EKS cluster, and EC2 instances.
- Terraform installed on your local machine.
- AWS CLI installed and configured with proper credentials.
- An existing Kubernetes configuration (kubeconfig) file pointing to the desired Kubernetes cluster.
This file defines the networking components for the EKS cluster. It uses the terraform-aws-modules/vpc/aws
module to create a VPC with public and private subnets. The VPC spans multiple availability zones (azs
) in the specified CIDR block (cidr_block
).
In this file, you can define the variables used throughout the Terraform configuration. The provided variables are as follows:
region
: The AWS region where the resources will be provisioned. Default value:us-east-1
.subnet_count
: The number of subnets to create in each availability zone. Default value:2
.cidr_block
: The CIDR block for the VPC. Default value:10.0.0.0/16
.domain_name
: The desired domain name for your application.
Feel free to adjust these variables according to your requirements.
The providers file configures the necessary providers required for this project:
aws
: The AWS provider used to create AWS resources like VPC, subnets, etc.kubernetes
: The Kubernetes provider that interacts with the EKS cluster to deploy Kubernetes manifests.kubectl
: The provider to interact with kubectl, which allows deploying Kubernetes manifests from the local machine.
This file defines local variables used within the Terraform configuration. It includes variables for the VPC and tags associated with the resources.
This file provisions the EKS cluster using the terraform-aws-modules/eks/aws
module. It configures the cluster name, version, VPC, subnets, and other essential settings.
This file creates a managed node group for the EKS cluster using the terraform-aws-modules/eks/aws//modules/eks-managed-node-group
module. It specifies the instance types, desired capacity, and other settings for the worker nodes.
This file deploys the Kubernetes manifests to the EKS cluster using the kubectl_manifest
resource. It reads the YAML files from the kubernetes_manifests/
directory and applies them to the cluster.
This file defines the security groups for the web servers, Redis, MongoDB, and SQL Server instances. It allows traffic between these resources as required for your application.
This file manages the Route 53 zone and DNS record for your domain. It uses ACM to obtain an SSL certificate for the domain.
This file obtains an SSL certificate for your domain using Certbot and exports the certificate ARN to be used by Terraform.
This shell script is used by the ssl.tf
file to obtain an SSL certificate using Certbot.
This directory contains the Kubernetes manifest files that will be deployed to the EKS cluster. You can place all your YAML files for different services and resources in this directory.
Follow these steps to provision and deploy the EKS cluster and Kubernetes manifests:
-
Ensure you have met all the prerequisites mentioned above.
-
Clone this Terraform project to your local machine.
-
Navigate to the project directory:
cd /path/to/your/terraform_project
- Initialize the Terraform configuration:
terraform init
- Review the Terraform plan to see the resources that will be created:
terraform plan
- If the plan looks good, apply the configuration:
terraform apply
- Terraform will create the necessary resources on
This Terraform project is designed to work with the provided default configurations. However, you can customize it to fit your specific requirements. Here are some ways you can customize the project:
-
Adjust Variables: In the
variables.tf
file, you can modify the default values of variables such as the AWS region, VPC CIDR block, number of subnets, domain name, etc. -
Kubernetes Manifests: The
kubernetes_manifests/
directory contains YAML files for deploying Kubernetes resources. You can modify these files or add new ones to deploy additional services or applications. -
SSL Certificate: By default, the project uses Certbot to obtain an SSL certificate for your domain. If you have an existing SSL certificate, you can modify the
ssl.tf
file to use your certificate ARN directly. -
Worker Nodes: The
node_groups.tf
file configures the managed node group for the EKS cluster. You can modify the instance types, capacity, and other settings based on your application's resource requirements.
-
The Terraform project provisions resources in your AWS account. Ensure that you have proper access control and security measures in place to protect your infrastructure.
-
When deploying Kubernetes manifests, ensure that sensitive data like secrets and credentials are properly encrypted and managed. Use Kubernetes secrets or other secure methods to store such information.
-
Regularly review and update security group rules to limit unnecessary access and ensure that only required ports are open.
If you encounter any issues while running this Terraform project, consider the following troubleshooting steps:
-
Verify Prerequisites: Ensure that you have met all the prerequisites, including proper AWS credentials, Terraform installation, and a valid kubeconfig file.
-
Check Terraform Output: After running
terraform apply
, carefully review the output to check for any errors or warnings. It provides valuable information about the resources created. -
Examine Terraform Logs: If needed, examine the Terraform logs to get detailed information about the resource provisioning process.
-
Check Kubernetes Logs: If there are issues with Kubernetes resources, use
kubectl logs
andkubectl describe
commands to troubleshoot pods and services. -
Review IAM Permissions: Ensure that the IAM roles and policies associated with the EKS cluster and worker nodes have the necessary permissions to access AWS resources.
If you would like to contribute to this project, feel free to fork the repository
Happy Terraforming and Kubernetes Deploying!