Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix syntax typo #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.terraform/
terraform.tfvars
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2016 James Turnbull

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# AWS Web service module for Terraform

A lightweight Web service module for The Terraform Book.

## Usage

```hcl
variable "cloudflare_email" {
description = "The Cloudflare email of your account"
}

variable "cloudflare_token" {
description = "The Cloudflare token"
}

variable "domain" {
default = "turnbullpublishing.com"
description = "The domain of our web service."
}

variable "web_instance_count" {
default = 1
description = "The number of Web instances to create"
}

variable "app_instance_count" {
default = 1
description = "The number of App instances to create"
}

provider "cloudflare" {
email = "${var.cloudflare_email}"
token = "${var.cloudflare_token}"
}

module "web" {
source = "github.com/turnbullpublishing/tf_web"
environment = "${var.environment}"
vpc_id = "${module.vpc.vpc_id}"
public_subnet_ids = "${module.vpc.public_subnet_ids}"
private_subnet_ids = "${module.vpc.private_subnet_ids}"
web_instance_counts = "${var.web_instance_counts}"
app_instance_counts = "${var.app_instance_counts}"
domain = "${var.domain}"
region = "${var.region}"
key_name = "${var.key_name}"
}

output "web_elb_address" {
value = "${module.web.web_elb_address}"
}

output "web_host_addresses" {
value = ["${module.web.web_host_addresses}"]
}

output "app_host_addresses" {
value = ["${module.web.app_host_addresses}"]
}
```

Assumes you're building your Web service inside a VPC created from [this
module](https://github.com/turnbullpublishing/tf_vpc).

See `interface.tf` for additional configurable variables.

## License

MIT

4 changes: 4 additions & 0 deletions files/app_bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
sudo apt-get update
sudo apt-get install -y nginx
sudo service nginx start
17 changes: 17 additions & 0 deletions files/web_bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
sudo apt-get update
sudo apt-get install -y nginx

# Added index.html
sudo cat >/var/www/html/index.html << "EOF"
<html>
<head>
<title>Web service</title>
</head>
<body>
<h1>The Terraform Book Web service</h1>
</body>
</html>
EOF

sudo service nginx start
65 changes: 65 additions & 0 deletions interface.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
variable "region" {
description = "The AWS region."
}

variable "environment" {
description = "The name of our environment, i.e. development."
}

variable "key_name" {
description = "The AWS key pair to use for resources."
}

variable "public_subnet_ids" {
default = []
description = "The list of public subnets to populate."
}

variable "private_subnet_ids" {
default = []
description = "The list of private subnets to populate."
}

variable "ami" {
default = {
"us-east-1" = "ami-f652979b"
"us-west-1" = "ami-7c4b331c"
}

description = "The AMIs to use for web and app instances."
}

variable "instance_type" {
default = "t2.micro"
description = "The instance type to launch "
}

variable "web_instance_count" {
default = 1
description = "The number of Web instances to create"
}

variable "app_instance_count" {
default = 1
description = "The number of App instances to create"
}

variable "vpc_id" {
description = "The VPC ID to launch in"
}

variable "domain" {
description = "The domain to use for the web service"
}

output "web_elb_address" {
value = "${aws_elb.web.dns_name}"
}

output "web_host_addresses" {
value = ["${aws_instance.web.*.private_ip}"]
}

output "app_host_addresses" {
value = ["${aws_instance.app.*.private_ip}"]
}
74 changes: 53 additions & 21 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
data "aws_vpc" "environment" {
id = "${var.vpc_id}"
}

resource "aws_instance" "web" {
ami = "${lookup(var.ami, var.region)}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
subnet_id = "${element(module.vpc.public_subnet_ids, 0)}"
user_data = "${file("files/web_bootstrap.sh")}"
subnet_id = "${var.public_subnet_ids[0]}"
user_data = "${file("${path.module}/files/web_bootstrap.sh")}"

vpc_security_group_ids = [
"${aws_security_group.web_host_sg.id}"
"${aws_security_group.web_host_sg.id}",
]

tags {
Name = "${var.environment}-web-${count.index}"
}
count = 2

count = "${var.web_instance_count}"
}

resource "aws_elb" "web" {
name = "${var.environment}-web-elb"
subnets = ["${element(module.vpc.public_subnet_ids, 0)}"]
security_groups = ["${aws_security_group.web_inbound_sg.id}"]
name = "${var.environment}-web-elb"
subnets = ["${var.public_subnet_ids[0]}"]
security_groups = ["${aws_security_group.web_inbound_sg.id}"]

listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}

instances = ["${aws_instance.web.*.id}"]
}

