Manage EKS with terraform
Let’s prepare kubernetes.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.48.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.16.1"
}
}
}
data "terraform_remote_state" "eks" {
backend = "local"
config = {
path = "../learn-terraform-provision-eks-cluster/terraform.tfstate"
}
}
# Retrieve EKS cluster information
provider "aws" {
region = data.terraform_remote_state.eks.outputs.region
}
data "aws_eks_cluster" "cluster" {
name = data.terraform_remote_state.eks.outputs.cluster_name
}
provider "kubernetes" {
host = data.aws_eks_cluster.cluster.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
args = [
"eks",
"get-token",
"--cluster-name",
data.aws_eks_cluster.cluster.name
]
}
}
terraform init
Initializing the backend...
Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding hashicorp/kubernetes versions matching ">= 2.16.1"...
- Finding hashicorp/aws versions matching ">= 4.48.0"...
- Installing hashicorp/kubernetes v2.31.0...
- Installed hashicorp/kubernetes v2.31.0 (signed by HashiCorp)
- Installing hashicorp/aws v5.57.0...
- Installed hashicorp/aws v5.57.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
https://developer.hashicorp.com/terraform/tutorials/kubernetes/kubernetes-provider