change in Microk8s version from 1.20 to 1.21.
[iec.git] / src / foundation / microk8s / main.tf
1 provider "aws" {
2   region = var.aws_region
3   access_key = var.access_key
4   secret_key = var.secret_key
5
6 }
7
8 resource "aws_instance" "master" {
9   ami           = var.aws_ami
10   instance_type = var.aws_instance
11   vpc_security_group_ids = [aws_security_group.instance.id]
12   subnet_id =  var.aws_subnet_id
13   user_data = <<-EOF
14               #!/bin/bash
15               sudo su
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.21 >> 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
25               EOF
26   key_name = "terraform"
27   tags = {
28     Name = "master"
29   }
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",
33             "sudo sleep 1m",
34             "sudo microk8s stop",
35             "sudo microk8s start"
36            ]
37   }
38
39   connection {
40     host = self.public_ip
41     type     = "ssh"
42     user     = "ubuntu"
43     password = ""
44     private_key = "${file("terraform.pem")}"
45   }
46
47   provisioner "local-exec" {
48     command = <<EOT
49                touch token 
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 .
54               EOT
55   }
56
57   lifecycle {
58     create_before_destroy = true
59   }
60 }
61
62 resource "aws_security_group" "instance" {
63   name = "master_microk8s"
64   vpc_id = var.vpc_id
65   ingress {
66     from_port   = 0
67     to_port     = 0
68     protocol    = "-1"
69     cidr_blocks = ["0.0.0.0/0"]
70   }
71   egress {
72     from_port   = 0
73     to_port     = 0
74     protocol    = "-1"
75     cidr_blocks = ["0.0.0.0/0"]
76   }
77
78 }
79
80 data "local_file" "tokenContent" {
81   filename = "token"
82   depends_on = [
83     aws_instance.master,
84   ]
85 }
86
87
88 locals {
89     public_dns = aws_instance.master.public_dns
90     join = data.local_file.tokenContent.content
91 }
92
93
94 resource "aws_instance" "worker" {
95   ami           = var.aws_ami
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"
101   tags = {
102     Name = "worker"
103   }
104
105   provisioner "remote-exec" {
106   inline = ["until [ -f /microk8s.complete ]; do sleep 5; done"]
107   }
108
109   connection {
110     host = self.public_ip
111     type     = "ssh"
112     user     = "ubuntu"
113     password = ""
114     private_key = "${file("terraform.pem")}"
115   }
116   lifecycle {
117     create_before_destroy = true
118   }
119
120   depends_on = [
121     aws_instance.master,
122   ]
123 }
124
125
126 resource "null_resource" "cluster" {
127   provisioner "remote-exec" {
128   inline = ["sudo microk8s kubectl get no >> kubectl.info"]
129   }
130
131   connection {
132     host = aws_instance.master.public_ip
133     type     = "ssh"
134     user     = "ubuntu"
135     password = ""
136     private_key = "${file("terraform.pem")}"
137   }
138
139   provisioner "local-exec" {
140   command = <<EOT
141                echo ${aws_instance.master.private_ip}
142                export privateIP=${aws_instance.master.private_ip}
143                export publicIP=${aws_instance.master.public_ip}
144                ssh-keyscan -H ${aws_instance.worker.public_dns} >> ~/.ssh/known_hosts
145                scp -i terraform.pem ubuntu@${aws_instance.worker.public_dns}:/configFile-worker .
146               EOT
147   }
148
149   lifecycle {
150     create_before_destroy = true
151   }
152
153   depends_on = [
154     aws_instance.worker,
155   ]
156 }
157
158 output "public_ip" {
159   value         = aws_instance.master.public_ip
160 }
161
162 output "private_ip" {
163   value = aws_instance.master.private_ip
164 }
165