resource "cloudflare_record" "web" {
domain = "${var.domain}"
name = "${var.environment}.${var.domain}"
value = "${aws_elb.web.dns_name}"
type = "CNAME"
ttl = 3600
}

resource "aws_instance" "app" {
ami = "${lookup(var.ami, var.region)}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
subnet_id = "${element(module.vpc.private_subnet_ids, 0)}"
user_data = "${file("files/app_bootstrap.sh")}"
subnet_id = "${var.private_subnet_ids[0]}"
user_data = "${file("${path.module}/files/app_bootstrap.sh")}"

vpc_security_group_ids = [
"${aws_security_group.app_host_sg.id}",
]

tags {
Name = "${var.environment}-app-${count.index}"
}
count = 2

count = "${var.app_instance_count}"
}

resource "aws_security_group" "web_inbound_sg" {
name = "${var.environment}-web_inbound"
name = "${var.environment}-web-inbound"
description = "Allow HTTP from Anywhere"
vpc_id = "${module.vpc.vpc_id}"
vpc_id = "${data.aws_vpc.environment.id}"

ingress {
from_port = 80
Expand All @@ -66,26 +86,30 @@ resource "aws_security_group" "web_inbound_sg" {
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags {
Name = "${var.environment}-web-inbound-sg"
}
}

resource "aws_security_group" "web_host_sg" {
name = "${var.environment}-web_host"
description = "Allow SSH & HTTP to web hosts"
vpc_id = "${module.vpc.vpc_id}"
name = "${var.environment}-web-host"
description = "Allow SSH and HTTP to web hosts"
vpc_id = "${data.aws_vpc.environment.id}"

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${module.vpc.vpc_cidr}"]
cidr_blocks = ["${data.aws_vpc.environment.cidr_block}"]
}

# HTTP access from the VPC
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["${module.vpc.vpc_cidr}"]
cidr_blocks = ["${data.aws_vpc.environment.cidr_block}"]
}

egress {
Expand All @@ -101,27 +125,31 @@ resource "aws_security_group" "web_host_sg" {
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}

tags {
Name = "${var.environment}-web-host-sg"
}
}

resource "aws_security_group" "app_host_sg" {
name = "${var.environment}-app_host"
name = "${var.environment}-app-host"
description = "Allow App traffic to app hosts"
vpc_id = "${module.vpc.vpc_id}"
vpc_id = "${data.aws_vpc.environment.id}"

# App access from the VPC
ingress {
from_port = 1234
to_port = 1234
protocol = "tcp"
cidr_blocks = ["${module.vpc.vpc_cidr}"]
cidr_blocks = ["${data.aws_vpc.environment.cidr_block}"]
}

# SSH access from the VPC
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${module.vpc.vpc_cidr}"]
cidr_blocks = ["${data.aws_vpc.environment.cidr_block}"]
}

egress {
Expand All @@ -137,4 +165,8 @@ resource "aws_security_group" "app_host_sg" {
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}

tags {
Name = "${var.environment}-app-host-sg"
}
}
11 changes: 0 additions & 11 deletions outputs.tf

This file was deleted.

12 changes: 0 additions & 12 deletions variables.tf

This file was deleted.