Skip to content

Commit 14979c8

Browse files
authored
Merge pull request #1 from data-platform-hq/linux_vm
fix: vm
2 parents 20cd757 + 4532b8a commit 14979c8

File tree

5 files changed

+318
-3
lines changed

5 files changed

+318
-3
lines changed

README.md

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,105 @@
1-
# Azure <> Terraform module
2-
Terraform module for creation Azure <>
1+
# Azure linux vm Terraform module
2+
Terraform module for creation Azure linux vm
33

44
## Usage
55

6+
```hcl
7+
# Prerequisite resources
8+
9+
data "azurerm_resource_group" "example" {
10+
name = "example"
11+
}
12+
13+
data "azurerm_subnet" "example" {
14+
name = "example"
15+
virtual_network_name = "production"
16+
resource_group_name = data.azurerm_resource_group.example.name
17+
}
18+
19+
resource "tls_private_key" "example" {
20+
algorithm = "RSA"
21+
ecdsa_curve = 2048
22+
}
23+
24+
module "linux_virtual_machine" {
25+
source = "data-platform-hq/linux-vm/azurerm"
26+
version = "~> 1.0"
27+
28+
project = var.project
29+
env = var.env
30+
resource_group = data.azurerm_resource_group.example.name
31+
location = var.location
32+
subnet_id = data.azurerm_subnet.example.id
33+
34+
admin_ssh_key = {
35+
username = "userexample"
36+
public_key = tls_private_key.example.public_key_openssh
37+
}
38+
}
39+
```
640
<!-- BEGIN_TF_DOCS -->
41+
## Requirements
42+
43+
| Name | Version |
44+
| ---------------------------------------------------------------------------- | --------- |
45+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
46+
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | >= 3.40.0 |
47+
48+
## Providers
49+
50+
| Name | Version |
51+
| ---------------------------------------------------------------------- | ------- |
52+
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 3.40.0 |
53+
54+
55+
## Inputs
56+
57+
| Name | Description | Type | Default | Required |
58+
|------|-------------|------|---------|:--------:|
59+
| <a name="input_project"></a> [project](#input\_project)| Project name | `string` | n/a | yes |
60+
| <a name="input_env"></a> [env](#input\_env)| Environment name | `string` | n/a | yes |
61+
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group)| The name of the resource group | `string` | n/a | yes |
62+
| <a name="input_suffix"></a> [suffix](#input\_suffix)| Optional suffix that would be added to the end of resources names. It is recommended to use dash at the beginning of variable (e.x., '-example') | `string` | "" | no |
63+
| <a name="input_location"></a> [location](#input\_location)| Azure location | `string` | n/a | yes |
64+
| <a name="input_custom_virtual_machine_name"></a> [custom\_virtual\_machine\_name](#input\_custom\_virtual\_machine\_name)| Specifies the name of the virtual machine name resource | `string` | null | no |
65+
| <a name="input_custom_network_interface_name"></a> [custom\_network\_interface\_name](#input\_custom\_network\_interface\_name)| Specifies the name of the virtual machine interface name resource | `string` | null | no |
66+
| <a name="input_custom_public_ip_name"></a> [custom\_public\_ip\_name](#input\_custom\_public\_ip\_name)| Specifies the name of the public ip name name resource | `string` | null | no |
67+
| <a name="input_tags"></a> [tags](#input\_tags)| Resource tags | map(any) | {} | no |
68+
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id)| The ID of the Subnet where this Network Interface should be located in. | `string` | n/a | yes |
69+
| <a name="input_public_ip_enabled"></a> [public\_ip\_enabled](#input\_public\_ip\_enabled)| Boolean flag to enable Public Ip address creation and assignment to Virtual Machine | `bool` | true | no |
70+
| <a name="input_public_ip_allocation_method"></a> [public\_ip\_allocation\_method](#input\_public\_ip\_allocation_method)| Defines the allocation method for this IP address. Possible values are Static or Dynamic | `string` | Dynamic | no |
71+
| <a name="input_network_interface_private_ip_address_allocation"></a> [network\_interface\_private\_ip_address\_allocation](#input\_network\_interface\_private\_ip_address\_allocation)| The allocation method used for the Private IP Address. | `string` | Dynamic | no |
72+
| <a name="input_vm_size"></a> [vm\_size](#input\_vm\_size)| The SKU which should be used for this Virtual Machine. | `string` | Standard_F2 | no |
73+
| <a name="input_vm_admin_username"></a> [vm\_admin\_username](#input\_vm\_admin\_username)| The username of the local administrator used for the Virtual Machine. | `string` | adminuser | no |
74+
| <a name="input_vm_admin_password"></a> [vm\_admin\_password](#input\_vm\_admin\_password)| The password of the local administrator used for the Virtual Machine. | `string` | null | no |
75+
| <a name="input_admin_ssh_key"></a> [admin\_ssh\_key](#input\_admin\_ssh\_key)| Objects to configure ssh key reference for virtual machine | <pre>object({<br> username = string<br> public_key = string<br>})</pre> | n/a | yes |
76+
| <a name="input_os_disk"></a> [os\_disk](#input\_os\_disk)| Objects to configure os disk reference for virtual machine | <pre>object({<br> caching = string<br> storage_account_type = string<br>})</pre> | <pre>{<br> caching = "ReadWrite"<br> storage_account_type = "Standard_LRS"<br>}</pre> | no |
77+
| <a name="input_source_image_reference"></a> [source\_image\_reference](#input\_source\_image\_reference)| Objects to configure source image reference for virtual machine | <pre>object({<br> publisher = string<br> offer = string<br> sku = string<br> version = string<br>})</pre> | <pre>{<br> publisher = "Canonical"<br> offer = "0001-com-ubuntu-server-focal"<br> sku = "20_04-lts"<br> version = "latest"<br>}</pre> | no |
78+
| <a name="input_identity_enabled"></a> [identity\_enabled](#input\_identity\_enabled)| Boolean flag than enables creation of System Assigned identity to VM | `bool` | false | no |
79+
| <a name="input_password_access_enabled"></a> [password\_access\_enabled](#input\_password\_access\_enabled)| Boolean flag that enables access using password | `bool` | false | no |
80+
81+
## Modules
82+
83+
No modules.
84+
85+
## Resources
86+
87+
| Name | Type |
88+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
89+
| [azurerm_public_ip.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) | resource |
90+
| [azurerm_network_interface.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) | resource |
91+
| [azurerm_linux_virtual_machine.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) | resource |
92+
93+
94+
## Outputs
795

96+
| Name | Description |
97+
| ----------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
98+
| <a name="output_id"></a> [id](#output\_id) | The ID of the Linux Virtual Machine |
99+
| <a name="output_identity"></a> [identity](#output\_identity) | linux virtual machine identity |
100+
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | Linux Virtual Machine public IP address |
8101
<!-- END_TF_DOCS -->
9102

10103
## License
11104

12-
Apache 2 Licensed. For more information please see [LICENSE](https://github.com/data-platform-hq/terraform-azurerm<>/tree/master/LICENSE)
105+
Apache 2 Licensed. For more information please see [LICENSE](https://github.com/data-platform-hq/terraform-azurerm-linux-vm/blob/main/LICENSE)

main.tf

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
locals {
2+
suffix = length(var.suffix) == 0 ? "" : "-${var.suffix}"
3+
virtual_machine_name = var.custom_virtual_machine_name == null ? "vm-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_virtual_machine_name}${local.suffix}"
4+
network_interface_name = var.custom_network_interface_name == null ? "nic-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_network_interface_name}${local.suffix}"
5+
public_ip = var.custom_public_ip_name == null ? "ip-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_public_ip_name}${local.suffix}"
6+
}
7+
8+
resource "azurerm_public_ip" "this" {
9+
count = var.public_ip_enabled ? 1 : 0
10+
11+
name = local.public_ip
12+
resource_group_name = var.resource_group
13+
location = var.location
14+
allocation_method = var.public_ip_allocation_method
15+
}
16+
17+
resource "azurerm_network_interface" "this" {
18+
name = local.network_interface_name
19+
location = var.location
20+
resource_group_name = var.resource_group
21+
tags = var.tags
22+
23+
ip_configuration {
24+
name = "ip-config-${var.project}-${var.env}-${var.location}"
25+
subnet_id = var.subnet_id
26+
private_ip_address_allocation = var.network_interface_private_ip_address_allocation
27+
public_ip_address_id = try(azurerm_public_ip.this[0].id, null)
28+
}
29+
}
30+
31+
resource "azurerm_linux_virtual_machine" "this" {
32+
name = local.virtual_machine_name
33+
resource_group_name = var.resource_group
34+
location = var.location
35+
size = var.vm_size
36+
admin_username = var.vm_admin_username
37+
tags = var.tags
38+
network_interface_ids = [azurerm_network_interface.this.id]
39+
admin_password = var.vm_admin_password
40+
disable_password_authentication = var.password_access_enabled ? false : true
41+
42+
dynamic "identity" {
43+
for_each = var.identity_enabled ? [1] : []
44+
45+
content {
46+
type = "SystemAssigned"
47+
}
48+
}
49+
50+
dynamic "admin_ssh_key" {
51+
for_each = var.password_access_enabled ? [] : [1]
52+
53+
content {
54+
username = var.admin_ssh_key.username
55+
public_key = var.admin_ssh_key.public_key
56+
}
57+
}
58+
59+
os_disk {
60+
caching = var.os_disk.caching
61+
storage_account_type = var.os_disk.storage_account_type
62+
}
63+
64+
source_image_reference {
65+
publisher = var.source_image_reference.publisher
66+
offer = var.source_image_reference.offer
67+
sku = var.source_image_reference.sku
68+
version = var.source_image_reference.version
69+
}
70+
}

outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "id" {
2+
value = azurerm_linux_virtual_machine.this.id
3+
description = "The ID of the Linux Virtual Machine"
4+
}
5+
6+
output "identity" {
7+
value = try(azurerm_linux_virtual_machine.this.identity, [])
8+
description = "linux virtual machine Identities list"
9+
}
10+
11+
output "public_ip" {
12+
value = try(azurerm_public_ip.this[0].ip_address, null)
13+
description = "Linux Virtual Machine public IP address"
14+
}

variables.tf

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
variable "project" {
2+
type = string
3+
description = "Project name"
4+
}
5+
6+
variable "env" {
7+
type = string
8+
description = "Environment name"
9+
}
10+
11+
variable "resource_group" {
12+
type = string
13+
description = "The name of the resource group"
14+
}
15+
16+
variable "suffix" {
17+
type = string
18+
description = "Optional suffix that would be added to the end of resources names. It is recommended to use dash at the beginning of variable (e.x., '-example')"
19+
default = ""
20+
}
21+
22+
variable "location" {
23+
type = string
24+
description = "Azure location"
25+
}
26+
27+
variable "custom_virtual_machine_name" {
28+
type = string
29+
description = "Specifies the name of the virtual machine name resource"
30+
default = null
31+
}
32+
33+
variable "custom_network_interface_name" {
34+
type = string
35+
description = "Specifies the name of the virtual machine interface name resource"
36+
default = null
37+
}
38+
39+
variable "custom_public_ip_name" {
40+
type = string
41+
description = "Specifies the name of the public ip name name resource"
42+
default = null
43+
}
44+
45+
variable "tags" {
46+
type = map(any)
47+
description = "Resource tags"
48+
default = {}
49+
}
50+
51+
variable "subnet_id" {
52+
type = string
53+
description = "The ID of the Subnet where this Network Interface should be located in."
54+
}
55+
56+
variable "public_ip_enabled" {
57+
type = bool
58+
description = "Boolean flag to enable Public Ip address creation and assignment to Virtual Machine"
59+
default = true
60+
}
61+
62+
variable "public_ip_allocation_method" {
63+
type = string
64+
description = "Defines the allocation method for this IP address. Possible values are Static or Dynamic"
65+
default = "Dynamic"
66+
}
67+
68+
variable "network_interface_private_ip_address_allocation" {
69+
type = string
70+
description = "The allocation method used for the Private IP Address."
71+
default = "Dynamic"
72+
}
73+
74+
variable "vm_size" {
75+
type = string
76+
description = "The SKU which should be used for this Virtual Machine."
77+
default = "Standard_F2"
78+
}
79+
80+
variable "vm_admin_username" {
81+
type = string
82+
description = "The username of the local administrator used for the Virtual Machine."
83+
default = "adminuser"
84+
}
85+
86+
variable "vm_admin_password" {
87+
type = string
88+
description = "The password of the local administrator used for the Virtual Machine."
89+
default = null
90+
}
91+
92+
variable "admin_ssh_key" {
93+
type = object({
94+
username = optional(string, "adminuser")
95+
public_key = optional(string)
96+
})
97+
description = "Objects to configure ssh key reference for virtual machine"
98+
}
99+
100+
variable "os_disk" {
101+
type = object({
102+
caching = optional(string, "ReadWrite")
103+
storage_account_type = optional(string, "Standard_LRS")
104+
})
105+
description = "Objects to configure os disk reference for virtual machine"
106+
}
107+
108+
variable "source_image_reference" {
109+
type = object({
110+
publisher = optional(string, "Canonical")
111+
offer = optional(string, "0001-com-ubuntu-server-focal")
112+
sku = optional(string, "20_04-lts")
113+
version = optional(string, "latest")
114+
})
115+
description = "Objects to configure source image reference for virtual machine"
116+
}
117+
118+
variable "identity_enabled" {
119+
type = bool
120+
description = "Boolean flag than enables creation of System Assigned identity to VM"
121+
default = false
122+
}
123+
124+
variable "password_access_enabled" {
125+
type = bool
126+
description = "Boolean flag that enables access using password"
127+
default = false
128+
}

versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">=1.0.0"
3+
4+
required_providers {
5+
azurerm = {
6+
source = "hashicorp/azurerm"
7+
version = ">=3.40.0"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)