Skip to content

Commit

Permalink
Generate code from book
Browse files Browse the repository at this point in the history
  • Loading branch information
brikis98 committed Nov 16, 2016
1 parent 2f68573 commit 8fb9223
Show file tree
Hide file tree
Showing 107 changed files with 1,945 additions and 0 deletions.
19 changes: 19 additions & 0 deletions code/bash/01-why-terraform/setup-webserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# A script that configures a web server

set -e

# Update the apt-get cache
sudo apt-get update

# Install PHP
sudo apt-get install -y php

# Install Apache
sudo apt-get install -y apache2

# Copy the code from repository
sudo git clone https://github.com/brikis98/php-app.git /var/www/html/app

# Start Apache
sudo service apache2 start
3 changes: 3 additions & 0 deletions code/bash/02-intro-to-terraform-syntax/run-webserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
13 changes: 13 additions & 0 deletions code/bash/03-terraform-state/bash-unit-test-example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

export db_address=12.34.56.78
export db_port=5555
export server_port=8888

./user-data.sh

output=$(curl "http://localhost:$server_port")

if [[ $output != *"Hello, World"* ]]; then
echo "Did not get back expected text 'Hello, World'"
fi
9 changes: 9 additions & 0 deletions code/bash/03-terraform-state/user-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

cat > index.html <<EOF
<h1>Hello, World</h1>
<p>DB address: ${db_address}</p>
<p>DB port: ${db_port}</p>
EOF

nohup busybox httpd -f -p "${server_port}" &
19 changes: 19 additions & 0 deletions code/packer/01-why-terraform/webserver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"builders": [{
"ami_name": "packer-example",
"instance_type": "t2.micro",
"region": "us-east-1",
"type": "amazon-ebs",
"source_ami": "ami-40d28157",
"ssh_username": "ubuntu"
}],
"provisioners": [{
"type": "shell",
"inline": [
"sudo apt-get update",
"sudo apt-get install -y php",
"sudo apt-get install -y apache2",
"sudo git clone https://github.com/brikis98/php-app.git /var/www/html/app"
]
}]
}
44 changes: 44 additions & 0 deletions code/ruby/06-terraform-team/terraform-test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'net/http'

if ARGV.length != 3
raise 'Invalid args. Usage: terraform-test.rb REGION DB_BUCKET DB_KEY'
end

vars = {
# A unique (ish) 6-char string: http://stackoverflow.com/a/88341/483528
:cluster_name => (0...6).map { (65 + rand(26)).chr }.join,
:aws_region => ARGV[0],
:db_remote_state_bucket => ARGV[1],
:db_remote_state_key => ARGV[2],
}
vars_string = vars.map{|key, value| "-var '#{key} = \"#{value}\"'"}.join(', ')

begin
puts "Deploying code in #{Dir.pwd}"
puts `terraform get 2>&1`
puts `terraform apply #{vars_string} 2>&1`

elb_dns_name = `terraform output -no-color elb_dns_name`
url = "http://#{elb_dns_name}/"

retries = 0
loop do
retries += 1
raise "Didn't get expected response after 10 retries" if retries > 10

puts "Checking #{url}"
output = Net::HTTP.get(url)
puts "Output: #{output}"

if output.include? 'Hello, World'
puts 'Success!'
break
end

puts 'Sleeping for 30 seconds and trying again'
sleep(30.seconds)
end
ensure
puts "Undeploying code in #{Dir.pwd}"
puts `terraform destroy -force #{vars_string} 2>&1`
end
8 changes: 8 additions & 0 deletions code/terraform/00-preface/hello-world/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
ami = "ami-40d28157"
instance_type = "t2.micro"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
count = 10
ami = "ami-40d28157"
instance_type = "t2.micro"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
count = 15
ami = "ami-40d28157"
instance_type = "t2.micro"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
count = 15
ami = "ami-408c7f28"
instance_type = "t2.micro"
}
20 changes: 20 additions & 0 deletions code/terraform/01-why-terraform/multi-provider-example/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
provider "aws" {
region = "us-east-1"
}

provider "dnsimple" {
token = "fake-token"
email = "fake-email"
}

resource "aws_instance" "example" {
ami = "ami-40d28157"
instance_type = "t2.micro"
}

resource "dnsimple_record" "example" {
domain = "example.com"
name = "test"
value = "${aws_instance.example.public_ip}"
type = "A"
}
36 changes: 36 additions & 0 deletions code/terraform/01-why-terraform/server-db-elb/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "app" {
instance_type = "t2.micro"
availability_zone = "us-east-1a"
ami = "ami-40d28157"

user_data = <<-EOF
#!/bin/bash
sudo service apache2 start
EOF
}

resource "aws_db_instance" "db" {
allocated_storage = 10
engine = "mysql"
instance_class = "db.t2.micro"
name = "mydb"
username = "admin"
password = "password"
}

resource "aws_elb" "load_balancer" {
name = "frontend-load-balancer"
instances = ["${aws_instance.app.id}"]
availability_zones = ["us-east-1a"]

listener {
instance_port = 8000
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
12 changes: 12 additions & 0 deletions code/terraform/02-intro-to-terraform-syntax/one-server/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
ami = "ami-40d28157"
instance_type = "t2.micro"

tags {
name = "terraform-example"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
ami = "ami-40d28157"
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.instance.id}"]

user_data = <<EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p "${var.server_port}" &
EOF

tags {
Name = "terraform-example"
}
}

resource "aws_security_group" "instance" {
name = "terraform-example-instance"

ingress {
from_port = "${var.server_port}"
to_port = "${var.server_port}"
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "public_ip" {
value = "${aws_instance.example.public_ip}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
variable "server_port" {
description = "The port the server will use for HTTP requests"
default = 8080
}

variable "list_example" {
description = "An example of a list in Terraform"
type = "list"
default = [1, 2, 3]
}

variable "map_example" {
description = "An example of a map in Terraform"
type = "map"

default = {
key1 = "value1"
key2 = "value2"
key3 = "value3"
}
}
34 changes: 34 additions & 0 deletions code/terraform/02-intro-to-terraform-syntax/one-webserver/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {
ami = "ami-40d28157"
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.instance.id}"]

user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF

tags {
Name = "terraform-example"
}
}

resource "aws_security_group" "instance" {
name = "terraform-example-instance"

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}

output "public_ip" {
value = "${aws_instance.example.public_ip}"
}
Loading

0 comments on commit 8fb9223

Please sign in to comment.