diff --git a/README.md b/README.md index d632745..af975b2 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Launches EC2 instance, creates private key, and saves it on the machine. Usage: ``` -terraform apply -var="instance_type=t2.nano" -var="instance_name=k3s-box" -var="instance_ami=ami-0889a44b331db0194" -var="availability_zone=us-east-1a" -var="create_ebs_block_device=false" - var="enable_ingress_http=false" -auto-approve +terraform apply -var="instance_type=t2.nano" -var="instance_name=k3s-box" -var="instance_ami=ami-0889a44b331db0194" -var="availability_zone=us-east-1a" -var="create_ebs_block_device=false" - var="enable_ingress_http=false" -var="duckdns_domain=" -var="duckdns_token=" -auto-approve ``` Defaults of vars are set to the ones provided in "Usage" \ No newline at end of file diff --git a/ec2.tf b/ec2.tf index 806875b..e149a55 100644 --- a/ec2.tf +++ b/ec2.tf @@ -12,7 +12,26 @@ resource "aws_instance" "k3s_box" { count = var.create_ebs_block_device ? 1 : 0 } - user_data = "${file("./script.sh")}" + # user_data = "${file("./setup_scripts/script.sh")}" + + provisioner "file" { + source = "./setup_scripts/install_crontab.sh" + destination = "/home/ec2-user" + } + + provisioner "file" { + source = "./setup_scripts/duckdns.sh" + destination = "/home/ec2-user" + } + + provisioner "remote-exec" { + inline = [ + "chmod +x /home/ec2-user/install_crontab.sh", + "/home/ec2-user/install_crontab.sh", + "chmod +x /home/ec2-user/duckdns.sh", + "/home/ec2-user/duckdns.sh ${var.duckdns_domain} ${var.duckdns_token}" + ] + } tags = { Name = var.instance_name diff --git a/setup_scripts/duckdns.sh b/setup_scripts/duckdns.sh new file mode 100644 index 0000000..5f01742 --- /dev/null +++ b/setup_scripts/duckdns.sh @@ -0,0 +1,10 @@ +#! /bin/bash + +mkdir duckdns +cd duckdns +touch duck.sh +echo url="https://www.duckdns.org/update?domains=$1&token=$2&ip=" | curl -k -o ~/duckdns/duck.log -K - +chmod 700 duck.sh +sudo touch /var/spool/cron/root +sudo echo "*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1" >> /var/spool/cron/root +./duck.sh diff --git a/setup_scripts/install_crontab.sh b/setup_scripts/install_crontab.sh new file mode 100644 index 0000000..be2166c --- /dev/null +++ b/setup_scripts/install_crontab.sh @@ -0,0 +1,6 @@ +#! /bin/bash + +sudo yum install cronie -y +sudo systemctl enable crond.service +sudo systemctl start crond.service +sudo systemctl status crond.service > /home/ec2-user/crontab-status.txt \ No newline at end of file diff --git a/script.sh b/setup_scripts/script.sh similarity index 100% rename from script.sh rename to setup_scripts/script.sh diff --git a/variables.tf b/variables.tf index 24288ed..18057c9 100644 --- a/variables.tf +++ b/variables.tf @@ -26,4 +26,14 @@ variable "create_ebs_block_device" { variable "enable_ingress_http" { type = bool default = false +} + +variable "duckdns_domain" { + type = string + default = "None" +} + +variable "duckdns_token" { + type = string + default = "None" } \ No newline at end of file