diff --git a/environments/dev/main.tf b/environments/dev/main.tf index cfa44e2..6991732 100644 --- a/environments/dev/main.tf +++ b/environments/dev/main.tf @@ -33,6 +33,13 @@ data "azurerm_ssh_public_key" "sd" { resource_group_name = "terraform" } +data "http" "my_ip" { + url = "https://checkip.amazonaws.com/" +} + +locals { + my_ip = chomp(data.http.my_ip.response_body) +} module "compute" { source = "../../modules/compute" @@ -42,4 +49,5 @@ module "compute" { vm_size = "Standard_B1s" admin_username = "azureuser" ssh_public_key = data.azurerm_ssh_public_key.sd.public_key + ex_ip = local.my_ip } diff --git a/environments/dev/outputs.tf b/environments/dev/outputs.tf index e69de29..21d299a 100644 --- a/environments/dev/outputs.tf +++ b/environments/dev/outputs.tf @@ -0,0 +1,3 @@ +output "my_ip" { + value = local.my_ip +} \ No newline at end of file diff --git a/modules/compute/main.tf b/modules/compute/main.tf index 7e91bf3..52a53f1 100644 --- a/modules/compute/main.tf +++ b/modules/compute/main.tf @@ -14,13 +14,8 @@ resource "azurerm_network_interface" "nic" { resource "azurerm_network_interface_security_group_association" "example" { network_interface_id = azurerm_network_interface.nic.id network_security_group_id = azurerm_network_security_group.vm_nsg.id -} -resource "azurerm_public_ip" "vm_public" { - name = "example-pip" - location = var.location - resource_group_name = var.resource_group_name - allocation_method = "Static" + depends_on = [azurerm_network_security_group.vm_nsg] } resource "azurerm_network_security_group" "vm_nsg" { @@ -36,11 +31,18 @@ resource "azurerm_network_security_group" "vm_nsg" { protocol = "Tcp" source_port_range = "*" destination_port_range = "22" - source_address_prefix = "81.78.69.107" + source_address_prefix = var.ex_ip destination_address_prefix = "*" } } +resource "azurerm_public_ip" "vm_public" { + name = "example-pip" + location = var.location + resource_group_name = var.resource_group_name + allocation_method = "Static" +} + resource "azurerm_linux_virtual_machine" "vm" { name = "example-vm" resource_group_name = var.resource_group_name diff --git a/modules/compute/variables.tf b/modules/compute/variables.tf index e5636fa..b09350c 100644 --- a/modules/compute/variables.tf +++ b/modules/compute/variables.tf @@ -4,3 +4,4 @@ variable "subnet_id" { type = string } variable "vm_size" { type = string } variable "admin_username" { type = string } variable "ssh_public_key" { type = string } +variable "ex_ip" { type = string }