Skip to content

Commit c254c80

Browse files
committed
added new module to create Ec2 in VPC
1 parent e1b5f76 commit c254c80

File tree

6 files changed

+143
-14
lines changed

6 files changed

+143
-14
lines changed

terraform-ec2-with-vpc/instance.tf

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "aws_instance" "web" {
2+
ami = lookup(var.ami_id, var.region)
3+
instance_type = var.instance_type
4+
5+
# Public Subnet assign to instance
6+
subnet_id = aws_subnet.public_1.id
7+
8+
# Security group assign to instance
9+
vpc_security_group_ids=[aws_security_group.allow_ssh.id]
10+
11+
# key name
12+
key_name = var.key_name
13+
14+
15+
tags = {
16+
Name = "Ec2-with-VPC"
17+
}
18+
}

terraform-ec2-with-vpc/provider.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = "${var.region}"
3+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "aws_security_group" "allow_ssh" {
2+
name = "allow_SSH"
3+
description = "Allow SSH inbound traffic"
4+
vpc_id = aws_vpc.vpc_demo.id
5+
6+
ingress {
7+
# SSH Port 22 allowed from any IP
8+
from_port = 22
9+
to_port = 22
10+
protocol = "tcp"
11+
cidr_blocks = ["0.0.0.0/0"]
12+
}
13+
14+
egress {
15+
from_port = 0
16+
to_port = 0
17+
protocol = "-1"
18+
cidr_blocks = ["0.0.0.0/0"]
19+
}
20+
}

terraform-ec2-with-vpc/variables.tf

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
variable "region" {
2+
type = "string"
3+
default = "us-east-1"
4+
}
5+
variable "ami_id" {
6+
type = "map"
7+
default = {
8+
us-east-1 = "ami-035b3c7efe6d061d5"
9+
eu-west-2 = "ami-132b3c7efe6sdfdsfd"
10+
eu-central-1 = "ami-9787h5h6nsn75gd33"
11+
}
12+
}
13+
variable "instance_type" {
14+
type = "string"
15+
default = "t2.micro"
16+
}
17+
variable "key_name" {
18+
type = "string"
19+
default = "ec2-demo"
20+
}
21+
22+
variable "cidr" {
23+
description = "The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden"
24+
type = string
25+
default = "10.0.0.0/16"
26+
}
27+
variable "instance_tenancy" {
28+
description = "A tenancy option for instances launched into the VPC"
29+
type = string
30+
default = "default"
31+
}
32+
33+
variable "enable_dns_hostnames" {
34+
description = "Should be true to enable DNS hostnames in the VPC"
35+
type = bool
36+
default = true
37+
}
38+
39+
variable "enable_dns_support" {
40+
description = "Should be true to enable DNS support in the VPC"
41+
type = bool
42+
default = true
43+
}
44+
45+
variable "enable_classiclink" {
46+
description = "Should be true to enable ClassicLink for the VPC. Only valid in regions and accounts that support EC2 Classic."
47+
type = bool
48+
default = false
49+
}
50+
51+
variable "tags" {
52+
description = "A map of tags to add to all resources"
53+
type = string
54+
default = "Vpc-custom-demo"
55+
}

terraform-ec2-with-vpc/vpc.tf

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
resource "aws_vpc" "vpc_demo" {
2+
cidr_block = var.cidr
3+
instance_tenancy = var.instance_tenancy
4+
enable_dns_hostnames = var.enable_dns_hostnames
5+
enable_dns_support = var.enable_dns_support
6+
enable_classiclink = var.enable_classiclink
7+
8+
tags = {
9+
Name = var.tags
10+
}
11+
}
12+
13+
resource "aws_internet_gateway" "gw" {
14+
vpc_id = aws_vpc.vpc_demo.id
15+
16+
tags = {
17+
Name = "internet-gateway-demo"
18+
}
19+
}
20+
21+
resource "aws_subnet" "public_1" {
22+
vpc_id = aws_vpc.vpc_demo.id
23+
map_public_ip_on_launch = true
24+
cidr_block = "10.0.1.0/24"
25+
26+
tags = {
27+
Name = "public_1-demo"
28+
}
29+
}
30+
31+
resource "aws_route_table" "route-public" {
32+
vpc_id = aws_vpc.vpc_demo.id
33+
34+
route {
35+
cidr_block = "10.0.0.0/0"
36+
gateway_id = aws_internet_gateway.gw.id
37+
}
38+
39+
tags = {
40+
Name = "public-route-table-demo"
41+
}
42+
}
43+
44+
resource "aws_route_table_association" "public_1" {
45+
subnet_id = aws_subnet.public_1.id
46+
route_table_id = aws_route_table.route-public.id
47+
}

variables.tf

-14
This file was deleted.

0 commit comments

Comments
 (0)