Created
June 6, 2021 18:04
-
-
Save rdalbuquerque/040e394e04333b73e86a3e9fd59a5219 to your computer and use it in GitHub Desktop.
Terraform code - 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform { | |
backend "local" { | |
path = "state/terraform.tfstate" | |
} | |
required_providers { | |
aws = { | |
source = "hashicorp/aws" | |
version = "~> 3.0" | |
} | |
} | |
} | |
# Configure the AWS Provider | |
# Credentials and default region are set on envrionment variables | |
provider "aws" {} | |
# Setting up master node | |
resource "aws_instance" "kube_master" { | |
ami = "ami-02e2a5679226e293c" # ID of default Debian 10 ami (64-bit|x86) | |
instance_type = "t2.micro" # To keep it free tier eligible we will be using t2.micro | |
tags = { | |
Name = "kube-master" | |
} | |
key_name = "k8s-lab" | |
provisioner "remote-exec" { | |
connection { | |
host = self.public_ip | |
user = "admin" | |
private_key = file("~/.ssh/k8s-lab.pem") | |
} | |
inline = ["echo 'Instance ${self.public_dns} is up!'"] | |
} | |
provisioner "local-exec" { | |
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -T 300 -i '${self.public_ip},' --extra-vars 'private_ip=${self.private_ip} hostname=${split(".", self.private_dns)[0]}' --private-key ~/.ssh/k8s-lab.pem ../kube-setup/master-playbook.yml" | |
} | |
} | |
# Setting up worker nodes | |
resource "aws_instance" "kube_worker" { | |
depends_on = [aws_instance.kube_master] | |
count = 2 | |
ami = "ami-02e2a5679226e293c" # ID of default Debian 10 ami (64-bit|x86) | |
instance_type = "t2.micro" # minimum requirement for kubernetes is 2 cpus and 2 gb of ram wich would be a chargeable EC2, so we will try the micro anyways | |
tags = { | |
Name = "KubeWorker" | |
} | |
key_name = "k8s-lab" | |
provisioner "remote-exec" { | |
connection { | |
host = self.public_ip | |
user = "admin" | |
private_key = file("~/.ssh/k8s-lab.pem") | |
} | |
inline = ["echo 'Instance ${self.public_dns} is up!'"] | |
} | |
provisioner "local-exec" { | |
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -T 300 -i '${self.public_ip},' --private-key ~/.ssh/k8s-lab.pem ../kube-setup/node-playbook.yml" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment