diff --git a/assets/setup-web.sh b/assets/setup-web.sh index 1a0ae95..edea516 100755 --- a/assets/setup-web.sh +++ b/assets/setup-web.sh @@ -4,3 +4,5 @@ cp /tmp/assets/webapp /usr/local/bin/ chmod +x /usr/local/bin/* cp /tmp/assets/webapp.service /lib/systemd/system/webapp.service service webapp start +systemctl enable webapp + diff --git a/assets/webapp.service b/assets/webapp.service index c28fb12..5455a0e 100644 --- a/assets/webapp.service +++ b/assets/webapp.service @@ -1,3 +1,6 @@ +[Install] +WantedBy=multi-user.target + [Unit] Description=Demo web app @@ -5,4 +8,5 @@ Description=Demo web app ExecStart=/usr/local/bin/webapp >> /var/log/webapp.log IgnoreSIGPIPE=false KillMode=process +Restart=on-failure diff --git a/main.tf b/main.tf index 5b367d1..009f4b0 100644 --- a/main.tf +++ b/main.tf @@ -1,9 +1,48 @@ +terraform { + required_version = ">= 0.11.0" +} + +variable "access_key" {} +variable "secret_key" {} +variable "public_key" {} +variable "private_key" {} + +variable "region" { + default = "us-east-1" +} + +variable "ami" {} +variable "subnet_id" {} +variable "vpc_security_group_id" {} +variable "identity" {} + +variable "num_webs" { + default = "1" +} + provider "aws" { - access_key = "" - secret_key = "" - region = "" + version = "~> 1.5" + access_key = "${var.access_key}" + secret_key = "${var.secret_key}" + region = "${var.region}" +} + +module "server" { + source = "./server" + + num_webs = "${var.num_webs}" + ami = "${var.ami}" + subnet_id = "${var.subnet_id}" + vpc_security_group_id = "${var.vpc_security_group_id}" + identity = "${var.identity}" + public_key = "${var.public_key}" + private_key = "${var.private_key}" +} + +output "public_ip" { + value = "${module.server.public_ip}" } -resource "aws_instance" "web" { - # ... +output "public_dns" { + value = "${module.server.public_dns}" } diff --git a/server/main.tf b/server/main.tf new file mode 100644 index 0000000..5c1a162 --- /dev/null +++ b/server/main.tf @@ -0,0 +1,39 @@ +variable "ami" {} + +variable "num_webs" {} + +variable "subnet_id" {} +variable "vpc_security_group_id" {} +variable "identity" {} +variable "public_key" {} +variable "private_key" {} + +resource "aws_key_pair" "training" { + key_name = "${var.identity}-key" + public_key = "${var.public_key}" +} + +resource "aws_instance" "web" { + ami = "${var.ami}" + instance_type = "t2.nano" + count = "${var.num_webs}" + + subnet_id = "${var.subnet_id}" + vpc_security_group_ids = ["${var.vpc_security_group_id}"] + + key_name = "${aws_key_pair.training.id}" + + tags { + "Name" = "web ${count.index+1}/${var.num_webs}" + "Identity" = "${var.identity}" + "Created by" = "Terraform" + } +} + +output "public_ip" { + value = ["${aws_instance.web.*.public_ip}"] +} + +output "public_dns" { + value = ["${aws_instance.web.*.public_dns}"] +}