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

Topfunky demo patch #43

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c1d01d3
Code after end of class
topfunky Dec 2, 2017
aae9fd8
Add variable declarations for all values
topfunky Dec 2, 2017
a6ba40e
Default to creating one instance
topfunky Dec 4, 2017
c52e9e6
Bundle Go app and deploy with terraform
topfunky Dec 4, 2017
ad5e892
Remove echo module
topfunky Dec 4, 2017
ccb623a
Fix whitespace and trailing comma
topfunky Dec 4, 2017
1e705ca
Refactor to a module
topfunky Dec 4, 2017
3ef4e52
Set version for AWS provider
topfunky Dec 5, 2017
87f612b
Revert to `num_webs` instead of `total_webs`
topfunky Dec 6, 2017
b6cc88e
Refactor variable names to match lab
topfunky Dec 6, 2017
65309e1
Simplify server setup with a script
topfunky Dec 7, 2017
4bcfac4
Rename variables to match attribute names
topfunky Dec 7, 2017
967c69e
Fix path to script
topfunky Dec 7, 2017
586713b
Cleanup unused code. Remove redundant default.
topfunky Dec 13, 2017
f05db9e
Merge from `master`
topfunky Dec 19, 2017
218e54b
Tweaks for use on a student's own AWS account
topfunky Jan 24, 2018
64b7e5b
Ensure that webapp starts on reboot
topfunky Jan 25, 2018
0ca74da
Tweak for use without SSH
topfunky Jan 25, 2018
a0300e2
Remove unnecessary code now that an AMI is used
topfunky Jan 26, 2018
70cba1c
Variables for resources to satify security requirements
topfunky Jan 26, 2018
95a8dd5
Refactor to individual files. Make independent of other AWS infra.
topfunky Feb 15, 2018
b4fb90f
Notes on the code in this branch
topfunky Feb 15, 2018
0c1e7db
Refactor to work with Terraform Enterprise
topfunky Jun 2, 2018
401682e
Notes about requirements for running this branch
topfunky Jun 2, 2018
7b903a0
0.12 updates
Jul 2, 2019
196ecca
Update readme to TF Cloud
Jul 2, 2019
70a1b00
Update main.tf
Jul 2, 2019
2584116
required version
Jul 7, 2019
7456d7b
Updating with 201 content
Sep 6, 2019
338f3ef
Revert "Updating with 201 content"
Sep 6, 2019
546cde8
Updating with 201 code
Sep 6, 2019
062e0b0
Update variables.tf
Sep 6, 2019
bf64f1e
Updating workspace names
Sep 6, 2019
d7d1ade
Merge pull request #33 from hashicorp/res-after-tfc
Sep 6, 2019
f4788e7
patched provider version
Sep 9, 2019
9e9a1ca
Merge branch 'master' into topfunky-demo-patch
jschnab Sep 9, 2019
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
4 changes: 4 additions & 0 deletions 01_read_state/primary/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# primary/main.tf
output "public_ip" {
value = "8.8.8.8"
}
12 changes: 12 additions & 0 deletions 01_read_state/secondary/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# secondary/main.tf
# Read state from another Terraform config’s state
data "terraform_remote_state" "primary" {
backend = "local"
config = {
path = "../primary/terraform.tfstate"
}
}

output "primary_public_ip" {
value = data.terraform_remote_state.primary.outputs.public_ip
}
29 changes: 29 additions & 0 deletions 02_store_state/read_state/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
terraform {
backend "remote" {
organization = "<ORGANIZATION NAME>"

workspaces {
name = "lab_2_read_state"
}
}
}

resource "random_id" "random" {
keepers = {
uuid = uuid()
}

byte_length = 8
}

data "terraform_remote_state" "write_state" {
backend = "remote"

config = {
organization = "<ORGANIZATION NAME>"

workspaces = {
name = "lab_2_write_state"
}
}
}
23 changes: 23 additions & 0 deletions 02_store_state/write_state/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# lab_2_terraform_cloud_demo/write_state/main.tf
terraform {
backend "remote" {
organization = "<ORGANIZATION NAME>"

workspaces {
name = "lab_2_write_state"
}
}
}


resource "random_id" "random" {
keepers = {
uuid = uuid()
}

byte_length = 8
}

output "random" {
value = random_id.random.hex
}
8 changes: 8 additions & 0 deletions 03_tfc_basics/assets/setup-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh

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

Binary file added 03_tfc_basics/assets/webapp
Binary file not shown.
12 changes: 12 additions & 0 deletions 03_tfc_basics/assets/webapp.service
Original file line number Diff line number Diff line change
@@ -0,0 +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

27 changes: 27 additions & 0 deletions 03_tfc_basics/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
terraform {
required_version = ">= 0.12.0"
}

provider "aws" {
region = var.region
version = ">= 2.27.0"

}

module "keypair" {
source = "mitchellh/dynamic-keys/aws"
version = "2.0.0"
name = "${var.identity}-key"
}

module "server" {
source = "./server"

num_webs = var.num_webs
identity = var.identity
ami = var.ami
ingress_cidr = var.ingress_cidr
key_name = module.keypair.key_name
private_key = module.keypair.private_key_pem
}

