-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q1_5_instances.tf
138 lines (117 loc) · 2.91 KB
/
Q1_5_instances.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# TF file in answer to Q1
#
# Please note the following assumptions
# - a VPC is already created with a subnet
# - the subnet has a route to an IG to allow external access
#
# Please note the following is not included as part of the answer
# - some of the EC2 setting including EBS volume and encryption/ssh key
# - the variables file
# - the outputs file
# - the userdata section of the EC2 instance
#
## create an instance with external IP and dns record
provider "aws" {
region = "eu-west-2"
}
/*
resource "aws_instance" "houly_ec2" {
ami = "${var.ami_id}"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.houly_subnet.id}"
tags = {
Name = "houly ec2 isntance"
Environment = "Dev"
}
}
}
resource "aws_eip" "houly_eip" {
vpc = true
instance = "${aws_instance.houly_ec2.id}"
depends_on = ["aws_internet_gateway.gw"]
}
resource "aws_route53_record" "houly_dns" {
zone_id = "${var.houly_zone_id}"
name = "www.houly.co.uk"
type = "A"
ttl = "300"
records = ["${aws_eip.houly_eip.public_ip}"]
}
*/
## build 5 Instances
variable "instances" {
type = "map"
default = {
key1 = "Houly_1",
key2 = "Houly_2",
key3 = "Houly_3",
key4 = "Houly_4",
key5 = "Houly_5"
}
}
resource "aws_instance" "houly_ec2" {
for_each = var.instances
ami = "${var.ami_id}"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.houly_subnet.id}"
iam_instance_profile = "${aws_iam_instance_profile.houly_profile.name}"
tags = {
Name = "${each.value}"
Environment = "Dev"
}
resource "aws_route53_record" "houly_dns" {
for_each = var.instances
zone_id = "${var.houly_zone_id}"
name = "www.${each.value}.co.uk"
type = "A"
ttl = "300"
records = "${lookup(each.value, "private_ip", "*")}"
}
resource "aws_iam_role" "houly_s3_role" {
name = "houly_s3_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "houly_s3_full" {
name = "houly-s3-policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:*"
],
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": ["arn:aws:s3:::aws_s3_bucket.houly_s3/*"]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "houly-attach" {
role = "${aws_iam_role.houly_s3_role.id}"
policy_arn = "${aws_iam_policy.houly_s3_full.arn}"
}
resource "aws_iam_instance_profile" "houly_profile" {
name = "houly_profile"
role = "${aws_iam_role.houly_s3_role.name}"
}