Spin up managed Kubernetes cluster on AWS with terraform

  1. AWS account
  2. Terraform (0.15.4)
  3. aws cli (2.0)
  1. settings.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>3.27"
}
}

backend "s3" {
bucket = "terrraformbackend"
key = "ec2-k8"
region = "us-east-1"
}
}

provider "aws" {
profile = "default"
region = "us-east-1"
}
resource "aws_instance" "master-k8" {
ami = "ami-013f17f36f8b1fefb"
instance_type = "t2.micro"
availability_zone = "us-east-1a"
key_name = "coda-k8-ec2"
security_groups = [aws_security_group.k8_sg.name]
user_data = <<-EOF
#!/bin/bash
sudo -i
apt-get update
swapoff -a
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm
apt-mark hold kubelet kubeadm

# install Docker runtime
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io -y
EOF
tags = {
created_by = "wadghulegaurav@gmail.com"
}
}


resource "aws_instance" "worker1-k8" {
ami = "ami-013f17f36f8b1fefb"
instance_type = "t2.micro"
availability_zone = "us-east-1b"
key_name = "coda-k8-ec2"
security_groups = [aws_security_group.k8_sg.name]
user_data = <<-EOF
#!/bin/bash
sudo -i
apt-get update
swapoff -a
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm
apt-mark hold kubelet kubeadm

# install Docker runtime
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io -y
EOF
tags = {
created_by = "wadghulegaurav@gmail.com"
}
}
resource "aws_security_group" "k8_sg" {
name = "k8_sg"
description = "Allow TLS inbound traffic"
vpc_id = aws_vpc.k8_vpc.id

ingress {
description = "TLS from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [aws_vpc.k8_vpc.cidr_block]
}

ingress {
description = "KubeAPI port from VPC"
from_port = 6443
to_port = 6443
protocol = "tcp"
cidr_blocks = [aws_vpc.k8_vpc.cidr_block]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "allow_tls"
}
}
resource "aws_vpc" "k8_vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"

tags = {
Name = "k8_vpc"
}
}
kubeadm init --pod-network-cidr=10.0.0.0/8
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

Devops | Kubernetes | Docker | CICD | Test Automation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to use Event API of slack bot

Connecting Excel with Python

Openpyxl

Coding Case Styles: Camel, Snake and CamelSnake

Scaling Rails

Snowflake Data Clean Room

Writing a high-quality data pipeline for master data with Apache Spark — Part 2

From Python to JavaScript FullStack

How to vote on a governance proposal in Evmos Testnet

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Wadghule

Gaurav Wadghule

Devops | Kubernetes | Docker | CICD | Test Automation

More from Medium

How to Design and Provision a Production-Ready EKS Cluster

Deploying Apps to a Local K3d Cluster

Automate Provisioning of Kubernetes Clusters on AWS with Terraform

GitOps: CI/CD using GitHub Actions and ArgoCD on Kubernetes