Skip to content
Merged
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
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.20.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