Skip to content

Commit 95a8dd5

Browse files
committed
Refactor to individual files. Make independent of other AWS infra.
1 parent 70cba1c commit 95a8dd5

File tree

7 files changed

+141
-61
lines changed

7 files changed

+141
-61
lines changed

main.tf

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,6 @@ terraform {
22
required_version = ">= 0.11.0"
33
}
44

5-
variable "access_key" {
6-
description = "The AWS access key used to provision resources"
7-
}
8-
9-
variable "secret_key" {
10-
description = "The AWS secret key used to provision resources"
11-
}
12-
13-
variable "security_group_id" {
14-
description = "The security group with ingress and egress rules that EC2 instances will be created within."
15-
}
16-
17-
variable "region" {
18-
description = "The AWS region in which to provision resources"
19-
default = "us-west-2"
20-
}
21-
22-
variable "identity" {
23-
description = "A unique name for your resources"
24-
}
25-
26-
variable "ami" {
27-
description = "The Amazon Machine Image for new instances."
28-
}
29-
30-
variable "num_webs" {
31-
description = "The number of servers to run"
32-
default = "1"
33-
}
34-
355
provider "aws" {
366
version = "~> 1.5"
377
access_key = "${var.access_key}"
@@ -42,16 +12,10 @@ provider "aws" {
4212
module "server" {
4313
source = "./server"
4414

45-
num_webs = "${var.num_webs}"
46-
identity = "${var.identity}"
47-
security_group_id = "${var.security_group_id}"
48-
ami = "${var.ami}"
49-
}
50-
51-
output "public_ip" {
52-
value = "${module.server.public_ip}"
53-
}
54-
55-
output "public_dns" {
56-
value = "${module.server.public_dns}"
15+
num_webs = "${var.num_webs}"
16+
identity = "${var.identity}"
17+
ami = "${var.ami}"
18+
ingress_cidr = "${var.ingress_cidr}"
19+
public_key_path = "${var.public_key_path}"
20+
private_key_path = "${var.private_key_path}"
5721
}

outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
output "public_ip" {
3+
value = "${module.server.public_ip}"
4+
}
5+
6+
output "public_dns" {
7+
value = "${module.server.public_dns}"
8+
}

server/main.tf

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,66 @@
1-
variable "ami" {
2-
description = "Base machine image for running this server"
3-
default = "ami-e70db29f"
1+
resource "aws_key_pair" "default" {
2+
key_name = "${var.identity}-key"
3+
public_key = "${file("${var.public_key_path}")}"
44
}
55

6-
variable "num_webs" {
7-
description = "The number of servers to create"
8-
default = 1
9-
}
6+
resource "aws_security_group" "default" {
7+
name_prefix = "${var.identity}"
108

11-
variable "identity" {
12-
description = "A unique name for this server"
13-
}
9+
ingress {
10+
from_port = 80
11+
to_port = 80
12+
protocol = "tcp"
13+
cidr_blocks = ["${var.ingress_cidr}"]
14+
}
15+
16+
ingress {
17+
from_port = 22
18+
to_port = 22
19+
protocol = "tcp"
20+
cidr_blocks = ["${var.ingress_cidr}"]
21+
}
1422

15-
variable "security_group_id" {
16-
description = "The AWS security group with ingress and egress rules for this instance."
23+
egress {
24+
from_port = 0
25+
to_port = 0
26+
protocol = "-1"
27+
cidr_blocks = ["0.0.0.0/0"]
28+
}
29+
30+
tags {
31+
"Created-by" = "Terraform"
32+
"Identity" = "${var.identity}"
33+
}
1734
}
1835

1936
resource "aws_instance" "web" {
2037
ami = "${var.ami}"
2138
instance_type = "t2.medium"
2239
count = "${var.num_webs}"
2340

24-
vpc_security_group_ids = ["${var.security_group_id}"]
41+
vpc_security_group_ids = ["${aws_security_group.default.id}"]
42+
43+
key_name = "${aws_key_pair.default.id}"
2544

2645
tags {
2746
"Name" = "${var.identity} web ${count.index+1}/${var.num_webs}"
2847
"Identity" = "${var.identity}"
2948
"Created-by" = "Terraform"
3049
}
31-
}
3250

33-
output "public_ip" {
34-
value = ["${aws_instance.web.*.public_ip}"]
35-
}
51+
connection {
52+
user = "ubuntu"
53+
private_key = "${file("${var.private_key_path}")}"
54+
}
55+
56+
provisioner "file" {
57+
source = "assets"
58+
destination = "/tmp/"
59+
}
3660

37-
output "public_dns" {
38-
value = ["${aws_instance.web.*.public_dns}"]
61+
provisioner "remote-exec" {
62+
inline = [
63+
"sudo sh /tmp/assets/setup-web.sh",
64+
]
65+
}
3966
}

server/outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
output "public_ip" {
3+
value = ["${aws_instance.web.*.public_ip}"]
4+
}
5+
6+
output "public_dns" {
7+
value = ["${aws_instance.web.*.public_dns}"]
8+
}

server/variables.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
variable "ami" {
3+
description = "Base machine image for running this server"
4+
}
5+
6+
variable "num_webs" {
7+
description = "The number of servers to create"
8+
default = 1
9+
}
10+
11+
variable "identity" {
12+
description = "A unique name for this server"
13+
}
14+
15+
variable "ingress_cidr" {
16+
description = "IP address block from which connections to this instance will be made"
17+
}
18+
19+
variable "public_key_path" {
20+
description = "Path on disk to the public key used to connect to this instance"
21+
}
22+
23+
variable "private_key_path" {
24+
description = "Path on disk to the private key used to connect to this instance"
25+
}
26+

terraform.tfvars.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
access_key=""
2+
secret_key=""
3+
identity="demo-wallaby"
4+
region="us-west-2"
5+
ingress_cidr="0.0.0.0/0"

variables.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
variable "access_key" {
3+
description = "The AWS access key used to provision resources"
4+
}
5+
6+
variable "secret_key" {
7+
description = "The AWS secret key used to provision resources"
8+
}
9+
10+
variable "region" {
11+
description = "The AWS region in which to provision resources"
12+
default = "us-west-2"
13+
}
14+
15+
variable "identity" {
16+
description = "A unique name for your resources"
17+
}
18+
19+
variable "ami" {
20+
description = "The Amazon Machine Image for new instances."
21+
default = "ami-c62eaabe"
22+
}
23+
24+
variable "ingress_cidr" {
25+
default = "0.0.0.0/0"
26+
description = "IP block from which connections to this instance will be made"
27+
}
28+
29+
variable "public_key_path" {
30+
description = "Path on disk to the public key used to connect to this instance"
31+
default = "~/.ssh/id_rsa.pub"
32+
}
33+
34+
variable "private_key_path" {
35+
description = "Path on disk to the private key used to connect to this instance"
36+
default = "~/.ssh/id_rsa"
37+
}
38+
39+
variable "num_webs" {
40+
description = "The number of servers to run"
41+
default = "1"
42+
}

0 commit comments

Comments
 (0)