8 changes: 8 additions & 0 deletions 03_tfc_basics/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output "public_ip" {
value = module.server.public_ip
}

output "public_dns" {
value = module.server.public_dns
}

63 changes: 63 additions & 0 deletions 03_tfc_basics/server/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
resource "aws_security_group" "default" {
name_prefix = var.identity

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [var.ingress_cidr]
}

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.ingress_cidr]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Created-by = "Terraform"
Identity = var.identity
}
}

resource "aws_instance" "web" {
ami = var.ami
instance_type = "t2.medium"
count = var.num_webs

vpc_security_group_ids = [aws_security_group.default.id]

key_name = var.key_name

tags = {
Name = "${var.identity} web ${count.index + 1}/${var.num_webs}"
Identity = var.identity
}

connection {
type = "ssh"
user = "ubuntu"
private_key = var.private_key
host = self.public_ip
}

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

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

8 changes: 8 additions & 0 deletions 03_tfc_basics/server/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output "public_ip" {
value = [aws_instance.web.*.public_ip]
}

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

24 changes: 24 additions & 0 deletions 03_tfc_basics/server/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "private_key" {
description = "EC2 instance private key - generated"
}

variable "key_name" {
description = "Key name - generated"
}

variable "ami" {
description = "Base machine image for running this server"
}

variable "num_webs" {
description = "The number of servers to create"
default = 1
}

variable "identity" {
description = "A unique name for this server"
}

variable "ingress_cidr" {
description = "IP address block from which connections to this instance will be made"
}
4 changes: 4 additions & 0 deletions 03_tfc_basics/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
identity="demo-wallaby"
ingress_cidr="0.0.0.0/0"
public_key="AAAA"
private_key="AAAA"
23 changes: 23 additions & 0 deletions 03_tfc_basics/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "identity" {
description = "A unique name for your resources"
}

variable "ami" {
description = "The Amazon Machine Image for new instances."
default = "ami-0735ea082a1534cac"
}

variable "ingress_cidr" {
default = "0.0.0.0/0"
description = "IP block from which connections to this instance will be made"
}


variable "num_webs" {
description = "The number of servers to run"
default = "1"
}

variable "region" {
default = "us-east-1"
}
2 changes: 2 additions & 0 deletions 03_tfc_basics/webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rice-box.go
dist/*
29 changes: 29 additions & 0 deletions 03_tfc_basics/webapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Web app

A web app written in Go that uses websockets to log visits and push notifications to all visitors.

Defaults to running on port 80. Set `PORT` as ENV var to specify another port.

### Build

Build for Linux and Darwin:

./bin/build

Output can be found in `dist`.

A copy of the Linux executable will also be copied to `../assets` for deployment with Terraform.

### Run

PORT=8080 go run main.go

### View

http://localhost:8080

## Credits

Browser icons from https://github.com/alrra/browser-logos


79 changes: 79 additions & 0 deletions 03_tfc_basics/webapp/assets/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var data = [{ date: new Date(), value: 1 }];
var ids = {};

const windowWidth = $(window).width();
function renderChart(data) {
MG.data_graphic({
title: "Visits",
description: "This graphic shows a time series of page views.",
data: data,
width: windowWidth < 800 ? windowWidth - 40 : 700,
height: windowWidth < 800 ? 150 : 250,
chart_type: "histogram",
bins: 30,
bar_margin: 0,
target: "#chart",
x_accessor: "date",
y_accessor: "value"
});
}

renderChart(data);

function addTableRow(record) {
// {browser:"firefox", epochs:39929283829, ip:"10.0.0.1", agent:"Chrome 238.23"}
$("#visits tbody").prepend(
'<tr><td><img src="images/128/' +
record.browser +
'_128x128.png"></td><td>' +
'<div class="metadata"><span class="time">' +
moment(record.epochs).format("LT") +
"</span></div>" +
'<div class="agent">' +
record.agent +
"</div>" +
"</td></tr>"
);
}

function logVisit(record) {
if (ids[record.id] === undefined) {
record.date = new Date(record.epochs);
data.push(record);
renderChart(data);
addTableRow(record);
ids[record.id] = 1;
}
}

var socket = io({ transports: ["websocket"] });

// Listen for messages
socket.on("message", function(message) {
logVisit(message);
});

socket.on("connect", function() {
// Broadcast a message
var message = {};
const epochs = new Date().getTime();

function broadcastMessage() {
message.epochs = new Date().getTime();
message.id = message.epochs;
socket.emit("send", message, function(result) {
// Silent success
});
}

message = {
id: epochs,
browser: navigator.appName.toLowerCase(),
epochs: epochs,
agent: navigator.userAgent,
value: 1
};
broadcastMessage();

setInterval(broadcastMessage, 30 * 1000);
});
2 changes: 2 additions & 0 deletions 03_tfc_basics/webapp/assets/dependencies/d3.v4.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions 03_tfc_basics/webapp/assets/dependencies/jquery-3.2.1.min.js

Large diffs are not rendered by default.

Loading