doc/user/infrastructure/clusters/connect/new_eks_cluster.md
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.
Before you begin:
Steps:
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:
https://gitlab.com/gitlab-org/configure/examples/gitlab-terraform-eks.git.This project provides you with:
{{< history >}}
certificate_based_clusters changed the Actions menu to focus on the agent rather than certificates. Disabled by default.{{< /history >}}
To create a GitLab agent for Kubernetes:
eks-agent and select Register an agent.Set up your AWS credentials when you want to authenticate AWS with GitLab.
Make sure that your IAM user or role has the appropriate permissions for your project. For this example project, you must have the permissions listed in the following JSON block. You can expand these permissions when you set up your own project.
// IAM custom Policy definition
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:*",
"eks:*",
"elasticloadbalancing:*",
"autoscaling:*",
"cloudwatch:*",
"logs:*",
"kms:DescribeKey",
"kms:TagResource",
"kms:UntagResource",
"kms:ListResourceTags",
"kms:CreateKey",
"kms:CreateAlias",
"kms:ListAliases",
"kms:DeleteAlias",
"iam:AddRoleToInstanceProfile",
"iam:AttachRolePolicy",
"iam:CreateInstanceProfile",
"iam:CreateRole",
"iam:CreateServiceLinkedRole",
"iam:GetRole",
"iam:ListAttachedRolePolicies",
"iam:ListRolePolicies",
"iam:ListRoles",
"iam:PassRole",
"iam:DetachRolePolicy",
"iam:ListInstanceProfilesForRole",
"iam:DeleteRole",
"iam:CreateOpenIDConnectProvider",
"iam:CreatePolicy",
"iam:TagOpenIDConnectProvider",
"iam:GetPolicy",
"iam:GetPolicyVersion",
"iam:GetOpenIDConnectProvider",
"iam:DeleteOpenIDConnectProvider",
"iam:ListPolicyVersions",
"iam:DeletePolicy"
],
"Resource": "*"
}
]
}
Save your access key and secret. You need these to authenticate AWS with GitLab.
Use CI/CD environment variables to configure your project.
Required configuration:
AWS_ACCESS_KEY_ID to your AWS access key ID.AWS_SECRET_ACCESS_KEY to your AWS secret access key.TF_VAR_agent_token to the agent token displayed in the previous task.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 for Kubernetes.View the AWS Terraform provider and the Kubernetes Terraform provider documentation for further resource options.
After configuring your project, manually trigger the provisioning of your cluster. In GitLab:
When the pipeline finishes successfully, you can view the new cluster:
After you provision the cluster, it is connected to GitLab and is ready for deployments. To check the connection:
For more information about the capabilities of the connection, see the GitLab agent for Kubernetes documentation.
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:
Add the following to your .gitlab-ci.yml file:
stages:
- init
- validate
- test
- build
- deploy
- cleanup
destroy:
extends: .terraform:destroy
needs: []
In the left sidebar, select Build > Pipelines and select the most recent pipeline.
For the destroy job, select Play ({{< icon name="play" >}}).