Skip to content

Commit

Permalink
testing random pet name to resource names
Browse files Browse the repository at this point in the history
  • Loading branch information
star3am committed Jul 4, 2024
1 parent 276a735 commit ede55b6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 40 deletions.
26 changes: 26 additions & 0 deletions .terraform.lock.hcl

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

21 changes: 15 additions & 6 deletions modules/aws-hashiqube/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ terraform {
source = "hashicorp/external"
version = "~> 2.3"
}
random = {
source = "hashicorp/random"
version = "3.6.2"
}
}
}

data "external" "myipaddress" {
program = ["bash", "-c", "curl -m 10 -sk 'https://api.ipify.org?format=json'"]
}

resource "random_pet" "hashiqube" {
length = 1
}

resource "null_resource" "hashiqube" {
triggers = {
deploy_to_aws = var.deploy_to_aws
Expand All @@ -40,6 +48,7 @@ resource "null_resource" "hashiqube" {
timestamp = local.timestamp
debug_user_data = var.debug_user_data
use_packer_image = var.use_packer_image
random_pet = random_pet.hashiqube.id
}
}

Expand Down Expand Up @@ -80,7 +89,7 @@ data "aws_ami" "packer" {
}

resource "aws_iam_role" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
Expand All @@ -99,13 +108,13 @@ EOF
}

resource "aws_iam_instance_profile" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
role = aws_iam_role.hashiqube.name
}

# tfsec:ignore:aws-iam-no-policy-wildcards
resource "aws_iam_role_policy" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
role = aws_iam_role.hashiqube.id
policy = <<EOF
{
Expand Down Expand Up @@ -144,17 +153,17 @@ resource "aws_instance" "hashiqube" {
}))
iam_instance_profile = aws_iam_instance_profile.hashiqube.name
tags = {
Name = "hashiqube"
Name = "hashiqube-${random_pet.hashiqube.id}"
}
}

resource "aws_key_pair" "hashiqube" {
key_name = "hashiqube"
key_name = "hashiqube-${random_pet.hashiqube.id}"
public_key = var.ssh_public_key
}

resource "aws_security_group" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
description = "Allow Your Whitelist CIDR addresses"
ingress {
from_port = 0
Expand Down
37 changes: 23 additions & 14 deletions modules/azure-hashiqube/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ terraform {
source = "hashicorp/external"
version = "~> 2.3"
}
random = {
source = "hashicorp/random"
version = "3.6.2"
}
}
}

data "external" "myipaddress" {
program = ["bash", "-c", "curl -m 10 -sk 'https://api.ipify.org?format=json'"]
}

resource "random_pet" "hashiqube" {
length = 1
}

resource "null_resource" "hashiqube" {
triggers = {
deploy_to_aws = var.deploy_to_aws
Expand All @@ -38,6 +46,7 @@ resource "null_resource" "hashiqube" {
timestamp = local.timestamp
debug_user_data = var.debug_user_data
use_packer_image = var.use_packer_image
random_pet = random_pet.hashiqube.id
}
}

Expand All @@ -47,7 +56,7 @@ locals {

# Create a resource group if it doesnt exist
resource "azurerm_resource_group" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
location = var.azure_region
tags = {
environment = "hashiqube"
Expand All @@ -56,7 +65,7 @@ resource "azurerm_resource_group" "hashiqube" {

# Create virtual network
resource "azurerm_virtual_network" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
address_space = ["10.0.0.0/16"]
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
Expand All @@ -67,15 +76,15 @@ resource "azurerm_virtual_network" "hashiqube" {

# Create subnet
resource "azurerm_subnet" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
resource_group_name = azurerm_resource_group.hashiqube.name
virtual_network_name = azurerm_virtual_network.hashiqube.name
address_prefixes = ["10.0.1.0/24"]
}

# Create public IPs
resource "azurerm_public_ip" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
location = azurerm_resource_group.hashiqube.location
resource_group_name = azurerm_resource_group.hashiqube.name
allocation_method = "Static"
Expand All @@ -86,7 +95,7 @@ resource "azurerm_public_ip" "hashiqube" {

# Create Network Security Group and rule
resource "azurerm_network_security_group" "my_ipaddress" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -107,7 +116,7 @@ resource "azurerm_network_security_group" "my_ipaddress" {

resource "azurerm_network_security_group" "azure_hashiqube_ip" {
count = var.deploy_to_azure ? 1 : 0
name = "azure_hashiqube_ip"
name = "azure_hashiqube_ip_${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -128,7 +137,7 @@ resource "azurerm_network_security_group" "azure_hashiqube_ip" {

resource "azurerm_network_security_group" "aws_hashiqube_ip" {
count = var.deploy_to_aws ? 1 : 0
name = "aws_hashiqube_ip"
name = "aws_hashiqube_ip_${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -149,7 +158,7 @@ resource "azurerm_network_security_group" "aws_hashiqube_ip" {

resource "azurerm_network_security_group" "gcp_hashiqube_ip" {
count = var.deploy_to_gcp ? 1 : 0
name = "gcp_hashiqube_ip"
name = "gcp_hashiqube_ip_${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -170,7 +179,7 @@ resource "azurerm_network_security_group" "gcp_hashiqube_ip" {

resource "azurerm_network_security_group" "whitelist_cidrs" {
count = var.whitelist_cidrs != "" ? 1 : 0
name = "whitelist_cidr"
name = "whitelist_cidr_${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -191,7 +200,7 @@ resource "azurerm_network_security_group" "whitelist_cidrs" {

resource "azurerm_network_security_group" "terraform_cloud_api_ip_ranges" {
count = var.debug_user_data == true ? 1 : 0
name = "terraform_cloud_api_ip_ranges"
name = "terraform_cloud_api_ip_ranges_${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -212,7 +221,7 @@ resource "azurerm_network_security_group" "terraform_cloud_api_ip_ranges" {

resource "azurerm_network_security_group" "terraform_cloud_notifications_ip_ranges" {
count = var.debug_user_data == true ? 1 : 0
name = "terraform_cloud_notifications_ip_ranges"
name = "terraform_cloud_notifications_ip_ranges_${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -233,7 +242,7 @@ resource "azurerm_network_security_group" "terraform_cloud_notifications_ip_rang

resource "azurerm_network_security_group" "debug_allow_ssh_cidr_range" {
count = var.debug_allow_ssh_cidr_range != "" ? 1 : 0
name = "debug_allow_ssh_cidr_range"
name = "debug_allow_ssh_cidr_range_${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
security_rule {
Expand All @@ -254,7 +263,7 @@ resource "azurerm_network_security_group" "debug_allow_ssh_cidr_range" {

# Create network interface
resource "azurerm_network_interface" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
location = var.azure_region
resource_group_name = azurerm_resource_group.hashiqube.name
ip_configuration {
Expand All @@ -270,7 +279,7 @@ resource "azurerm_network_interface" "hashiqube" {

# Create virtual machine
resource "azurerm_linux_virtual_machine" "hashiqube" {
name = "hashiqube"
name = "hashiqube-${random_pet.hashiqube.id}"
location = var.azure_region
size = var.azure_instance_type
admin_username = "ubuntu"
Expand Down
Loading

0 comments on commit ede55b6

Please sign in to comment.