Skip to content

Sample code that uses Terraform to create an EKS cluster and deploy the Ghost application in that EKS cluster

License

Notifications You must be signed in to change notification settings

dungpham91/terraform-eks

Repository files navigation

Creating Amazon EKS (Elastic Kubernetes Service) with Terraform

Repository contains sample code that uses Terraform to create an EKS cluster and deploy the Ghost application in that EKS cluster.

Some of the goals this project accomplishes:

  • Use Terraform to create an EKS cluster.
  • Use Terraform to deploy Ghost applications to EKS cluster.
  • Use RDS H-A architecture for the database layer of Ghost application.
  • Using EFS for the storage of Ghost applications, allowing containers to use the same folder containing files.
  • Implement centralized logging system using ELK stack to collect logs from Ghost application.
  • Deploy a monitoring system using Grafana and Prometheus to monitor system resources.
  • Test some cases after the deployment is complete, for example: auto scaling when CPU is high, fault injection simulator,...

Table of contents

1. Prepare

1.1 AWS CLI

Follow the official guide to install and configure profiles.

1.2 Terraform

To install Terraform, find the appropriate package (https://www.terraform.io/downloads.html) for your system and download it.

Also, it is recommended that you use an Ubuntu computer to install Terraform by following the commands below:

$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install terraform

1.3 Kubernetes CLI

To install Kubernetes CLI, follow the instructions of this page (https://kubernetes.io/docs/tasks/tools) and if you are using Ubuntu computer follow this page (https://kubernetes.io/docs/tasks/tools/install-kubectl-linux).

2. Getting started

2.1 Create an S3 bucket to use as a backend for Terraform

In real projects, it is very dangerous to store Terraform state locally if it is lost or changed unintentionally.

Besides, it is possible to have a team with many members working with Terraform, so saving the state locally is not feasible. Members need to get the latest state of the project and be able to sync the new state to S3 every time they update the code.

So the first step before actually getting into creating the EKS cluster and deploying the applications, you need to create an S3 bucket to use as the backend for the main Terraform project.

2.1.1 Move into the folder s3-backend change the required value

2.1.2 Create S3 bucket for Terraform backend

At the s3-backend directory, execute the following commands in turn:

$ terraform init
$ terraform plan # This command to check if the code has any problem, it will output the changed information to the screen
$ terraform apply # Implement resource creation

When the terraform apply command finishes, you will get an information on the terminal window like the image below. Save this information and use it in the next section.

01-create-s3-backend

2.2 Create EKS cluster and deploy Ghost application

2.2.1 Change the necessary values before creating the EKS cluster

  • First, get the output values in step 2.1.2, information about the S3 bucket for remote backend. Please replace these values in the file backend.tf

  • Next, update the path shared_credentials_files to the credential file in the file main.tf

  • Finally, change the values to your liking in the file terraform.tfvars

2.2.2 Implement Terraform to create EKS cluster and deploy Ghost application

At the main project directory, execute the following commands:

$ terraform init
$ terraform plan # This command to check if the code has any problem, it will output the changed information to the screen
$ terraform apply # Implement resource creation

The output of the terraform apply command should look like this:

02-create-eks-cluster-deploy-ghost

2.2.3 Go to the Ghost app

You copy the output alb_hostname and paste it in your browser, this is ALB's public DNS pointing to the Ghost application in the EKS cluster.

05-access-ghost-1

I won't go into too much detail about using Ghost, here I test it in action by simply setting up an Admin account and then creating an article called Test Post.

To set up an Admin account for the first time, go to the application's URL and add the path /ghost. Specifically would be: https://8be9a4a9-fargatenode-ghost-bbb9-1433839261.ap-southeast-1.elb.amazonaws.com/ghost.

Please note that this link is for demo purposes only, I will delete it after testing is done. Please use the link on your environment.

Now, I can log in to the Ghost app with an Admin account.

05-access-ghost-2

05-access-ghost-3

2.3 Connect kubeclt to the newly created EKS cluster

Run the command below to update the configuration for kubectl:

$ aws eks --region $(terraform output -raw eks_region) update-kubeconfig --name $(terraform output -raw eks_cluster_name)

Check the connection with the command get node information:

$ kubectl get nodes

The result like this:

03-connect-kubectl-to-eks

View pod list information:

$ kubectl get pod -A | grep Running

04-list-pods

3. What has been done and checked

  • Using S3 bucket as backend for Terraform

    Terraform's state is stored on S3, which is more secure than local storage.

06-s3-bucket-backend

Also check the access to the public state file, make sure it's not exposed to the internet.

07-access-s3-bucket-backend

  • Deploy successfully applied Ghost to EKS cluster through Terraform tool, using Kubernetes provider.

  • Successfully created RDS Aurora engine MySQL 5.7 database and used it for Ghost application.

4. More things to do

  • Upgrade RDS Aurora to global mode.

  • Create an EFS and connect it to the Ghost application.

  • Build system logging & monitoring

  • Fault testing, scale testing,...

5. Clean everything

After you finish using the project, maybe for testing purposes, for example. You can delete all created AWS resources like this:

$ terraform destroy # Delete EKS cluster and related resources
$ cd s3-backend
$ terraform destroy # Delete the S3 bucket used for the Terraform backend

6. Software version

Software Version
AWS CLI 1.18.69
Terraform 1.1.8
kubectl 1.23
EKS 1.21

About

Sample code that uses Terraform to create an EKS cluster and deploy the Ghost application in that EKS cluster

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages