Skip to content

Commit

Permalink
Regenerate code with READMEs
Browse files Browse the repository at this point in the history
  • Loading branch information
brikis98 committed Nov 16, 2016
1 parent 185d6a7 commit 3c038ec
Show file tree
Hide file tree
Showing 56 changed files with 2,000 additions and 148 deletions.
17 changes: 17 additions & 0 deletions code/bash/01-why-terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# setup-webserver.sh

This folder contains an example of a Bash script that can be used to install and Apache, PHP, and a sample PHP app
on an Ubuntu server.

For more info, please see Chapter 1, "Why Terraform", of
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.

## Pre-requisites

This script should be run on an Ubuntu server.

## Quick start

```
./setup-webserver.sh
```
18 changes: 18 additions & 0 deletions code/bash/02-intro-to-terraform-syntax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# run-webserver.sh

This folder contains an example of a Bash script that can be used to start a web server that listens on port 8080
and returns the text "Hello, World" for the URL `/`.

For more info, please see Chapter 2, "An Introduction to Terraform Syntax", of
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.

## Pre-requisites

You must have [busybox](https://busybox.net/) installed on your computer.

## Quick start

```
./run-webserver.sh
curl http://localhost:8080
```
19 changes: 19 additions & 0 deletions code/bash/03-terraform-state/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# bash-unit-test-example.sh

This folder shows how extracting your [User Data
script](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html) into a separate file allows you to write
unit tests for that file. The folder contains a sample `user-data.sh` Bash script, which fires up a web server on port
8080, plus a simple unit test for it, `bash-unit-test-example.sh`, that runs that server and checks that it works.

For more info, please see Chapter 3, "How to Manage Terraform State", of
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.

## Pre-requisites

You must have [busybox](https://busybox.net/) installed on your computer.

## Quick start

```
./bash-unit-test-example.sh
```
6 changes: 4 additions & 2 deletions code/bash/03-terraform-state/bash-unit-test-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export server_port=8888

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

if [[ $output != *"Hello, World"* ]]; then
echo "Did not get back expected text 'Hello, World'"
if [[ $output == *"Hello, World"* ]]; then
echo "Success! Got expected text from server."
else
echo "Error. Did not get back expected text 'Hello, World'."
fi
34 changes: 34 additions & 0 deletions code/packer/01-why-terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Packer example

This folder shows an example [Packer](https://www.packer.io/) template that can be used to create an [Amazon Machine
Image (AMI)](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html) of an Ubuntu server with Apache, PHP, and
a sample PHP app installed.

For more info, please see Chapter 1, "Why Terraform", of
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.

## Pre-requisites

* You must have [Packer](https://www.packer.io/) installed on your computer.
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).

## Quick start

**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
charges you may incur.**

Configure your [AWS access
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
environment variables:

```
export AWS_ACCESS_KEY_ID=(your access key id)
export AWS_SECRET_ACCESS_KEY=(your secret access key)
```

To build the AMI:

```
packer build webserver.json
```
48 changes: 48 additions & 0 deletions code/ruby/06-terraform-team/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Terraform automated test example

This folder shows an example of how to write automated tests for a web server cluster defined in
[Terraform](https://www.terraform.io/) templates. The folder contains a Ruby script, `terraform-test.rb`, that will
apply your Terraform templates and then test the web server cluster URL to make sure it returns "Hello, World".

For more info, please see Chapter 6, "How to use Terraform as a Team", of
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.

## Pre-requisites

* You must have [Ruby](https://www.ruby-lang.org/) installed on your computer.
* You must have [Terraform](https://www.terraform.io/) installed on your computer.
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).

## Quick start

**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
charges you may incur.**

Configure your [AWS access
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
environment variables:

```
export AWS_ACCESS_KEY_ID=(your access key id)
export AWS_SECRET_ACCESS_KEY=(your secret access key)
```

Deploy a test database of some sort that the web server cluster can connect to. For example, you could deploy the
Terraform templates under
[code/terraform/06-terraform-team/live/stage/data-stores/mysql](/code/terraform/06-terraform-team/live/stage/data-stores/mysql).
Make sure to note down the AWS region you're deploying into (e.g. `us-east-1`) as well as the S3 bucket name (e.g.
`my-terraform-state`) and key (e.g. `qa/stage/data-stores/mysql/terraform.tfstate`) you use to store the remote state
data of the database.

To run the Ruby automated test, navigate to a folder that contains the web server cluster templates, such as
[code/terraform/06-terraform-team/live/stage/services/webserver-cluster](/code/terraform/06-terraform-team/live/stage/services/webserver-cluster),
and run the following:

```
ruby \
../../../../../../ruby/06-terraform-team/terraform-test.rb \
us-east-1 \
my-terraform-state \
qa/stage/data-stores/mysql/terraform.tfstate
```
37 changes: 18 additions & 19 deletions code/ruby/06-terraform-team/terraform-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,30 @@
}
vars_string = vars.map{|key, value| "-var '#{key} = \"#{value}\"'"}.join(', ')

def test_url(url, expected_text, retries)
retries.times do
begin
output = Net::HTTP.get(URI.parse(url))
puts "Output from #{url}: #{output}"
return 'Success!' if output.include? expected_text
rescue => e
puts "Error from #{url}: #{e}"
end

puts 'Sleeping for 30 seconds and trying again'
sleep 30
end

raise "Response didn't contain '#{expected_text}' after #{retries} retries"
end

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.strip}/"

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
puts test_url("http://#{elb_dns_name.strip}/", 'Hello, World', 10)
ensure
puts "Undeploying code in #{Dir.pwd}"
puts `terraform destroy -force #{vars_string} 2>&1`
Expand Down
46 changes: 46 additions & 0 deletions code/terraform/00-preface/hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Terraform "Hello, World" example

This folder contains a "Hello, World" example of a [Terraform](https://www.terraform.io/) template. The template
deploys a single server in an [Amazon Web Services (AWS) account](http://aws.amazon.com/).

For more info, please see the preface of *[Terraform: Up and Running](http://www.terraformupandrunning.com)*.

## Pre-requisites

* You must have [Terraform](https://www.terraform.io/) installed on your computer.
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).

Please note that this code was written for Terraform {{ terraform_version }}.

## Quick start

**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
charges you may incur.**

Configure your [AWS access
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
environment variables:

```
export AWS_ACCESS_KEY_ID=(your access key id)
export AWS_SECRET_ACCESS_KEY=(your secret access key)
```

Validate the templates:

```
terraform plan
```

Deploy the code:

```
terraform apply
```

Clean up when you're done:

```
terraform destroy
```

This file was deleted.

This file was deleted.

This file was deleted.

20 changes: 0 additions & 20 deletions code/terraform/01-why-terraform/multi-provider-example/main.tf

This file was deleted.

49 changes: 49 additions & 0 deletions code/terraform/01-why-terraform/server-db-elb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Server, DB, ELB Example

This folder contains an example [Terraform](https://www.terraform.io/) template that deploys a server (using
[EC2](https://aws.amazon.com/ec2/)), database (using [RDS](https://aws.amazon.com/rds/)), and
load balancer (using [ELB](https://aws.amazon.com/elasticloadbalancing/)) in an [Amazon Web Services (AWS)
account](http://aws.amazon.com/).

For more info, please see Chapter 1, "Why Terraform", of
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.

## Pre-requisites

* You must have [Terraform](https://www.terraform.io/) installed on your computer.
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).

Please note that this code was written for Terraform {{ terraform_version }}.

## Quick start

**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
charges you may incur.**

Configure your [AWS access
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
environment variables:

```
export AWS_ACCESS_KEY_ID=(your access key id)
export AWS_SECRET_ACCESS_KEY=(your secret access key)
```

Validate the templates:

```
terraform plan
```

Deploy the code:

```
terraform apply
```

Clean up when you're done:

```
terraform destroy
```
Loading

0 comments on commit 3c038ec

Please sign in to comment.