-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathMayank_Singh_Rawat-849363.tf.txt
106 lines (83 loc) · 2.16 KB
/
Mayank_Singh_Rawat-849363.tf.txt
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
/* Aim: Create a template for running a simple two-tier architecture on Amazon Web services.
The premise is that you have stateless app servers running behind an ELB serving traffic. */
--------------------------------------
#provider.tf
provider "aws"{
access_key="${var.access_key}"
secret_key="${var.secret_key}"
region="${var.region}"
}
--------------------------------------
#resource.tf
resource "aws_elb" "my-elb" {
name = "my-elb"
instances = ["${instance_1}", "${instance_2}"]
security_groups = ["${aws_security_group.elb-securitygroup.id}"]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
resource "aws_key_pair" "aws_key"{
key_name="${var.key_pair_name}"
public_key="${file("${var.key_pair_path}")}"
}
#run the below command to generate a key-pair
$ssh-keygen -t rsa -b 4096 -f aws_rsa_keyfile
resource "aws_instance" "myec2_server" {
ami="${var.ami}"
instance_type="${var.instancetype}"
key_name = "${aws_key_pair.aws_key.my_name}"
aws_security_group="myec2_server_securitygroup"
count=2;
}
resource "aws_security_group" "myec2_server_securitygroup" {
name = "web server security group"
ingress {
from_port = 0
to_port = 80
protocol = "http"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
-----------------------------
#provisioner.tf
provisioner "remote-exec"{
connection{
type="ssh"
user="ec2-user"
private_key="${file("/path_to/aws_rsa_keyfile)}"
}
}
provisioner "remote-exec"{
inline{
#commands
"sudo yum -y update"
"sudo yum install -y httpd",
"sudo service httpd start"
}
}
----------------------------
#Defining the variables
#variables.tf
variable "my_map"{
type="map"
default{
"ami"="machine image you want to create"
"instance_type"="t2.micro or any other type"
"key_pair_name"="my_key_pair"
"key_pair_path"="my_key_pair_path"
"access_key"="xxx"
"secret_key"="xxx"
"region"="us-east-1"
}
description=" variables to be used"
}