Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usmc686 patch 2 #6

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/setup-web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 4 additions & 0 deletions assets/webapp.service
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
[Install]
WantedBy=multi-user.target

[Unit]
Description=Demo web app

[Service]
ExecStart=/usr/local/bin/webapp >> /var/log/webapp.log
IgnoreSIGPIPE=false
KillMode=process
Restart=on-failure

49 changes: 44 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
55 changes: 55 additions & 0 deletions server/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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"
}

connection {
user = "ubuntu"
private_key = "${$var.private_key}"
}

provisioner "file" {
source = "assets"
destination = "/tmp/"
}

provisioner "remote-exec" {
inline = [
"sudo sh /tmp/assets/setup-web.sh",
]
}
}

output "public_ip" {
value = ["${aws_instance.web.*.public_ip}"]
}

output "public_dns" {
value = ["${aws_instance.web.*.public_dns}"]
}