From f5a881e0a97ef6489e92579e4165095edb590746 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 24 Oct 2023 20:11:38 -0300 Subject: [PATCH 1/5] Implements Deploy via Dispatcher --- .github/workflows/terraform.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index 58a3e15..2935718 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -42,4 +42,13 @@ jobs: # Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks - name: Terraform Apply if: github.event_name == 'push' - run: terraform apply -auto-approve -input=false \ No newline at end of file + run: terraform apply -auto-approve -input=false + + - name: Deploy Project via Dispatcher + env: + DISPATCHER_EVENT: ${{env.TF_WORKSPACE == 'prod' && 'deploy-prod' || 'deploy-staging'}} + run: | + curl -X POST https://api.github.com/repos/HideyoshiNakazone/infra-hideyoshi.com/dispatches \ + -H 'Accept: application/vnd.github.everest-preview+json' \ + -u ${{ secrets.ACTIONS_KEY }} \ + --data '{"event_type": "${{env.DISPATCHER_EVENT}}" }' \ No newline at end of file From 4b5c9dbcc2844470c37d749f976e21716e8f9a7c Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 24 Oct 2023 22:07:05 -0300 Subject: [PATCH 2/5] Resizes Instances for Better Cost --- .github/workflows/terraform.yml | 3 ++- instances/instance.tf | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index 2935718..226f389 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -45,8 +45,9 @@ jobs: run: terraform apply -auto-approve -input=false - name: Deploy Project via Dispatcher + if: env.TF_WORKSPACE == 'prod' env: - DISPATCHER_EVENT: ${{env.TF_WORKSPACE == 'prod' && 'deploy-prod' || 'deploy-staging'}} + DISPATCHER_EVENT: 'deploy-prod' run: | curl -X POST https://api.github.com/repos/HideyoshiNakazone/infra-hideyoshi.com/dispatches \ -H 'Accept: application/vnd.github.everest-preview+json' \ diff --git a/instances/instance.tf b/instances/instance.tf index bf6c8c6..525e91e 100644 --- a/instances/instance.tf +++ b/instances/instance.tf @@ -72,7 +72,7 @@ resource "aws_security_group" "project_pool" { resource "aws_instance" "main" { ami = "ami-0af6e9042ea5a4e3e" - instance_type = "t3a.small" + instance_type = "t2.micro" vpc_security_group_ids = [ aws_security_group.project_pool.id ] key_name = aws_key_pair.ssh_key_main.key_name From 8aaa518baa7fbe978963a17bf4a6d3502068d795 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 24 Oct 2023 23:04:17 -0300 Subject: [PATCH 3/5] Adds Swap for Cost --- instances/scripts/setup_server.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/instances/scripts/setup_server.sh b/instances/scripts/setup_server.sh index 441fcc4..49e6b9f 100644 --- a/instances/scripts/setup_server.sh +++ b/instances/scripts/setup_server.sh @@ -1,6 +1,13 @@ #!/bin/bash +# Setting up swap space +sudo fallocate -l 2G /swapfile +sudo mkswap /swapfile +sudo chmod 600 /swapfile +sudo swapon /swapfile + +# Configuring ssh keys echo -e "${extra_key}" >> /home/ubuntu/.ssh/authorized_keys; echo -e "${terraform_key}" >> /home/ubuntu/.ssh/authorized_keys; From 7cf314ea6429413841a0272446a07d5addd1ee93 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 24 Oct 2023 23:14:59 -0300 Subject: [PATCH 4/5] Fixes Setup Script --- instances/scripts/setup_server.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/instances/scripts/setup_server.sh b/instances/scripts/setup_server.sh index 49e6b9f..090ca2b 100644 --- a/instances/scripts/setup_server.sh +++ b/instances/scripts/setup_server.sh @@ -1,10 +1,10 @@ #!/bin/bash # Setting up swap space -sudo fallocate -l 2G /swapfile -sudo mkswap /swapfile -sudo chmod 600 /swapfile -sudo swapon /swapfile +fallocate -l 2G /swapfile +mkswap /swapfile +chmod 600 /swapfile +swapon /swapfile # Configuring ssh keys From ea499c397747c0ec14fcfc30b4b8eb03e8a22b57 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 24 Oct 2023 23:57:38 -0300 Subject: [PATCH 5/5] Fixes Swap Configuration --- instances/instance.tf | 8 ++++++++ instances/scripts/setup_server.sh | 7 ------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/instances/instance.tf b/instances/instance.tf index 525e91e..4663845 100644 --- a/instances/instance.tf +++ b/instances/instance.tf @@ -92,6 +92,10 @@ resource "aws_instance" "main" { } inline = [ + "sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024", + "sudo /sbin/mkswap /var/swap.1", + "sudo chmod 600 /var/swap.1", + "sudo /sbin/swapon /var/swap.1", "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", @@ -130,6 +134,10 @@ resource "aws_instance" "worker" { } inline = [ + "sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024", + "sudo /sbin/mkswap /var/swap.1", + "sudo chmod 600 /var/swap.1", + "sudo /sbin/swapon /var/swap.1", "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", diff --git a/instances/scripts/setup_server.sh b/instances/scripts/setup_server.sh index 090ca2b..286c4ba 100644 --- a/instances/scripts/setup_server.sh +++ b/instances/scripts/setup_server.sh @@ -1,12 +1,5 @@ #!/bin/bash -# Setting up swap space -fallocate -l 2G /swapfile -mkswap /swapfile -chmod 600 /swapfile -swapon /swapfile - - # Configuring ssh keys echo -e "${extra_key}" >> /home/ubuntu/.ssh/authorized_keys; echo -e "${terraform_key}" >> /home/ubuntu/.ssh/authorized_keys;