Separates Into SubModules

This commit is contained in:
2023-09-22 17:13:24 -03:00
parent 0eca3287d1
commit 6a36d215b9
19 changed files with 592 additions and 288 deletions

24
instances/.terraform.lock.hcl generated Normal file
View File

@@ -0,0 +1,24 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "5.17.0"
hashes = [
"h1:U+EDfeUqefebA1h7KyBMD1xH0h311LMi7wijPDPkC/0=",
"zh:0087b9dd2c9c638fd63e527e5b9b70988008e263d480a199f180efe5a4f070f0",
"zh:0fd532a4fd03ddef11f0502ff9fe4343443e1ae805cb088825a71d6d48906ec7",
"zh:16411e731100cd15f7e165f53c23be784b2c86c2fcfd34781e0642d17090d342",
"zh:251d520927e77f091e2ec6302e921d839a2430ac541c6a461aed7c08fb5eae12",
"zh:4919e69682dc2a8c32d44f6ebc038a52c9f40af9c61cb574b64e322800d6a794",
"zh:5334c60759d5f76bdc51355d1a3ebcc451d4d20f632f5c73b6e55c52b5dc9e52",
"zh:7341a2b7247572eba0d0486094a870b872967702ec0ac7af728c2df2c30af4e5",
"zh:81d1b1cb2cac6b3922a05adab69543b678f344a01debd54500263700dad7a288",
"zh:882bc8e15ef6d4020a07321ec4c056977c5c1d96934118032922561d29504d43",
"zh:8cd4871ef2b03fd916de1a6dc7eb8a81a354c421177d4334a2e3308e50215e41",
"zh:97e12fe6529b21298adf1046c5e20ac35d0569c836a6f385ff041e257e00cfd2",
"zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
"zh:9f5baf5d59b9f3cf5504d1fa975f10f27da3791896a9e18ece47c258bac17634",
"zh:dffafba6731ac1db1c540bdbd6a8c878486b71de9d0ca1d23c5c00a6c3c14d80",
"zh:fa7440c3c15a42fc5731444d324ced75407d417bfe3184661ae47d40a9718dce",
]
}

32
instances/config.tf Normal file
View File

@@ -0,0 +1,32 @@
### SET VARIABLES
variable "project_name" {
type = string
default = "hideyoshi-portifolio"
}
variable "project_domain" {
type = string
}
variable "k3s_token" {
type = string
}
variable "number_of_workers" {
type = number
default = 2
}
variable "aws_region" {
type = string
default = "sa-east-1"
}
variable "ssh_public_key_main" {
type = string
}
variable "ssh_public_key_ci_cd" {
type = string
}

103
instances/instance.tf Normal file
View File

@@ -0,0 +1,103 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.17.0"
configuration_aliases = [ aws.main ]
}
}
}
# EC2 Instances
resource "aws_key_pair" "ssh_key_main" {
key_name = "ssh_key_main"
public_key = var.ssh_public_key_main
}
resource "aws_key_pair" "ssh_key_ci_cd" {
key_name = "ssh_key_ci_cd"
public_key = var.ssh_public_key_ci_cd
}
locals {
ports_in = [
22,
80,
443,
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 {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}
resource "aws_instance" "main" {
ami = "ami-0af6e9042ea5a4e3e"
instance_type = "t3a.medium"
vpc_security_group_ids = [ aws_security_group.project_pool.id ]
key_name = aws_key_pair.ssh_key_main.key_name
user_data = templatefile("${path.module}/scripts/setup_main.sh", {
extra_key = aws_key_pair.ssh_key_ci_cd.public_key
k3s_token = var.k3s_token
})
tags = {
Name = "${var.project_name}-main"
}
}
resource "aws_instance" "worker" {
ami = "ami-0af6e9042ea5a4e3e"
instance_type = "t2.micro"
vpc_security_group_ids = [ aws_security_group.project_pool.id ]
count = var.number_of_workers
key_name = aws_key_pair.ssh_key_main.key_name
user_data = templatefile("${path.module}/scripts/setup_worker.sh", {
extra_key = aws_key_pair.ssh_key_ci_cd.public_key
k3s_token = var.k3s_token
k3s_cluster_ip = var.project_domain
})
tags = {
Name = "${var.project_name}-worker-${count.index+1}"
}
}
# OUTPUTS
output "pool_master_public_ip" {
value = aws_instance.main.public_ip
}

View File

@@ -0,0 +1,12 @@
#!/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

View File

@@ -0,0 +1,14 @@
#!/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