Create AWS EKS Fargate Cluster using Terraform Easily

This post will show you How to Create AWS EKS Fargate Cluster using Terraform with Step by Step Procedure.

If you are interested in learning, Request you to go through the below recommended tutorial.

DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online

Create AWS EKS Fargate Cluster using Terraform

How to Create AWS EKS Fargate Cluster using Terraform

What is AWS EKS?

AWS EKS (Elastic Kubernetes Service) is a managed Kubernetes service that makes it easy to deploy, scale, and manage containerized applications using Kubernetes. Fargate is a serverless compute engine for containers that runs on Amazon ECS and Amazon EKS.

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. It can be used to create, manage, and update resources in AWS, including EKS and Fargate. By using Terraform, you can write infrastructure as code, automate the provisioning and management of resources, and ensure that your infrastructure is in a predictable and consistent state.

Lets Create AWS EKS Fargate using Terraform.

You could use Terraform to provision the necessary resources for an EKS cluster and Fargate profile, and then configure the cluster and profile to work together. This will allow you to deploy and run your containerized applications on Fargate without the need to provision or manage the underlying EC2 instances.

Here is an example of Terraform scripts to create an AWS EKS cluster with Fargate:

First, we will create the EKS cluster:


resource "aws_eks_cluster" "example" {
  name     = "example-cluster"
  role_arn = aws_iam_role.example.arn
}
resource "aws_iam_role" "example" {
  name = "example-role"
  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "eks.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}


Next, we will create the Fargate profile and attach it to the EKS cluster:


resource "aws_eks_fargate_profile" "example" {
  name            = "example-profile"
  cluster_name    = aws_eks_cluster.example.name
  pod_execution_role_arn = aws_iam_role.fargate.arn
  subnets         = [aws_subnet.example.id]
  selectors {
    namespace = "example-namespace"
    labels = {
      app = "example-app"
    }
  }
}


resource "aws_iam_role" "fargate" {
  name = "example-fargate-role"


  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "eks-fargate-pods.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}


resource "aws_subnet" "example" {
  vpc_id                  = aws_vpc.example.id
  cidr_block              = "10.0.0.0/24"
  availability_zone       = "us-west-2a"
  map_public_ip_on_launch = true
}


resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}


Finally, we will deploy a sample Fargate pod:


resource "kubernetes_pod" "example" {
  metadata {
    name = "example-pod"
    namespace = "example-namespace"
    labels = {
      app = "example-app"
    }
  }


  spec {
    container {
      name  = "example-container"
      image = "nginx:latest"
    }
  }
}


Note: These scripts are just an example and may need to be adjusted for your specific use case. It is also important to add in appropriate security groups and policies for the EKS cluster and Fargate resources.

That’s it for this post, Hope you have got an idea on How to Create AWS EKS Fargate Cluster using Terraform Easily.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.


Post a Comment

0 Comments