2 region = var.aws_region
3 access_key = var.access_key
4 secret_key = var.secret_key
8 resource "aws_instance" "master" {
10 instance_type = var.aws_instance
11 vpc_security_group_ids = [aws_security_group.instance.id]
12 subnet_id = var.aws_subnet_id
16 apt update -y >> microk8s_install.log
17 apt install snapd -y >> microk8s_install.log
18 snap install core >> microk8s_install.log
19 export PATH=$PATH:/snap/bin
20 snap install microk8s --classic --channel=1.20/stable >> microk8s_install.log
21 microk8s status --wait-ready
22 microk8s enable dns >> microk8s_install.log
23 microk8s add-node > microk8s.join_token
24 microk8s config > configFile-master
26 key_name = "terraform"
30 provisioner "remote-exec" {
31 inline = ["until [ -f /microk8s.join_token ]; do sleep 5; done; cat /microk8s.join_token",
32 "sudo sed -i 's/#MOREIPS/IP.7 = ${self.public_ip}\\n#MOREIPS/g' /var/snap/microk8s/current/certs/csr.conf.template",
44 private_key = "${file("terraform.pem")}"
47 provisioner "local-exec" {
50 ssh-keyscan -H ${self.public_dns} >> ~/.ssh/known_hosts
51 scp -i terraform.pem ubuntu@${self.public_dns}:/microk8s.join_token .
52 tail -n1 microk8s.join_token >> token
53 scp -i terraform.pem ubuntu@${self.public_dns}:/configFile-master .
58 create_before_destroy = true
62 resource "aws_security_group" "instance" {
63 name = "master_microk8s"
69 cidr_blocks = ["0.0.0.0/0"]
75 cidr_blocks = ["0.0.0.0/0"]
80 data "local_file" "tokenContent" {
89 public_dns = aws_instance.master.public_dns
90 join = data.local_file.tokenContent.content
94 resource "aws_instance" "worker" {
96 instance_type = var.aws_instance
97 vpc_security_group_ids = [aws_security_group.instance.id]
98 subnet_id = var.aws_subnet_id
99 user_data = templatefile("worker_user_data.tmpl", { token = local.join } )
100 key_name = "terraform"
105 provisioner "remote-exec" {
106 inline = ["until [ -f /microk8s.complete ]; do sleep 5; done"]
110 host = self.public_ip
114 private_key = "${file("terraform.pem")}"
117 create_before_destroy = true
125 resource "null_resource" "cluster" {
126 provisioner "remote-exec" {
127 inline = ["sudo microk8s kubectl get no >> kubectl.info"]
131 host = aws_instance.master.public_ip
135 private_key = "${file("terraform.pem")}"
138 provisioner "local-exec" {
140 echo ${aws_instance.master.private_ip}
141 export privateIP=${aws_instance.master.private_ip}
142 export publicIP=${aws_instance.master.public_ip}
143 ssh-keyscan -H ${aws_instance.worker.public_dns} >> ~/.ssh/known_hosts
144 scp -i terraform.pem ubuntu@${aws_instance.worker.public_dns}:/configFile-worker .
149 create_before_destroy = true
158 value = aws_instance.master.public_ip
161 output "private_ip" {
162 value = aws_instance.master.private_ip