-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-create-ec2-instance-through-vpc.tf
124 lines (104 loc) · 2.44 KB
/
3-create-ec2-instance-through-vpc.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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
profile="vishnu2"
}
########### Create a VPC
resource "aws_vpc" "my-vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "EKYC VPC"
}
}
# Create Web layer Public Subnet
resource "aws_subnet" "web-subnet-1" {
vpc_id = aws_vpc.my-vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = {
Name = "SN-BASTION-EKYC-1"
}
}
# Create Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my-vpc.id
tags = {
Name = "EKYC-IGW"
}
}
# Create Web layber route table
resource "aws_route_table" "web-rt" {
vpc_id = aws_vpc.my-vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "EKYC-IGRT"
}
}
# Create Web Subnet association with Web route table
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.web-subnet-1.id
route_table_id = aws_route_table.web-rt.id
}
# Create Web Security Group
resource "aws_security_group" "webserver-sg" {
name = "Web-SG"
description = "Allow HTTP inbound traffic"
vpc_id = aws_vpc.my-vpc.id
ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
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"]
}
tags = {
Name = "Web-SG"
}
}
#Create EC2 Instance for BASTION
resource "aws_instance" "webserver1" {
ami = "ami-05fa00d4c63e32376"
instance_type = "t2.micro"
availability_zone = "us-east-1a"
vpc_security_group_ids = [aws_security_group.webserver-sg.id]
subnet_id = aws_subnet.web-subnet-1.id
key_name = "user"
connection {
type = "ssh"
host = self.public_ip
user = "ec2-user"
#private_key = file("C:/Users/clou
private_key = file("C:/Users/Downloads/user")
timeout = "4m"
}
tags = {
Name = "BASTION-EKYC-1"
}
}