Skip to content

Commit

Permalink
feat: modulize hashicat
Browse files Browse the repository at this point in the history
- mv to module aws_instance and provisioner(null_resource)
- change their names as `this`
- pass common vars
- change root var placeholder as array
- pass network and ssh vars as required ones
  • Loading branch information
flavono123 committed Sep 24, 2023
1 parent 9291c66 commit e96e931
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 76 deletions.
94 changes: 22 additions & 72 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,78 +103,6 @@ resource "aws_route_table_association" "hashicat" {
route_table_id = aws_route_table.hashicat.id
}

data "aws_ami" "ubuntu" {
most_recent = true

filter {
name = "name"
#values = ["ubuntu/images/hvm-ssd/ubuntu-disco-19.04-amd64-server-*"]
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}

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

owners = ["099720109477"] # Canonical
}

resource "aws_instance" "hashicat" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
key_name = aws_key_pair.hashicat.key_name
associate_public_ip_address = true
subnet_id = aws_subnet.hashicat.id
vpc_security_group_ids = [aws_security_group.hashicat.id]

tags = {
Name = "${var.prefix}-hashicat-instance"
}
}

resource "null_resource" "configure-cat-app" {
depends_on = [aws_instance.hashicat]

// triggers = {
// build_number = timestamp()
// }

provisioner "file" {
source = "files/"
destination = "/home/ubuntu/"

connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.hashicat.private_key_pem
host = aws_instance.hashicat.public_ip
}
}

provisioner "remote-exec" {
inline = [
"sudo apt -y update",
"sleep 15",
"sudo apt -y update",
"sudo apt -y install apache2",
"sudo systemctl start apache2",
"sudo chown -R ubuntu:ubuntu /var/www/html",
"chmod +x *.sh",
"PLACEHOLDER=${var.placeholder} WIDTH=${var.width} HEIGHT=${var.height} PREFIX=${var.prefix} ./deploy_app.sh",
"sudo apt -y install cowsay",
"cowsay Mooooooooooo!",
]

connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.hashicat.private_key_pem
host = aws_instance.hashicat.public_ip
}
}
}

resource "tls_private_key" "hashicat" {
algorithm = "RSA"
}
Expand All @@ -187,3 +115,25 @@ resource "aws_key_pair" "hashicat" {
key_name = local.private_key_filename
public_key = tls_private_key.hashicat.public_key_openssh
}

module "hashicats" {
source = "./modules/hashicat"
count = length(var.placeholders)

# inherit common variables from root module
prefix = var.prefix
instance_type = var.instance_type
height = var.height
width = var.width

# set placeholder for each
placeholder = var.placeholders[count.index]

# additional(required)
# network things
subnet_id = aws_subnet.hashicat.id
vpc_segurity_group_id = aws_security_group.hashicat.id
# ssh key
key_name = aws_key_pair.hashicat.key_name
private_key = tls_private_key.hashicat.private_key_pem
}
71 changes: 71 additions & 0 deletions modules/hashicat/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
data "aws_ami" "ubuntu" {
most_recent = true

filter {
name = "name"
#values = ["ubuntu/images/hvm-ssd/ubuntu-disco-19.04-amd64-server-*"]
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}

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

owners = ["099720109477"] # Canonical
}

resource "aws_instance" "this" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
key_name = var.key_name
associate_public_ip_address = true
subnet_id = var.subnet_id
vpc_security_group_ids = [var.vpc_segurity_group_id]

tags = {
Name = "${var.prefix}-this-instance"
}
}

resource "null_resource" "configure-cat-app" {
depends_on = [aws_instance.this]

// triggers = {
// build_number = timestamp()
// }

provisioner "file" {
source = "files/"
destination = "/home/ubuntu/"

connection {
type = "ssh"
user = "ubuntu"
private_key = var.private_key
host = aws_instance.this.public_ip
}
}

provisioner "remote-exec" {
inline = [
"sudo apt -y update",
"sleep 15",
"sudo apt -y update",
"sudo apt -y install apache2",
"sudo systemctl start apache2",
"sudo chown -R ubuntu:ubuntu /var/www/html",
"chmod +x *.sh",
"PLACEHOLDER=${var.placeholder} WIDTH=${var.width} HEIGHT=${var.height} PREFIX=${var.prefix} ./deploy_app.sh",
"sudo apt -y install cowsay",
"cowsay Mooooooooooo!",
]

connection {
type = "ssh"
user = "ubuntu"
private_key = var.private_key
host = aws_instance.this.public_ip
}
}
}
7 changes: 7 additions & 0 deletions modules/hashicat/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "catapp_url" {
value = "http://${aws_instance.this.public_dns}"
}

output "catapp_ip" {
value = "http://${aws_instance.this.public_ip}"
}
42 changes: 42 additions & 0 deletions modules/hashicat/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
variable "prefix" {
description = "This prefix will be included in the name of most resources."
}

variable "instance_type" {
description = "Specifies the AWS instance type."
default = "t3.micro"
}

variable "height" {
default = "400"
description = "Image height in pixels."
}

variable "width" {
default = "600"
description = "Image width in pixels."
}

variable "placeholder" {
type = string
description = "Image-as-a-service URL. Some other fun ones to try are fillmurray.com, placecage.com, placebeard.it, loremflickr.com, baconmockup.com, placeimg.com, placebear.com, placeskull.com, stevensegallery.com, placedog.net"
}

# Network
variable "subnet_id" {
description = "id of the subnet"
}

variable "vpc_segurity_group_id" {
description = "id of the security group"
}

# SSH
variable "key_name" {
description = "Name of the SSH keypair to use in AWS"
}


variable "private_key" {
description = "SSH private key"
}
10 changes: 8 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
output "catapp_url" {
value = "http://${aws_instance.hashicat.public_dns}"
value = [
for hashicat in module.hashicats :
hashicat.catapp_url
]
}

output "catapp_ip" {
value = "http://${aws_instance.hashicat.public_ip}"
value = [
for instance in module.hashicats :
instance.catapp_ip
]
}
8 changes: 6 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ variable "width" {
description = "Image width in pixels."
}

variable "placeholder" {
default = "placekitten.com"
variable "placeholders" {
default = [
"placekitten.com",
"placebear.com",
"placedog.net"
]
description = "Image-as-a-service URL. Some other fun ones to try are fillmurray.com, placecage.com, placebeard.it, loremflickr.com, baconmockup.com, placeimg.com, placebear.com, placeskull.com, stevensegallery.com, placedog.net"
}

0 comments on commit e96e931

Please sign in to comment.