Skip to content

Commit

Permalink
Merge pull request #36 from brikis98/2nd-edition
Browse files Browse the repository at this point in the history
2nd edition
  • Loading branch information
brikis98 authored Aug 30, 2019
2 parents 0f67c39 + 740c996 commit daa73fe
Show file tree
Hide file tree
Showing 356 changed files with 7,942 additions and 1,743 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This repo contains the code samples for the book *[Terraform: Up and Running](ht



## IMPORTANT NOTE ON 2ND EDITION

The 2nd edition of *Terraform: Up & Running* will be available shortly! If you're reading an early copy of the new edition of the book, please head over to the [2nd-edition branch](https://github.com/brikis98/terraform-up-and-running-code/tree/2nd-edition), which has all the code updated to Terraform 0.12, tons of new code for the new chapters in the book, and so on. It's a work in progress, so think of this branch as a preview of what things will look like when all the updates are finished.


## IMPORTANT NOTE ON 2ND EDITION

Expand Down
6 changes: 6 additions & 0 deletions code/ansible/01-why-terraform/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[webservers]
11.11.11.11
11.11.11.12
11.11.11.13
11.11.11.14
11.11.11.15
3 changes: 3 additions & 0 deletions code/ansible/01-why-terraform/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- hosts: webservers
roles:
- webserver
9 changes: 9 additions & 0 deletions code/ansible/01-why-terraform/procedural-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- ec2:
count: 10
image: ami-0c55b159cbfafe1f0
instance_type: t2.micro

- ec2:
count: 5
image: ami-0c55b159cbfafe1f0
instance_type: t2.micro
17 changes: 17 additions & 0 deletions code/ansible/01-why-terraform/webserver.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- name: Update the apt-get cache
apt:
update_cache: yes

- name: Install PHP
apt:
name: php

- name: Install Apache
apt:
name: apache2

- name: Copy the code from the repository
git: repo=https://github.com/brikis98/php-app.git dest=/var/www/html/app

- name: Start Apache
service: name=apache2 state=started enabled=yes
7 changes: 2 additions & 5 deletions code/bash/01-why-terraform/setup-webserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ 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
# Install PHP and Apache
sudo apt-get install -y php apache2

# Copy the code from the repository
sudo git clone https://github.com/brikis98/php-app.git /var/www/html/app
Expand Down
2 changes: 1 addition & 1 deletion code/bash/03-terraform-state/user-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cat > index.html <<EOF
<p>DB port: ${db_port}</p>
EOF

nohup busybox httpd -f -p "${server_port}" &
nohup busybox httpd -f -p ${server_port} &
10 changes: 6 additions & 4 deletions code/packer/01-why-terraform/webserver.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
"builders": [{
"ami_name": "packer-example",
"instance_type": "t2.micro",
"region": "us-east-1",
"region": "us-east-2",
"type": "amazon-ebs",
"source_ami": "ami-40d28157",
"source_ami": "ami-0c55b159cbfafe1f0",
"ssh_username": "ubuntu"
}],
"provisioners": [{
"type": "shell",
"inline": [
"sudo apt-get update",
"sudo apt-get install -y php",
"sudo apt-get install -y apache2",
"sudo apt-get install -y php apache2",
"sudo git clone https://github.com/brikis98/php-app.git /var/www/html/app"
],
"environment_vars": [
"DEBIAN_FRONTEND=noninteractive"
]
}]
}
6 changes: 6 additions & 0 deletions code/ruby/04-terraform-modules/function-example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def example_function()
puts "Hello, World"
end

# Other places in your code
example_function()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def example_function(param1, param2)
return "Hello, #{param1} #{param2}"
end

# Other places in your code
return_value = example_function("foo", "bar")
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def example_function(param1, param2)
puts "Hello, #{param1} #{param2}"
end

# Other places in your code
example_function("foo", "bar")
48 changes: 0 additions & 48 deletions code/ruby/06-terraform-team/README.md

This file was deleted.

43 changes: 0 additions & 43 deletions code/ruby/06-terraform-team/terraform-test.rb

This file was deleted.

49 changes: 49 additions & 0 deletions code/ruby/07-testing-terraform-code/web-server-basic-test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative "web-server-basic"
require "test/unit"
require 'webrick'
require 'net/http'

class TestWebServer < Test::Unit::TestCase
def test_integration_hello
do_integration_test('/', lambda { |response|
assert_equal(200, response.code.to_i)
assert_equal('text/plain', response['Content-Type'])
assert_equal('Hello, World', response.body)
})
end

def test_integration_api
do_integration_test('/api', lambda { |response|
assert_equal(201, response.code.to_i)
assert_equal('application/json', response['Content-Type'])
assert_equal('{"foo":"bar"}', response.body)
})
end

def test_integration_404
do_integration_test('/invalid-path', lambda { |response|
assert_equal(404, response.code.to_i)
assert_equal('text/plain', response['Content-Type'])
assert_equal('Not Found', response.body)
})
end

def do_integration_test(path, check_response)
port = 8002
server = WEBrick::HTTPServer.new :Port => port
server.mount '/', WebServer

begin
thread = Thread.new do
server.start
end

uri = URI("http://localhost:#{port}#{path}")
response = Net::HTTP.get_response(uri)
check_response.call(response)
ensure
server.shutdown
thread.join
end
end
end
34 changes: 34 additions & 0 deletions code/ruby/07-testing-terraform-code/web-server-basic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'webrick'

class WebServer < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
case request.path
when "/"
response.status = 200
response['Content-Type'] = 'text/plain'
response.body = 'Hello, World'
when "/api"
response.status = 201
response['Content-Type'] = 'application/json'
response.body = '{"foo":"bar"}'
else
response.status = 404
response['Content-Type'] = 'text/plain'
response.body = 'Not Found'
end
end
end

# This will only run if this script was called directly from the CLI, but
# not if it was required from another file
if __FILE__ == $0
# Run the server on localhost at port 8000
server = WEBrick::HTTPServer.new :Port => 8000
server.mount '/', WebServer

# Shut down the server on CTRL+C
trap 'INT' do server.shutdown end

# Start the server
server.start
end
Loading

0 comments on commit daa73fe

Please sign in to comment.