website: update examples to use new module system

This commit is contained in:
Mitchell Hashimoto 2014-10-13 18:07:54 -07:00
parent dfbd47eb05
commit 104ed0fb9b
2 changed files with 34 additions and 114 deletions

View File

@ -6,10 +6,10 @@ sidebar_current: "examples-aws"
# Basic Two-Tier AWS Architecture # Basic Two-Tier AWS Architecture
This provides a template for running a simple two-tier architecture on Amazon [**Example Contents**](https://github.com/hashicorp/terraform/tree/master/examples/aws-two-tier)
Web services.
The premise is that you have stateless app servers running behind This provides a template for running a simple two-tier architecture on Amazon
Web services. The premise is that you have stateless app servers running behind
an ELB serving traffic. an ELB serving traffic.
To simplify the example, this intentionally ignores deploying and To simplify the example, this intentionally ignores deploying and
@ -24,113 +24,3 @@ registers, this should respond with the default nginx web page.
As with all examples, just copy and paste the example and run As with all examples, just copy and paste the example and run
`terraform apply` to see it work. `terraform apply` to see it work.
## Configuration
```
variable "key_name" {}
variable "key_path" {}
variable "aws_region" {
default = "us-west-2"
}
# Ubuntu Precise 12.04 LTS (x64)
variable "aws_amis" {
default = {
eu-west-1 = "ami-b1cf19c6"
us-east-1 = "ami-de7ab6b6"
us-west-1 = "ami-3f75767a"
us-west-2 = "ami-21f78e11"
}
}
# Specify the provider and access details
provider "aws" {
region = "${var.aws_region}"
}
# Our default security group to access
# the instances over SSH and HTTP
resource "aws_security_group" "default" {
name = "terraform_example"
description = "Used in the terraform"
# SSH access from anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP access from anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_elb" "web" {
name = "terraform-example-elb"
# The same availability zone as our instance
availability_zones = ["${aws_instance.web.availability_zone}"]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
# The instance is registered automatically
instances = ["${aws_instance.web.id}"]
}
resource "aws_instance" "web" {
instance_type = "m1.small"
# Lookup the correct AMI based on the region
# we specified
ami = "${lookup(var.aws_amis, var.aws_region)}"
# The name of our SSH keypair you've created and downloaded
# from the AWS console.
#
# https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs:
#
key_name = "${var.key_name}"
# Our Security group to allow HTTP and SSH access
security_groups = ["${aws_security_group.default.name}"]
# The connection block tells our provisioner how to
# communicate with the resource (instance)
connection {
# The default username for our AMI
user = "ubuntu"
# The path to your keyfile
key_file = "${var.key_path}"
}
# We run a remote provisioner on the instance after creating it.
# In this case, we just install nginx and start it. By default,
# this should be on port 80
provisioner "remote-exec" {
inline = [
"sudo apt-get -y update",
"sudo apt-get -y install nginx",
"sudo service nginx start"
]
}
}
output "address" {
value = "${aws_elb.web.dns_name}"
}
```

View File

@ -9,7 +9,7 @@ sidebar_current: "examples"
These examples are designed to help you understand some These examples are designed to help you understand some
of the ways Terraform can be used. of the ways Terraform can be used.
All examples in this section are ready to run as-is. Terraform will All examples are ready to run as-is. Terraform will
ask for input of things such as variables and API keys. If you want to ask for input of things such as variables and API keys. If you want to
conitnue using the example, you should save those parameters in a conitnue using the example, you should save those parameters in a
"terraform.tfvars" file or in a `provider` config bock. "terraform.tfvars" file or in a `provider` config bock.
@ -31,5 +31,35 @@ If you're completely new to Terraform, we recommend reading the
the examples. However, due to the intuitive configuration Terraform the examples. However, due to the intuitive configuration Terraform
uses it isn't required. uses it isn't required.
## Examples
All of the examples are in the
["examples" directory within the Terraform source code](https://github.com/hashicorp/terraform/tree/master/examples). Each example (as well as the examples
directory) has a README explaining the goal of the example.
To use these examples, Terraform must first be installed on your machine. To use these examples, Terraform must first be installed on your machine.
You can install Terraform from the [downloads page](/downloads.html). You can install Terraform from the [downloads page](/downloads.html).
Once installed, you can use two steps to view and run the examples.
To clone any examples, run `terraform init` with the URL to the example:
```
$ terraform init github.com/hashicorp/terraform/examples/aws-two-tier
...
```
This will put the example files within your working directory. You can then
use your own editor to read and browse the configurations. This command will
not _run_ any code.
If you want to run the example, just run `terraform apply`:
```
$ terraform apply
...
```
Terraform will interactively ask for variable input and potentially
provider configuration, and will start executing.
When you're done with the example, run `terraform destroy` to clean up.