diff --git a/.github/workflows/delete-staging.yml b/.github/workflows/delete-staging.yml index 12b9860..ef45e73 100644 --- a/.github/workflows/delete-staging.yml +++ b/.github/workflows/delete-staging.yml @@ -30,6 +30,13 @@ jobs: # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. - name: Terraform Init run: terraform init + + - name: Terraform Ignore Buckets + run: | + terraform state rm \ + module.bucket.aws_s3_bucket.default \ + module.bucket.aws_s3_bucket_policy.default \ + module.bucket.aws_s3_bucket_public_access_block.bucket_public_disabled # Generates an execution plan for Terraform - name: Terraform Plan diff --git a/config.tf b/config.tf index 7f32b44..068435b 100644 --- a/config.tf +++ b/config.tf @@ -1,5 +1,9 @@ ### SET VARIABLES +variable "environment_name" { + type = string +} + variable "project_name" { type = string default = "hideyoshi-portifolio" diff --git a/dns/dns.tf b/dns/dns.tf index 153c6c2..226efc2 100644 --- a/dns/dns.tf +++ b/dns/dns.tf @@ -12,6 +12,10 @@ variable "public_ip" { type = string } +variable "environment_name" { + type = string +} + resource "godaddy_domain_record" "default" { domain = "hideyoshi.com.br" @@ -19,7 +23,7 @@ resource "godaddy_domain_record" "default" { overwrite = false record { - name = "staging " + name = var.environment_name == "prod" ? "@" : "staging" type = "A" data = "${var.public_ip}" ttl = 600 @@ -27,7 +31,7 @@ resource "godaddy_domain_record" "default" { } record { - name = "api.staging " + name = var.environment_name == "prod" ? "api" : "api.staging" type = "A" data = "${var.public_ip}" ttl = 600 diff --git a/instances/instance.tf b/instances/instance.tf index 2096c8a..423d482 100644 --- a/instances/instance.tf +++ b/instances/instance.tf @@ -34,12 +34,13 @@ resource "aws_key_pair" "ssh_key_ci_cd" { } locals { - ports_in = [ - 22, - 80, - 443, - 6443, - 10250 + rules_in = [ + { "port": 22, "protocol": "tcp" }, + { "port": 80, "protocol": "tcp" }, + { "port": 443, "protocol": "tcp" }, + { "port": 6080, "protocol": "tcp" }, + { "port": 6443, "protocol": "tcp" }, + { "port": 10250, "protocol": "tcp" }, ] } @@ -48,11 +49,11 @@ resource "aws_security_group" "project_pool" { description = "Security group for project pool" dynamic "ingress" { - for_each = toset(local.ports_in) + for_each = toset(local.rules_in) content { - from_port = ingress.value - to_port = ingress.value - protocol = "tcp" + from_port = ingress.value["port"] + to_port = ingress.value["port"] + protocol = ingress.value["protocol"] cidr_blocks = ["0.0.0.0/0"] } } @@ -69,7 +70,7 @@ resource "aws_security_group" "project_pool" { resource "aws_instance" "main" { ami = "ami-0af6e9042ea5a4e3e" - instance_type = "t3a.medium" + instance_type = "t3a.small" vpc_security_group_ids = [ aws_security_group.project_pool.id ] key_name = aws_key_pair.ssh_key_main.key_name @@ -89,14 +90,13 @@ resource "aws_instance" "main" { } inline = [ - "echo 'curl -sfL https://get.k3s.io | K3S_TOKEN=\"${var.k3s_token}\" sh -' >> /home/ubuntu/setup.sh", + "echo 'curl -sfL https://get.k3s.io | K3S_TOKEN=\"${var.k3s_token}\" K3S_KUBECONFIG_MODE=644 INSTALL_K3S_EXEC=\"server --disable=traefik\" sh -' >> /home/ubuntu/setup.sh", + "echo 'mkdir /home/ubuntu/.kube' >> /home/ubuntu/setup.sh", + "echo 'sudo chmod 644 /etc/rancher/k3s/k3s.yaml' >> /home/ubuntu/setup.sh", + "echo 'cp /etc/rancher/k3s/k3s.yaml /home/ubuntu/.kube/k3s.yaml' >> /home/ubuntu/setup.sh", + "echo 'export KUBECONFIG=/home/ubuntu/.kube/k3s.yaml' >> /home/ubuntu/.profile", "chmod +x /home/ubuntu/setup.sh", "exec /home/ubuntu/setup.sh | tee logs.txt", - "mkdir /home/ubuntu/.kube", - "sudo chmod 644 /etc/rancher/k3s/k3s.yaml", - "cp /etc/rancher/k3s/k3s.yaml /home/ubuntu/.kube/k3s.yaml", - "echo 'export KUBECONFIG=/home/ubuntu/.kube/k3s.yaml' >> /home/ubuntu/profile", - "export KUBECONFIG=/home/ubuntu/.kube/k3s.yaml" ] } @@ -128,7 +128,7 @@ resource "aws_instance" "worker" { } 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", + "echo 'curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC=\"agent\" K3S_TOKEN=\"${var.k3s_token}\" K3S_URL=\"${var.project_domain}:6443\" sh -s -' >> /home/ubuntu/setup.sh", "chmod +x /home/ubuntu/setup.sh", "while ! nc -z ${aws_instance.main.public_ip} 6443; do sleep 0.1; done", "exec /home/ubuntu/setup.sh | tee logs.txt", diff --git a/main.tf b/main.tf index cca818a..0b93773 100644 --- a/main.tf +++ b/main.tf @@ -56,4 +56,5 @@ module "dns" { godaddy = godaddy } public_ip = module.instances.pool_master_public_ip + environment_name = var.environment_name } \ No newline at end of file