Skip to content

Commit 3c038ec

Browse files
committed
Regenerate code with READMEs
1 parent 185d6a7 commit 3c038ec

File tree

56 files changed

+2000
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2000
-148
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# setup-webserver.sh
2+
3+
This folder contains an example of a Bash script that can be used to install and Apache, PHP, and a sample PHP app
4+
on an Ubuntu server.
5+
6+
For more info, please see Chapter 1, "Why Terraform", of
7+
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.
8+
9+
## Pre-requisites
10+
11+
This script should be run on an Ubuntu server.
12+
13+
## Quick start
14+
15+
```
16+
./setup-webserver.sh
17+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# run-webserver.sh
2+
3+
This folder contains an example of a Bash script that can be used to start a web server that listens on port 8080
4+
and returns the text "Hello, World" for the URL `/`.
5+
6+
For more info, please see Chapter 2, "An Introduction to Terraform Syntax", of
7+
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.
8+
9+
## Pre-requisites
10+
11+
You must have [busybox](https://busybox.net/) installed on your computer.
12+
13+
## Quick start
14+
15+
```
16+
./run-webserver.sh
17+
curl http://localhost:8080
18+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# bash-unit-test-example.sh
2+
3+
This folder shows how extracting your [User Data
4+
script](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html) into a separate file allows you to write
5+
unit tests for that file. The folder contains a sample `user-data.sh` Bash script, which fires up a web server on port
6+
8080, plus a simple unit test for it, `bash-unit-test-example.sh`, that runs that server and checks that it works.
7+
8+
For more info, please see Chapter 3, "How to Manage Terraform State", of
9+
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.
10+
11+
## Pre-requisites
12+
13+
You must have [busybox](https://busybox.net/) installed on your computer.
14+
15+
## Quick start
16+
17+
```
18+
./bash-unit-test-example.sh
19+
```

code/bash/03-terraform-state/bash-unit-test-example.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export server_port=8888
88

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

11-
if [[ $output != *"Hello, World"* ]]; then
12-
echo "Did not get back expected text 'Hello, World'"
11+
if [[ $output == *"Hello, World"* ]]; then
12+
echo "Success! Got expected text from server."
13+
else
14+
echo "Error. Did not get back expected text 'Hello, World'."
1315
fi
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Packer example
2+
3+
This folder shows an example [Packer](https://www.packer.io/) template that can be used to create an [Amazon Machine
4+
Image (AMI)](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html) of an Ubuntu server with Apache, PHP, and
5+
a sample PHP app installed.
6+
7+
For more info, please see Chapter 1, "Why Terraform", of
8+
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.
9+
10+
## Pre-requisites
11+
12+
* You must have [Packer](https://www.packer.io/) installed on your computer.
13+
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).
14+
15+
## Quick start
16+
17+
**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
18+
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
19+
charges you may incur.**
20+
21+
Configure your [AWS access
22+
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
23+
environment variables:
24+
25+
```
26+
export AWS_ACCESS_KEY_ID=(your access key id)
27+
export AWS_SECRET_ACCESS_KEY=(your secret access key)
28+
```
29+
30+
To build the AMI:
31+
32+
```
33+
packer build webserver.json
34+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Terraform automated test example
2+
3+
This folder shows an example of how to write automated tests for a web server cluster defined in
4+
[Terraform](https://www.terraform.io/) templates. The folder contains a Ruby script, `terraform-test.rb`, that will
5+
apply your Terraform templates and then test the web server cluster URL to make sure it returns "Hello, World".
6+
7+
For more info, please see Chapter 6, "How to use Terraform as a Team", of
8+
*[Terraform: Up and Running](http://www.terraformupandrunning.com)*.
9+
10+
## Pre-requisites
11+
12+
* You must have [Ruby](https://www.ruby-lang.org/) installed on your computer.
13+
* You must have [Terraform](https://www.terraform.io/) installed on your computer.
14+
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).
15+
16+
## Quick start
17+
18+
**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
19+
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
20+
charges you may incur.**
21+
22+
Configure your [AWS access
23+
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
24+
environment variables:
25+
26+
```
27+
export AWS_ACCESS_KEY_ID=(your access key id)
28+
export AWS_SECRET_ACCESS_KEY=(your secret access key)
29+
```
30+
31+
Deploy a test database of some sort that the web server cluster can connect to. For example, you could deploy the
32+
Terraform templates under
33+
[code/terraform/06-terraform-team/live/stage/data-stores/mysql](/code/terraform/06-terraform-team/live/stage/data-stores/mysql).
34+
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.
35+
`my-terraform-state`) and key (e.g. `qa/stage/data-stores/mysql/terraform.tfstate`) you use to store the remote state
36+
data of the database.
37+
38+
To run the Ruby automated test, navigate to a folder that contains the web server cluster templates, such as
39+
[code/terraform/06-terraform-team/live/stage/services/webserver-cluster](/code/terraform/06-terraform-team/live/stage/services/webserver-cluster),
40+
and run the following:
41+
42+
```
43+
ruby \
44+
../../../../../../ruby/06-terraform-team/terraform-test.rb \
45+
us-east-1 \
46+
my-terraform-state \
47+
qa/stage/data-stores/mysql/terraform.tfstate
48+
```

code/ruby/06-terraform-team/terraform-test.rb

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,30 @@
1313
}
1414
vars_string = vars.map{|key, value| "-var '#{key} = \"#{value}\"'"}.join(', ')
1515

16+
def test_url(url, expected_text, retries)
17+
retries.times do
18+
begin
19+
output = Net::HTTP.get(URI.parse(url))
20+
puts "Output from #{url}: #{output}"
21+
return 'Success!' if output.include? expected_text
22+
rescue => e
23+
puts "Error from #{url}: #{e}"
24+
end
25+
26+
puts 'Sleeping for 30 seconds and trying again'
27+
sleep 30
28+
end
29+
30+
raise "Response didn't contain '#{expected_text}' after #{retries} retries"
31+
end
32+
1633
begin
1734
puts "Deploying code in #{Dir.pwd}"
1835
puts `terraform get 2>&1`
1936
puts `terraform apply #{vars_string} 2>&1`
2037

2138
elb_dns_name = `terraform output -no-color elb_dns_name`
22-
url = "http://#{elb_dns_name.strip}/"
23-
24-
retries = 0
25-
loop do
26-
retries += 1
27-
raise "Didn't get expected response after 10 retries" if retries > 10
28-
29-
puts "Checking #{url}"
30-
output = Net::HTTP.get(url)
31-
puts "Output: #{output}"
32-
33-
if output.include? 'Hello, World'
34-
puts 'Success!'
35-
break
36-
end
37-
38-
puts 'Sleeping for 30 seconds and trying again'
39-
sleep(30.seconds)
40-
end
39+
puts test_url("http://#{elb_dns_name.strip}/", 'Hello, World', 10)
4140
ensure
4241
puts "Undeploying code in #{Dir.pwd}"
4342
puts `terraform destroy -force #{vars_string} 2>&1`
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Terraform "Hello, World" example
2+
3+
This folder contains a "Hello, World" example of a [Terraform](https://www.terraform.io/) template. The template
4+
deploys a single server in an [Amazon Web Services (AWS) account](http://aws.amazon.com/).
5+
6+
For more info, please see the preface of *[Terraform: Up and Running](http://www.terraformupandrunning.com)*.
7+
8+
## Pre-requisites
9+
10+
* You must have [Terraform](https://www.terraform.io/) installed on your computer.
11+
* You must have an [Amazon Web Services (AWS) account](http://aws.amazon.com/).
12+
13+
Please note that this code was written for Terraform {{ terraform_version }}.
14+
15+
## Quick start
16+
17+
**Please note that this example will deploy real resources into your AWS account. We have made every effort to ensure
18+
all the resources qualify for the [AWS Free Tier](https://aws.amazon.com/free/), but we are not responsible for any
19+
charges you may incur.**
20+
21+
Configure your [AWS access
22+
keys](http://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys) as
23+
environment variables:
24+
25+
```
26+
export AWS_ACCESS_KEY_ID=(your access key id)
27+
export AWS_SECRET_ACCESS_KEY=(your secret access key)
28+
```
29+
30+
Validate the templates:
31+
32+
```
33+
terraform plan
34+
```
35+
36+
Deploy the code:
37+
38+
```
39+
terraform apply
40+
```
41+
42+
Clean up when you're done:
43+
44+
```
45+
terraform destroy
46+
```

code/terraform/01-why-terraform/declarative-examples/part-1/main.tf

Lines changed: 0 additions & 9 deletions
This file was deleted.

code/terraform/01-why-terraform/declarative-examples/part-2/main.tf

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)