-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.tf
105 lines (87 loc) · 3.06 KB
/
aws.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
provider "aws" {
access_key = "${var.aws_keys["access"]}"
secret_key = "${var.aws_keys["secret"]}"
region = "${var.aws_region}"
}
resource "aws_instance" "ansible" {
tags = {
Name = "${var.user_prefix}-ansible"
Group = "mgmt"
}
instance_type = "m1.small"
ami = "${lookup(var.aws_amis, var.aws_region)}"
subnet_id = "${aws_subnet.default.id}"
vpc_security_group_ids = ["${aws_security_group.ansible.id}"]
connection {
# The default username for our AMI
user = "ubuntu"
private_key = "${file("demo.pem")}"
}
key_name = "demo"
# copy playbooks
provisioner "file" {
source = "ansible/"
destination = "/home/ubuntu"
}
# copy SSH key so that Ansible can talk to other hosts
provisioner "file" {
source = "demo.pem"
destination = "/home/ubuntu/demo.pem"
}
# Install Ansible and boto3 for dynamic inventory
provisioner "remote-exec" {
inline = [
"sudo apt-get -y install python-software-properties",
"sudo apt-add-repository -y ppa:ansible/ansible",
"sudo apt-get -y update",
"sudo apt-get -y install ansible python-pip python-dev",
"sudo apt-get -y remove python-boto",
"sudo pip install boto boto3"
]
}
# execute Ansible playbook
provisioner "remote-exec" {
inline = [
"chmod 400 demo.pem",
"sudo chmod +x ec2.py",
"sed -i 's/# aws_access_key.*/aws_access_key_id=${var.aws_keys["access"]}/' ec2.ini",
"sed -i 's/# aws_secret_access.*/aws_secret_access_key=${var.aws_keys["secret"]}/' ec2.ini",
"ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook install.yml -i ec2.py --key-file=demo.pem",
"ansible-playbook site.yml -i ec2.py -v"
]
}
# we want to run Ansible node after all other have been provisioned, so that Ansbile can configure them
depends_on = ["aws_instance.lb", "aws_instance.web"]
}
resource "aws_instance" "lb" {
tags = {
Name = "${var.user_prefix}-lb"
Group = "lb"
}
instance_type = "m1.small"
ami = "${lookup(var.aws_amis, var.aws_region)}"
key_name = "demo"
# Our Security group to allow HTTP and SSH access
vpc_security_group_ids = ["${aws_security_group.lb.id}"]
# We're going to launch into the same subnet as our ELB. In a production
# environment it's more common to have a separate private subnet for
# backend instances.
subnet_id = "${aws_subnet.default.id}"
}
resource "aws_instance" "web" {
tags = {
Name = "${var.user_prefix}-web-${count.index}"
Group = "web"
}
instance_type = "m1.small"
ami = "${lookup(var.aws_amis, var.aws_region)}"
key_name = "demo"
# Our Security group to allow HTTP and SSH access
vpc_security_group_ids = ["${aws_security_group.default.id}"]
# We're going to launch into the same subnet as our ELB. In a production
# environment it's more common to have a separate private subnet for
# backend instances.
subnet_id = "${aws_subnet.default.id}"
# This will create a number of instances idicated below
count = "${var.aws_instances_count}"
}