-
Notifications
You must be signed in to change notification settings - Fork 2
/
vishal.tf
166 lines (135 loc) · 3.55 KB
/
vishal.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//Describing Provider
provider "aws" {
region = "ap-south-1"
profile = "vishal"
}
//Creating Key
resource "tls_private_key" "tls_key" {
algorithm = "RSA"
}
//Generating Key-Value Pair
resource "aws_key_pair" "generated_key" {
key_name = "vishal1-env-key"
public_key = "${tls_private_key.tls_key.public_key_openssh}"
depends_on = [
tls_private_key.tls_key
]
}
//Saving Private Key PEM File
resource "local_file" "key-file" {
content = "${tls_private_key.tls_key.private_key_pem}"
filename = "vishal1-env-key.pem"
depends_on = [
tls_private_key.tls_key
]
}
//Creating Variable for AMI_ID
variable "ami_id" {
type = string
default = "ami-0447a12f28fddb066"
}
//Creating Variable for AMI_Type
variable "ami_type" {
type = string
default = "t2.micro"
}
//Creating Security Group
resource "aws_security_group" "web-SG" {
name = "Terraform-SG"
description = "Web Environment Security Group"
//Adding Rules to Security Group
ingress {
description = "SSH Rule"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP Rule"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
//Creating a S3 Bucket for Terraform Integration
resource "aws_s3_bucket" "vishal-bucket" {
bucket = "vishal-static-data-bucket"
acl = "public-read"
}
//Putting Objects in S3 Bucket
resource "aws_s3_bucket_object" "web-object1" {
bucket = "${aws_s3_bucket.vishal-bucket.bucket}"
key = "vishal.png"
source = "/home/vishal/Desktop/terr/vishal.png"
acl = "public-read"
}
//Launching EC2 Instance
resource "aws_instance" "web" {
ami = "${var.ami_id}"
instance_type = "${var.ami_type}"
key_name = "${aws_key_pair.generated_key.key_name}"
security_groups = ["${aws_security_group.web-SG.name}","default"]
//Labelling the Instance
tags = {
Name = "Web-Env"
env = "Production"
}
depends_on = [
aws_security_group.web-SG,
aws_key_pair.generated_key
]
}
resource "null_resource" "remote1" {
depends_on = [ aws_instance.web, ]
//Executing Commands to initiate WebServer in Instance Over SSH
provisioner "remote-exec" {
connection {
agent = "false"
type = "ssh"
user = "ec2-user"
private_key = "${tls_private_key.tls_key.private_key_pem}"
host = "${aws_instance.web.public_ip}"
}
inline = [
"sudo yum install httpd git -y",
"sudo systemctl start httpd",
"sudo systemctl enable httpd",
]
}
}
//Creating EBS Volume
resource "aws_ebs_volume" "web-vol" {
availability_zone = "${aws_instance.web.availability_zone}"
size = 1
tags = {
Name = "ebs-vol"
}
}
//Attaching EBS Volume to a Instance
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.web-vol.id}"
instance_id = "${aws_instance.web.id}"
force_detach = true
provisioner "remote-exec" {
connection {
agent = "false"
type = "ssh"
user = "ec2-user"
private_key = "${tls_private_key.tls_key.private_key_pem}"
host = "${aws_instance.web.public_ip}"
}
inline = [
"sudo mkfs.ext4 /dev/xvdh",
"sudo mount /dev/xvdh /var/www/html/",
"sudo rm -rf /var/www/html/*",
"sudo git clone https://github.com/VishalPraneeth/Terraform-AWS.git /var/www/html/",
]
}
depends_on = [
aws_instance.web,
aws_ebs_volume.web-vol
]
}