Skip to content

Commit

Permalink
examples directory start
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 14, 2014
1 parent 70191d2 commit 2f4e441
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Terraform Examples

This folder contains a set of Terraform examples. These examples each
have their own README you can read for more details on what the example
does.

To run any example, just run `terraform apply` within that directory
if you have Terraform checked out. Or, you can run it directly from git:

```
$ terraform init github.com/hashicorp/terraform/examples/aws-two-tier
...
$ terraform apply
...
```
85 changes: 85 additions & 0 deletions examples/aws-two-tier/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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" {
# 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}"
}

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}"]

# 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"
]
}
}
3 changes: 3 additions & 0 deletions examples/aws-two-tier/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "address" {
value = "${aws_elb.web.dns_name}"
}
22 changes: 22 additions & 0 deletions examples/aws-two-tier/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
variable "key_name" {
description = "Name of the SSH keypair to use in AWS."
}

variable "key_path" {
description = "Path to the private portion of the SSH key specified."
}

variable "aws_region" {
description = "AWS region to launch servers."
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"
}
}

0 comments on commit 2f4e441

Please sign in to comment.