Skip to content

Commit c8b2b4c

Browse files
committed
added ec2, network modules
1 parent 388e64c commit c8b2b4c

File tree

17 files changed

+222
-0
lines changed

17 files changed

+222
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
11+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as passwords
12+
*.tfvars
13+
14+
# Ignore override files as they are usually used to override resources locally and should not be shared
15+
override.tf
16+
override.tf.json
17+
*_override.tf
18+
*_override.tf.json
19+
20+
# Include .terraformrc and terraform.rc files as they are user-specific configuration files
21+
.terraformrc
22+
terraform.rc
23+
24+
# Ignore any plan output files
25+
*.tfplan

tf-ec2-with-modules/terraform-project/.terraform.lock.hcl

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
provider "aws" {
2+
region = "us-west-2"
3+
}
4+
5+
module "vpc" {
6+
source = "./modules/vpc"
7+
cidr_block = "10.0.0.0/16"
8+
}
9+
10+
module "subnet" {
11+
source = "./modules/subnet"
12+
vpc_id = module.vpc.vpc_id
13+
cidr_block = "10.0.1.0/24"
14+
availability_zone = "us-west-2a"
15+
}
16+
17+
module "security_group" {
18+
source = "./modules/security-group"
19+
vpc_id = module.vpc.vpc_id
20+
}
21+
22+
23+
data "aws_ami" "example" {
24+
most_recent = true
25+
26+
filter {
27+
name = "name"
28+
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
29+
}
30+
31+
filter {
32+
name = "virtualization-type"
33+
values = ["hvm"]
34+
}
35+
36+
owners = ["amazon"]
37+
}
38+
39+
module "ec2" {
40+
source = "./modules/ec2"
41+
ami = data.aws_ami.example.id
42+
instance_type = "t2.micro"
43+
subnet_id = module.subnet.subnet_id
44+
security_group_id = module.security_group.security_group_id
45+
key_name = var.key_name
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "aws_instance" "main" {
2+
ami = var.ami
3+
instance_type = var.instance_type
4+
subnet_id = var.subnet_id
5+
security_groups = [var.security_group_id]
6+
key_name = var.key_name
7+
8+
tags = {
9+
Name = "MyEC2Instance"
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "instance_id" {
2+
value = aws_instance.main.id
3+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "ami" {
2+
description = "The AMI ID for the EC2 instance"
3+
type = string
4+
}
5+
6+
variable "instance_type" {
7+
description = "The instance type for the EC2 instance"
8+
type = string
9+
}
10+
11+
variable "subnet_id" {
12+
description = "The ID of the subnet"
13+
type = string
14+
}
15+
16+
variable "security_group_id" {
17+
description = "The ID of the security group"
18+
type = string
19+
}
20+
21+
variable "key_name" {
22+
description = "value of the key pair"
23+
type = string
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "aws_security_group" "main" {
2+
vpc_id = var.vpc_id
3+
4+
ingress {
5+
from_port = 22
6+
to_port = 22
7+
protocol = "tcp"
8+
cidr_blocks = ["0.0.0.0/0"]
9+
}
10+
11+
egress {
12+
from_port = 0
13+
to_port = 0
14+
protocol = "-1"
15+
cidr_blocks = ["0.0.0.0/0"]
16+
}
17+
18+
tags = {
19+
Name = "allow_ssh"
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "security_group_id" {
2+
value = aws_security_group.main.id
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "vpc_id" {
2+
description = "The ID of the VPC"
3+
type = string
4+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
resource "aws_subnet" "main" {
2+
vpc_id = var.vpc_id
3+
cidr_block = var.cidr_block
4+
availability_zone = var.availability_zone
5+
map_public_ip_on_launch = true
6+
}
7+
8+
resource "aws_internet_gateway" "main" {
9+
vpc_id = var.vpc_id
10+
}
11+
12+
resource "aws_route_table" "public" {
13+
vpc_id = var.vpc_id
14+
15+
route {
16+
cidr_block = "0.0.0.0/0"
17+
gateway_id = aws_internet_gateway.main.id
18+
}
19+
}
20+
21+
resource "aws_route_table_association" "public" {
22+
subnet_id = aws_subnet.main.id
23+
route_table_id = aws_route_table.public.id
24+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "subnet_id" {
2+
value = aws_subnet.main.id
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "vpc_id" {
2+
description = "The ID of the VPC"
3+
type = string
4+
}
5+
6+
variable "cidr_block" {
7+
description = "The CIDR block for the subnet"
8+
type = string
9+
}
10+
11+
variable "availability_zone" {
12+
description = "The availability zone for the subnet"
13+
type = string
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "aws_vpc" "main" {
2+
cidr_block = var.cidr_block
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "vpc_id" {
2+
value = aws_vpc.main.id
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "cidr_block" {
2+
description = "The CIDR block for the VPC"
3+
type = string
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "instance_id" {
2+
value = module.ec2.instance_id
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Define any global variables if needed
2+
variable "key_name" {
3+
description = "value of the key pair"
4+
type = string
5+
default = "your-pem-key-name"
6+
}
7+

0 commit comments

Comments
 (0)