Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ Show all outputs:





15 changes: 15 additions & 0 deletions environments/dev/ec2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
provider "aws" {
region = var.region
}

module "ec2" {
source = "../../../modules/ec2"
region = var.region
instance_name = var.instance_name
ami_id = var.ami_id
instance_type = var.instance_type
key_name = var.key_name
instance_count = var.instance_count
subnet_ids = var.subnet_ids
security_group_ids = var.security_group_ids
}
14 changes: 14 additions & 0 deletions environments/dev/ec2/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "instance_state" {
description = "The state of the ec2 instance "
value = module.ec2.instance_state
}

output "instance_public_dns" {
description = "The Public DNS address of the ec2 instance"
value = module.ec2.instance_public_dns
}

output "instance_public_ip" {
description = "The Public Ip address of the ec2 instance"
value = module.ec2.instance_public_ip
}
39 changes: 39 additions & 0 deletions environments/dev/ec2/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variable "region" {
type = string
description = "Region of the ec2 instance"
}

variable "instance_name" {
type = string
description = "Name of the ec2 instance"
}

variable "ami_id" {
type = string
description = "AMI Id of the ec2 instance"
}

variable "instance_type" {
type = string
description = "Instance type of the ec2 instance"
}

variable "key_name" {
type = string
description = "Key name of the ec2 instance"
}

variable "security_group_ids" {
type = list(string)
description = "Security group id of the ec2 instance"
}

variable "instance_count" {
type = number
description = "Count of the ec2 instances"
}

variable "subnet_ids" {
type = list(string)
description = "Subnet ids of the ec2 instance"
}
34 changes: 34 additions & 0 deletions modules/ec2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
resource "aws_security_group" "instance-sg" {
name = "instance-sg"
description = "Security Group for Instance"

ingress {
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"]
}
}


resource "aws_instance" "gitlab_instance" {
count = var.instance_count

ami = var.ami_id
instance_type = var.instance_type
key_name = var.key_name
vpc_security_group_ids = [aws_security_group.instance-sg.id]

tags = {
Name = "${var.instance_name}-${count.index + 1}"
}

subnet_id = element(var.subnet_ids, count.index % length(var.subnet_ids))
}
14 changes: 14 additions & 0 deletions modules/ec2/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "instance_state" {
description = "The state of the ec2 instance "
value = aws_instance.gitlab_instance.*.instance_state
}

output "instance_public_dns" {
description = "The Public DNS address of the ec2 instance"
value = aws_instance.gitlab_instance.*.public_dns
}

output "instance_public_ip" {
description = "The Public Ip address of the ec2 instance"
value = aws_instance.gitlab_instance.*.public_ip
}
39 changes: 39 additions & 0 deletions modules/ec2/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variable "region" {
type = string
description = "Region of the ec2 instance"
}

variable "instance_name" {
type = string
description = "Name of the ec2 instance"
}

variable "ami_id" {
type = string
description = "AMI Id of the ec2 instance"
}

variable "instance_type" {
type = string
description = "Instance type of the ec2 instance"
}

variable "key_name" {
type = string
description = "Key name of the ec2 instance"
}

variable "security_group_ids" {
type = list(string)
description = "Security group id of the ec2 instance"
}

variable "instance_count" {
type = number
description = "Count of the ec2 instances"
}

variable "subnet_ids" {
type = list(string)
description = "Subnet ids of the ec2 instance"
}
9 changes: 9 additions & 0 deletions vars/dev/ec2.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ec2 instance vars
region = "eu-north-1"
instance_name = "GitLab-CI"
ami_id = "ami-0989fb15ce71ba39e"
instance_type = "t3.micro"
key_name = "techiescamp"
instance_count = 1
security_group_ids = ["sg-0aa656667277a3e65"]
subnet_ids = ["subnet-07fab58fa9620dfb9", "subnet-051fbcbaa925b8c44", "subnet-0c46e29a23c5ba3b8"]