-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_configure_setup
More file actions
145 lines (129 loc) · 3.59 KB
/
Copy path04_configure_setup
File metadata and controls
145 lines (129 loc) · 3.59 KB
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
Step1:
mkdir terraform
cd terraform/
Step2: Create variables files - for aws provider
```
[ec2-user@ip-10-0-14-30 terraform]$ cat variables.tf
provider "aws" {
access_key = "AKghskass$%%%%%%V"
secret_key = "xwva@##$$*************QXSj4X3"
region = "us-east-1"
}
```
NOTE! Get the user accees key and secret key for the respective AWS user. Make sure user has got right privileges [ example: admin rights]
Step3: Here I have added all the required components to provision EC2 in AWS inside setup.tf
```
[ec2-user@ip-10-0-14-30 terraform]$ cat setup.tf
#Create key-pair for logging into EC2 in us-east-1
resource "aws_key_pair" "webserver-key" {
key_name = "webserver-key"
public_key = file("~/.ssh/webserver-key.pub")
}
#Get Linux AMI ID using SSM Parameter endpoint in us-east-1
data "aws_ssm_parameter" "webserver-ami" {
name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}
#Create VPC in us-east-1
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "terraform-vpc"
}
}
#Create IGW in us-east-1
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
}
#Get main route table to modify
data "aws_route_table" "main_route_table" {
filter {
name = "association.main"
values = ["true"]
}
filter {
name = "vpc-id"
values = [aws_vpc.vpc.id]
}
}
#Create route table in us-east-1
resource "aws_default_route_table" "internet_route" {
default_route_table_id = data.aws_route_table.main_route_table.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "Terraform-RouteTable"
}
}
#Get all available AZ's in VPC for master region
data "aws_availability_zones" "azs" {
state = "available"
}
#Create subnet # 1 in us-east-1
resource "aws_subnet" "subnet" {
availability_zone = element(data.aws_availability_zones.azs.names, 0)
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.1.0/24"
}
#Create SG for allowing TCP/80 & TCP/22
resource "aws_security_group" "sg" {
name = "sg"
description = "Allow TCP/80 & TCP/22"
vpc_id = aws_vpc.vpc.id
ingress {
description = "Allow SSH traffic"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "allow traffic from TCP/80"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
output "Webserver-Public-IP" {
value = aws_instance.webserver.public_ip
}
```
STep 4: main file is as below
[ec2-user@ip-10-0-14-30 terraform]$ cat main.tf
```
#Create and bootstrap webserver
resource "aws_instance" "webserver" {
ami = data.aws_ssm_parameter.webserver-ami.value
instance_type = "t3.micro"
key_name = aws_key_pair.webserver-key.key_name
associate_public_ip_address = true
vpc_security_group_ids = [aws_security_group.sg.id]
subnet_id = aws_subnet.subnet.id
provisioner "remote-exec" {
inline = [
"sudo yum -y install httpd && sudo systemctl start httpd",
"echo '<h1><center>This is a Website - Terraform Practice</center></h1>' > index.html",
"sudo mv index.html /var/www/html/"
]
connection {
type = "ssh"
user = "ec2-user"
private_key = file("~/.ssh/webserver-key")
host = self.public_ip
}
}
tags = {
Name = "webserver"
}
}
```