Create an Amazon EKS cluster

You can create a cluster on Amazon Elastic Kubernetes Service (EKS) through Infrastructure as Code (IaC). This process uses the AWS and Kubernetes Terraform providers to create EKS clusters. You connect the clusters to GitLab by using the GitLab agent for Kubernetes.

Prerequisites:

  • An Amazon Web Services (AWS) account, with a set of configured security credentials.
  • A runner you can use to run the GitLab CI/CD pipeline.

Steps:

  1. Import the example project.
  2. Register the agent for Kubernetes.
  3. Configure your project.
  4. Provision your cluster.

Import the example project

To create a cluster from GitLab using Infrastructure as Code, you must create a project to manage the cluster from. In this tutorial, you start with a sample project and modify it according to your needs.

Start by importing the example project by URL.

To import the project:

  1. In GitLab, on the top bar, select Main menu > Projects > View all projects.
  2. On the right of the page, select New project.
  3. Select Import project.
  4. Select Repository by URL.
  5. For the Git repository URL, enter https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-eks.git.
  6. Complete the fields and select Create project.

This project provides you with:

Register the agent

In GitLab 14.10, a flag named certificate_based_clusters changed the Actions menu to focus on the agent rather than certificates. The flag is enabled on GitLab.com and self-managed.

To create a GitLab agent for Kubernetes:

  1. On the left sidebar, select Infrastructure > Kubernetes clusters.
  2. Select Connect a cluster (agent).
  3. From the Select an agent dropdown list, select eks-agent and select Register an agent.
  4. GitLab generates a registration token for the agent. Securely store this secret token, as you will need it later.
  5. GitLab provides an address for the agent server (KAS), which you will also need later.

Configure your project

Use CI/CD environment variables to configure your project.

Required configuration:

  1. On the left sidebar, select Settings > CI/CD.
  2. Expand Variables.
  3. Set the variable AWS_ACCESS_KEY_ID to your AWS access key ID.
  4. Set the variable AWS_SECRET_ACCESS_KEY to your AWS secret access key.
  5. Set the variable TF_VAR_agent_token to the agent token displayed in the previous task.
  6. Set the variable TF_VAR_kas_address to the agent server address displayed in the previous task.

Optional configuration:

The file variables.tf contains other variables that you can override according to your needs:

  • TF_VAR_region: Set your cluster’s region.
  • TF_VAR_cluster_name: Set your cluster’s name.
  • TF_VAR_cluster_version: Set the version of Kubernetes.
  • TF_VAR_instance_type: Set the instance type for the Kubernetes nodes.
  • TF_VAR_instance_count: Set the number of Kubernetes nodes.
  • TF_VAR_agent_namespace: Set the Kubernetes namespace for the GitLab agent.

View the AWS Terraform provider and the Kubernetes Terraform provider documentation for further resource options.

Provision your cluster

After configuring your project, manually trigger the provisioning of your cluster. In GitLab:

  1. On the left sidebar, go to CI/CD > Pipelines.
  2. Next to Play (), select the dropdown icon ().
  3. Select Deploy to manually trigger the deployment job.

When the pipeline finishes successfully, you can view the new cluster:

  • In AWS: From the EKS console, select Amazon EKS > Clusters.
  • In GitLab: On the left sidebar, select Infrastructure > Kubernetes clusters.

Use your cluster

After you provision the cluster, it is connected to GitLab and is ready for deployments. To check the connection:

  1. On the left sidebar, select Infrastructure > Kubernetes clusters.
  2. In the list, view the Connection status column.

For more information about the capabilities of the connection, see the GitLab agent for Kubernetes documentation.

Remove the cluster

A cleanup job is not included in your pipeline by default. To remove all created resources, you must modify your GitLab CI/CD template before running the cleanup job.

To remove all resources:

  1. Add the following to your .gitlab-ci.yml file:

     stages:
       - init
       - validate
       - test
       - build
       - deploy
       - cleanup
    
     destroy:
       extends: .terraform:destroy
       needs: []
    
  2. On the left sidebar, select CI/CD > Pipelines and select the most recent pipeline.
  3. For the destroy job, select Play ().