2022-02-18 03:17:36 +00:00
---
stage: Configure
group: Configure
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
---
2022-02-21 15:19:50 +00:00
# Create an Amazon EKS cluster
2022-02-18 03:17:36 +00:00
2022-02-21 15:19:50 +00:00
You can create a cluster on Amazon Elastic Kubernetes Service (EKS) through
[Infrastructure as Code (IaC) ](../../index.md ). 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.
2022-02-18 03:17:36 +00:00
**Prerequisites:**
- An Amazon Web Services (AWS) account, with a set of configured
[security credentials ](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-prereqs.html ).
2022-02-21 15:19:50 +00:00
- [A runner ](https://docs.gitlab.com/runner/install/ ) you can use to run the GitLab CI/CD pipeline.
2022-02-18 03:17:36 +00:00
**Steps:**
1. [Import the example project ](#import-the-example-project ).
2022-02-21 15:19:50 +00:00
1. [Register the agent for Kubernetes ](#register-the-agent ).
2022-02-18 03:17:36 +00:00
1. [Configure your project ](#configure-your-project ).
1. [Provision your cluster ](#provision-your-cluster ).
## Import the example project
2022-02-21 15:19:50 +00:00
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 ](../../../project/import/repo_by_url.md ).
2022-02-18 03:17:36 +00:00
2022-02-21 15:19:50 +00:00
To import the project:
2022-02-18 03:17:36 +00:00
2022-09-15 18:10:36 +00:00
1. In GitLab, on the top bar, select **Main menu > Projects > View all projects** .
1. On the right of the page, select **New project** .
2022-02-21 15:19:50 +00:00
1. Select **Import project** .
2022-05-31 21:09:06 +00:00
1. Select **Repository by URL** .
2022-02-21 15:19:50 +00:00
1. For the **Git repository URL** , enter `https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-eks.git` .
1. Complete the fields and select **Create project** .
This project provides you with:
2022-02-18 03:17:36 +00:00
- An Amazon [Virtual Private Cloud (VPC) ](https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-eks/-/blob/main/vpc.tf ).
- An Amazon [Elastic Kubernetes Service (EKS) ](https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-eks/-/blob/main/eks.tf ) cluster.
2022-02-21 15:19:50 +00:00
- The [GitLab agent for Kubernetes ](https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-eks/-/blob/main/agent.tf ) installed in the cluster.
2022-02-18 03:17:36 +00:00
2022-02-21 15:19:50 +00:00
## Register the agent
2022-02-18 03:17:36 +00:00
2022-03-29 00:09:12 +00:00
FLAG:
In GitLab 14.10, a [flag ](../../../../administration/feature_flags.md ) 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 ](https://gitlab.com/groups/gitlab-org/configure/-/epics/8 ).
2022-02-21 15:19:50 +00:00
To create a GitLab agent for Kubernetes:
2022-02-18 03:17:36 +00:00
2022-02-21 15:19:50 +00:00
1. On the left sidebar, select **Infrastructure > Kubernetes clusters** .
2022-03-29 00:09:12 +00:00
1. Select **Connect a cluster (agent)** .
2022-02-21 15:19:50 +00:00
1. From the **Select an agent** dropdown list, select `eks-agent` and select **Register an agent** .
1. GitLab generates a registration token for the agent. Securely store this secret token, as you will need it later.
1. GitLab provides an address for the agent server (KAS), which you will also need later.
2022-02-18 03:17:36 +00:00
## Configure your project
2022-02-21 15:19:50 +00:00
Use CI/CD environment variables to configure your project.
2022-02-18 03:17:36 +00:00
**Required configuration:**
1. On the left sidebar, select **Settings > CI/CD** .
1. Expand **Variables** .
1. Set the variable `AWS_ACCESS_KEY_ID` to your AWS access key ID.
1. Set the variable `AWS_SECRET_ACCESS_KEY` to your AWS secret access key.
2022-02-21 15:19:50 +00:00
1. Set the variable `TF_VAR_agent_token` to the agent token displayed in the previous task.
1. Set the variable `TF_VAR_kas_address` to the agent server address displayed in the previous task.
2022-02-18 03:17:36 +00:00
**Optional configuration:**
The file [`variables.tf` ](https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-eks/-/blob/main/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.
2022-02-22 03:14:19 +00:00
- `TF_VAR_agent_namespace` : Set the Kubernetes namespace for the GitLab agent.
2022-02-18 03:17:36 +00:00
2022-02-21 15:19:50 +00:00
View the [AWS Terraform provider ](https://registry.terraform.io/providers/hashicorp/aws/latest/docs ) and the [Kubernetes Terraform provider ](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs ) documentation for further resource options.
2022-02-18 03:17:36 +00:00
## Provision your cluster
After configuring your project, manually trigger the provisioning of your cluster. In GitLab:
2022-02-21 15:19:50 +00:00
1. On the left sidebar, go to **CI/CD > Pipelines** .
2022-06-10 18:09:28 +00:00
1. Next to **Play** (**{play}**), select the dropdown icon (**{chevron-lg-down}**).
2022-02-21 15:19:50 +00:00
1. Select **Deploy** to manually trigger the deployment job.
2022-02-18 03:17:36 +00:00
2022-02-21 15:19:50 +00:00
When the pipeline finishes successfully, you can view the new cluster:
2022-02-18 03:17:36 +00:00
2022-02-21 15:19:50 +00:00
- In AWS: From the [EKS console ](https://console.aws.amazon.com/eks/home ), select **Amazon EKS > Clusters** .
- In GitLab: On the left sidebar, select **Infrastructure > Kubernetes clusters** .
2022-02-18 03:17:36 +00:00
2022-02-19 00:19:25 +00:00
## 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** .
1. In the list, view the **Connection status** column.
For more information about the capabilities of the connection, see [the GitLab agent for Kubernetes documentation ](../index.md ).
2022-02-21 15:19:50 +00:00
## Remove the cluster
2022-02-18 03:17:36 +00:00
A cleanup job is not included in your pipeline by default. To remove all created resources, you
2022-02-21 15:19:50 +00:00
must modify your GitLab CI/CD template before running the cleanup job.
2022-02-18 03:17:36 +00:00
To remove all resources:
2022-02-21 15:19:50 +00:00
1. Add the following to your `.gitlab-ci.yml` file:
2022-02-18 03:17:36 +00:00
```yaml
stages:
- init
- validate
2022-07-22 06:10:47 +00:00
- test
2022-02-18 03:17:36 +00:00
- build
- deploy
- cleanup
destroy:
2022-07-20 18:08:44 +00:00
extends: .terraform:destroy
2022-02-18 03:17:36 +00:00
needs: []
```
2022-02-21 15:19:50 +00:00
1. On the left sidebar, select **CI/CD > Pipelines** and select the most recent pipeline.
1. For the `destroy` job, select **Play** (**{play}**).