fb59a14b8793959f19cfd0bd64e4dbea1fd37a33
[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.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
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 resource "null_resource" "cluster" {
126   provisioner "remote-exec" {
127   inline = ["sudo microk8s kubectl get no >> kubectl.info"]
128   }
129
130   connection {
131     host = aws_instance.master.public_ip
132     type     = "ssh"
133     user     = "ubuntu"
134     password = ""
135     private_key = "${file("terraform.pem")}"
136   }
137
138   provisioner "local-exec" {
139   command = <<EOT
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 .
145               EOT
146   }
147
148   lifecycle {
149     create_before_destroy = true
150   }
151
152   depends_on = [
153     aws_instance.worker,
154   ]
155 }
156
157 output "public_ip" {
158   value         = aws_instance.master.public_ip
159 }
160
161 output "private_ip" {
162   value = aws_instance.master.private_ip
163 }