Skip to content

Commit

Permalink
Added ec2 terraform script
Browse files Browse the repository at this point in the history
  • Loading branch information
rishavnandi committed Mar 11, 2023
1 parent 6c7b456 commit aaf658b
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
24 changes: 24 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
provider "aws" {
region = "us-east-1"
}

resource "aws_vpc" "tf_vpc" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "tf_vpc"
}
}

resource "aws_subnet" "tf_subnet" {
vpc_id = aws_vpc.tf_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"

tags = {
Name = "tf_subnet"
}
}

resource "aws_internet_gateway" "tf_igw" {
vpc_id = aws_vpc.tf_vpc.id

tags = {
Name = "tf_igw"
}
}

resource "aws_route_table" "tf_rt" {
vpc_id = aws_vpc.tf_vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.tf_igw.id
}

tags = {
Name = "tf_rt"
}
}

resource "aws_route_table_association" "tf_rta" {
subnet_id = aws_subnet.tf_subnet.id
route_table_id = aws_route_table.tf_rt.id
}

resource "aws_security_group" "tf_sg" {
name = "tf_sg"
vpc_id = aws_vpc.tf_vpc.id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 5000
to_port = 5000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "tf_sg"
}
}

data "aws_ami" "tf_ami" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

filter {
name = "architecture"
values = ["x86_64"]
}
}

resource "aws_instance" "tf_instance" {
ami = data.aws_ami.tf_ami.id
instance_type = "t2.micro"
associate_public_ip_address = true
subnet_id = aws_subnet.tf_subnet.id
vpc_security_group_ids = [aws_security_group.tf_sg.id]

tags = {
Name = "tf_instance"
}
}

0 comments on commit aaf658b

Please sign in to comment.