From 10dd3039862c15ab93221fd12da3eb1e89acffda Mon Sep 17 00:00:00 2001 From: Rupal Sharma Date: Tue, 27 Feb 2024 15:44:17 +0530 Subject: [PATCH] feat:add user data attribute --- _example/linux-vm/example.tf | 13 ++++++++----- _example/linux-vm/user-data.sh | 8 ++++++++ main.tf | 2 ++ variables.tf | 5 +++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100755 _example/linux-vm/user-data.sh diff --git a/_example/linux-vm/example.tf b/_example/linux-vm/example.tf index 339f2ed..412f0f7 100644 --- a/_example/linux-vm/example.tf +++ b/_example/linux-vm/example.tf @@ -90,7 +90,7 @@ module "security_group" { module "key_vault" { source = "clouddrove/key-vault/azure" version = "1.1.0" - name = "app399433" + name = "hello55" environment = "test" label_order = ["name", "environment", ] resource_group_name = module.resource_group.resource_group_name @@ -148,10 +148,10 @@ module "virtual-machine" { network_interface_sg_enabled = true network_security_group_id = module.security_group.id ## Public IP - public_ip_enabled = false + public_ip_enabled = true ## Virtual Machine vm_size = "Standard_B1s" - public_key = "ssh-rsa AAAA" + public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC/e5bluEqTbb3qtuoKh1qEXMP4bk6QtqPrbhg7bMYQjJChmzvuA/aqA8gkQ48lweoFZMd7yr/ylhBPXvpYQ450INW6CNksatr4+z+EXHzyJ3MBTLSWlFc+ut6ji6Fglgkk3fe4sJw2fPZlf3FWFkomLJ3dIOIHyHQO6IaL9ZP22TRSPegSceNC30XF4xz1nHYiqNlZVq/COMHDtammwq8VnitkP6lRbokeJ1HlNBaJygUHlzYKrg8nguxGph3rlG9g+2+xAYf/qHQ/8k14xUMac7JBORnv+HAI+YHYCRYlQVXg635Bnj83jWmDK5Wed98O9ORARv70UN8Bj46247cdkeXXOAslUW19RQj0BA+QYRRvsXfLqlNX/Wq9V5yp66wO4QZV/2wvmoUwSELz6fn+qQvdBbs8IN9K7oJz2nl43Dth0vLnn8IrG7V2PfgW1g4WHjK5/GJXfyZXEYiM5Aye4W8V24g0AnunjIldBTnpofT6q4ki9bXx6en9EeBYB6E=" admin_username = "ubuntu" caching = "ReadWrite" disk_size_gb = 30 @@ -180,6 +180,9 @@ module "virtual-machine" { }] #### enable diagnostic setting - diagnostic_setting_enable = true + diagnostic_setting_enable = false log_analytics_workspace_id = module.log-analytics.workspace_id ## when diagnostic_setting_enable enable, add log analytics workspace id -} + + #vm With User Data + user_data = file("user-data.sh") +} \ No newline at end of file diff --git a/_example/linux-vm/user-data.sh b/_example/linux-vm/user-data.sh new file mode 100755 index 0000000..48043db --- /dev/null +++ b/_example/linux-vm/user-data.sh @@ -0,0 +1,8 @@ +#! /bin/bash +sudo apt-get update +sudo apt-get install -y apache2 +sudo systemctl start apache2 +sudo systemctl enable apache2 +echo "

Deployed via Terraform

" | sudo tee /var/www/html/index.html +sudo systemctl restart apache2 + diff --git a/main.tf b/main.tf index 2222b07..01f147d 100644 --- a/main.tf +++ b/main.tf @@ -99,6 +99,7 @@ resource "azurerm_linux_virtual_machine" "default" { name = var.vm_addon_name == null ? format("%s-virtual-machine-%s", module.labels.id, count.index + 1) : format("%s-virtual-machine-%s", module.labels.id, var.vm_addon_name) resource_group_name = var.resource_group_name location = var.location + custom_data = base64encode(file("user-data.sh")) size = var.vm_size admin_username = var.admin_username admin_password = var.disable_password_authentication == true ? null : var.admin_password @@ -178,6 +179,7 @@ resource "azurerm_linux_virtual_machine" "default" { } } + timeouts { create = var.create update = var.update diff --git a/variables.tf b/variables.tf index 1236d6b..465490b 100644 --- a/variables.tf +++ b/variables.tf @@ -607,3 +607,8 @@ variable "user_object_id" { default = {} description = "The ID of the Principal (User, Group or Service Principal) to assign the Role Definition to. Changing this forces a new resource to be created." } +variable "user_data" { + type = string + default = "" + description = "(Optional) A string of the desired User Data for the vm." +}