Skip to content

Commit b555dbd

Browse files
authored
Merge pull request #21 from hugodeaguiar/main
Create a basic-webserver-vm module.
2 parents bc97b4d + 36901c3 commit b555dbd

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

modules/basic-webserver-vm/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Basic Webserver VM Module
2+
3+
This module creates a virtual machine on Magalu Cloud with a basic webserver configuration.
4+
5+
## Variables
6+
7+
The following variables are required:
8+
9+
* `provider_region`: The region where the VM will be created (br-ne1 or br-se1)
10+
* `vm_name`: The name of the VM
11+
* `machine_type`: The type of VM (default: cloud-bs1.xsmall)
12+
* `image`: The image for the VM (default: cloud-rocky-09)
13+
* `associate_public_ip`: Whether the VM should have a public IP (default: true)
14+
* `ssh_key`: The SSH key to use for the VM
15+
* `interface_id`: The ID of the interface to attach to the VM
16+
17+
## Outputs
18+
19+
The following output is available:
20+
21+
* `server_id`: The ID of the created server
22+
23+
## Usage
24+
25+
To use this module, create a `main.tf` file with the following content:
26+
```hcl
27+
module "basic_webserver_vm" {
28+
source = "../modules/basic-webserver-vm"
29+
30+
provider_region = "br-se1"
31+
vm_name = "my-webserver-vm"
32+
ssh_key = "my-ssh-key"
33+
}
34+
```
35+
Then, run `terraform apply` to create the VM.
36+
37+
Note: This module assumes that you have already set up a Magalu Cloud provider and have the necessary credentials.

modules/basic-webserver-vm/main.tf

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
terraform {
2+
required_providers {
3+
mgc = {
4+
source = "MagaluCloud/mgc"
5+
version = "0.31.0"
6+
}
7+
}
8+
}
9+
10+
provider "mgc" {
11+
alias = "mgc"
12+
region = var.provider_region
13+
}
14+
15+
resource "mgc_ssh_keys" "my_key" {
16+
provider = mgc
17+
name = "local"
18+
key = var.ssh_key
19+
}
20+
21+
resource "mgc_network_security_groups" "basic_security_group" {
22+
name = "basic-security-group"
23+
description = "A basic security group"
24+
disable_default_rules = false
25+
}
26+
27+
resource "mgc_network_security_groups_rules" "allow_ssh" {
28+
description = "Allow incoming SSH traffic"
29+
direction = "ingress"
30+
ethertype = "IPv4"
31+
port_range_max = 22
32+
port_range_min = 22
33+
protocol = "tcp"
34+
remote_ip_prefix = "0.0.0.0/0"
35+
security_group_id = mgc_network_security_groups.basic_security_group.id
36+
}
37+
38+
resource "mgc_network_security_groups_rules" "allow_ssh_ipv6" {
39+
description = "Allow incoming SSH traffic from IPv6"
40+
direction = "ingress"
41+
ethertype = "IPv6"
42+
port_range_max = 22
43+
port_range_min = 22
44+
protocol = "tcp"
45+
remote_ip_prefix = "::/0"
46+
security_group_id = mgc_network_security_groups.basic_security_group.id
47+
}
48+
49+
resource "mgc_network_security_groups_rules" "allow_http" {
50+
description = "Allow incoming HTTP traffic"
51+
direction = "ingress"
52+
ethertype = "IPv4"
53+
port_range_max = 80
54+
port_range_min = 80
55+
protocol = "tcp"
56+
remote_ip_prefix = "0.0.0.0/0"
57+
security_group_id = mgc_network_security_groups.basic_security_group.id
58+
}
59+
60+
resource "mgc_network_security_groups_rules" "allow_http_ipv6" {
61+
description = "Allow incoming HTTP traffic from IPv6"
62+
direction = "ingress"
63+
ethertype = "IPv6"
64+
port_range_max = 80
65+
port_range_min = 80
66+
protocol = "tcp"
67+
remote_ip_prefix = "::/0"
68+
security_group_id = mgc_network_security_groups.basic_security_group.id
69+
}
70+
71+
resource "mgc_network_security_groups_rules" "allow_https" {
72+
description = "Allow incoming HTTPS traffic"
73+
direction = "ingress"
74+
ethertype = "IPv4"
75+
port_range_max = 443
76+
port_range_min = 443
77+
protocol = "tcp"
78+
remote_ip_prefix = "0.0.0.0/0"
79+
security_group_id = mgc_network_security_groups.basic_security_group.id
80+
}
81+
82+
resource "mgc_network_security_groups_rules" "allow_https_ipv6" {
83+
description = "Allow incoming HTTPS traffic from IPv6"
84+
direction = "ingress"
85+
ethertype = "IPv6"
86+
port_range_max = 443
87+
port_range_min = 443
88+
protocol = "tcp"
89+
remote_ip_prefix = "::/0"
90+
security_group_id = mgc_network_security_groups.basic_security_group.id
91+
}
92+
93+
resource "mgc_virtual_machine_instances" "server" {
94+
provider = mgc.mgc
95+
name = var.vm_name
96+
97+
machine_type = {
98+
name = var.machine_type
99+
}
100+
101+
image = {
102+
name = var.image
103+
}
104+
105+
network = {
106+
associate_public_ip = var.associate_public_ip
107+
interface = {
108+
security_groups = [{ id = mgc_network_security_groups.basic_security_group.id }]
109+
}
110+
}
111+
112+
ssh_key_name = mgc_ssh_keys.my_key.name
113+
}
114+
115+
resource "mgc_network_security_groups_attach" "attach_sg_to_interface" {
116+
security_group_id = mgc_network_security_groups.basic_security_group.id
117+
interface_id = var.interface_id
118+
}

modules/basic-webserver-vm/output.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "server_id" {
2+
description = "The ID of the created server."
3+
value = mgc_virtual_machine_instances.server.id
4+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
variable "provider_region" {
3+
description = "Region (br-ne1 or br-se1)"
4+
type = string
5+
default = "br-se1"
6+
}
7+
8+
variable "vm_name" {
9+
description = "VM Name"
10+
type = string
11+
}
12+
13+
variable "machine_type" {
14+
description = "Type of VM"
15+
type = string
16+
default = "cloud-bs1.xsmall"
17+
}
18+
19+
variable "image" {
20+
description = "Image for VM"
21+
type = string
22+
default = "cloud-rocky-09"
23+
}
24+
25+
variable "associate_public_ip" {
26+
description = "VM with public IP"
27+
type = bool
28+
default = true
29+
}
30+
31+
variable "ssh_key" {
32+
description = "SSH Key"
33+
type = string
34+
}
35+
36+
variable "interface_id" {
37+
description = "Interface ID"
38+
type = string
39+
}

0 commit comments

Comments
 (0)