diff --git a/instances/instance.tf b/instances/instance.tf index f35a028..253d87a 100644 --- a/instances/instance.tf +++ b/instances/instance.tf @@ -1,11 +1,23 @@ terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "5.17.0" - configuration_aliases = [ aws.main ] + required_providers { + aws = { + source = "hashicorp/aws" + version = "5.17.0" + configuration_aliases = [ aws.main ] + } + tls = { + source = "hashicorp/tls" + version = "3.1.0" + } } - } +} + + +# TERRAFORM SSH KEYS + +resource "tls_private_key" "terraform_ssh_key" { + algorithm = "RSA" + rsa_bits = 4096 } @@ -29,25 +41,12 @@ locals { 6443, 10250 ] - ports_out = [ - 0, - ] } resource "aws_security_group" "project_pool" { name = "${var.project_name}_pool_security_group" description = "Security group for project pool" - dynamic "egress" { - for_each = toset(local.ports_out) - content { - from_port = egress.value - to_port = egress.value - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - } - dynamic "ingress" { for_each = toset(local.ports_in) content { @@ -57,6 +56,15 @@ resource "aws_security_group" "project_pool" { cidr_blocks = ["0.0.0.0/0"] } } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + ipv6_cidr_blocks = ["::/0"] + } + } resource "aws_instance" "main" { @@ -66,11 +74,27 @@ resource "aws_instance" "main" { key_name = aws_key_pair.ssh_key_main.key_name - user_data = templatefile("${path.module}/scripts/setup_main.sh", { + user_data = templatefile("${path.module}/scripts/setup_server.sh", { extra_key = aws_key_pair.ssh_key_ci_cd.public_key - k3s_token = var.k3s_token + terraform_key = tls_private_key.terraform_ssh_key.public_key_openssh }) + provisioner "remote-exec" { + connection { + type = "ssh" + user = "ubuntu" + agent = false + private_key = tls_private_key.terraform_ssh_key.private_key_pem + host = self.public_ip + } + + inline = [ + "echo 'curl -sfL https://get.k3s.io | K3S_TOKEN=\"${var.k3s_token}\" sh -' >> /home/ubuntu/setup.sh", + "chmod +x /home/ubuntu/setup.sh", + "exec /home/ubuntu/setup.sh | tee logs.txt" + ] + } + tags = { Name = "${var.project_name}-main" } @@ -84,12 +108,27 @@ resource "aws_instance" "worker" { key_name = aws_key_pair.ssh_key_main.key_name - user_data = templatefile("${path.module}/scripts/setup_worker.sh", { + user_data = templatefile("${path.module}/scripts/setup_server.sh", { extra_key = aws_key_pair.ssh_key_ci_cd.public_key - k3s_token = var.k3s_token - k3s_cluster_ip = var.project_domain + terraform_key = tls_private_key.terraform_ssh_key.public_key_openssh }) + provisioner "remote-exec" { + connection { + type = "ssh" + user = "ubuntu" + agent = false + private_key = tls_private_key.terraform_ssh_key.private_key_pem + host = self.public_ip + } + + inline = [ + "echo 'curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC=\"agent\" K3S_TOKEN=\"${var.k3s_token}\" sh -s - --server ${var.project_domain}:6443' >> /home/ubuntu/setup.sh", + "chmod +x /home/ubuntu/setup.sh", + "exec /home/ubuntu/setup.sh | tee logs.txt" + ] + } + tags = { Name = "${var.project_name}-worker-${count.index+1}" } @@ -100,4 +139,12 @@ resource "aws_instance" "worker" { output "pool_master_public_ip" { value = aws_instance.main.public_ip +} + +output "pool_master_instance" { + value = aws_instance.main +} + +output "pool_worker_instances" { + value = aws_instance.worker } \ No newline at end of file diff --git a/instances/scripts/setup_main.sh b/instances/scripts/setup_main.sh deleted file mode 100644 index d7f89c1..0000000 --- a/instances/scripts/setup_main.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -xe - - -echo -e "\n${extra_key}" >> /home/ubuntu/.ssh/authorized_keys; - -echo -e "export TERM='xterm-256color'" >> /home/ubuntu/.profile; - -su ubuntu -i << EOF -# curl -sfL https://get.k3s.io | \ -# K3S_TOKEN="${k3s_token}" sh -' -echo "HERE" >> /home/ubuntu/test.txt -EOF \ No newline at end of file diff --git a/instances/scripts/setup_server.sh b/instances/scripts/setup_server.sh new file mode 100644 index 0000000..441fcc4 --- /dev/null +++ b/instances/scripts/setup_server.sh @@ -0,0 +1,9 @@ +#!/bin/bash + + +echo -e "${extra_key}" >> /home/ubuntu/.ssh/authorized_keys; +echo -e "${terraform_key}" >> /home/ubuntu/.ssh/authorized_keys; + +echo "export TERM='xterm-256color';" > /home/ubuntu/.profile; + +exit 0; \ No newline at end of file diff --git a/instances/scripts/setup_worker.sh b/instances/scripts/setup_worker.sh deleted file mode 100644 index 7d5ce57..0000000 --- a/instances/scripts/setup_worker.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -xe - - -echo -e "\n${extra_key}" >> /home/ubuntu/.ssh/authorized_keys; - -echo "export TERM='xterm-256color'" > /home/ubuntu/.profile; - -su ubuntu -i << EOF -# curl -sfL https://get.k3s.io | \ -# INSTALL_K3S_EXEC="agent" \ -# K3S_TOKEN="${k3s_token}" \ -# sh -s - --server ${k3s_cluster_ip} -echo "HERE" >> /home/ubuntu/test.txt -EOF \ No newline at end of file diff --git a/k3s/k3s.tf b/k3s/k3s.tf deleted file mode 100644 index 411c761..0000000 --- a/k3s/k3s.tf +++ /dev/null @@ -1,38 +0,0 @@ -module "k3s" { - source = "xunleii/k3s/module" - version = "3.3.0" - k3s_version = "v1.21.4+k3s1" - - cluster_domain = "civo_k3s" - - drain_timeout = "60s" - managed_fields = ["label"] - generate_ca_certificates = true - - global_flags = [for instance in civo_instance.node_instances : "--tls-san ${instance.public_ip}"] - - servers = { - # The node name will be automatically provided by - # the module using the field name... any usage of - # --node-name in additional_flags will be ignored - - for instance in civo_instance.node_instances : - instance.hostname => { - ip = instance.private_ip - connection = { - timeout = "60s" - type = "ssh" - host = instance.public_ip - password = instance.initial_password - user = "root" - } - - labels = { "node.kubernetes.io/type" = "master" } - } - } -} - -output "kube_config" { - value = module.k3s.kube_config - sensitive = true -} \ No newline at end of file