Skip to content

Commit 513c1b0

Browse files
committed
Terraform skeleton
1 parent 782e00d commit 513c1b0

15 files changed

+187
-0
lines changed

terraform/main.tf

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## AWS
2+
module "aws" {
3+
source = "./modules/aws"
4+
count = var.enable_aws ? 1 : 0
5+
instances = var.instance_count
6+
subnet = var.aws_subnet
7+
}
8+
9+
## Azure
10+
module "azure" {
11+
source = "./modules/azure"
12+
count = var.enable_azure ? 1 : 0
13+
instances = var.instance_count
14+
resource_group_name = var.resource_group_name
15+
resource_group_location = var.resource_group_location
16+
}

terraform/modules/aws/aws.tf

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_instance" "inst" {
2+
count = var.instances
3+
ami = var.ami_id
4+
instance_type = var.instance_type
5+
subnet_id = var.subnet
6+
7+
tags = {
8+
Name = "EX42-${count.index}"
9+
}
10+
}

terraform/modules/aws/outputs.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "instance_name" {
2+
value = aws.inst.*.tags.Name
3+
}

terraform/modules/aws/variables.tf

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
variable "instances" {}
2+
3+
variable "ami_id" {
4+
type = string
5+
default = "ami-0729e439b6769d6ab"
6+
}
7+
8+
variable "instance_type" {
9+
type = string
10+
default = "t3.micro"
11+
}
12+
13+
variable "subnet" {}

terraform/modules/azure/azure.tf

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Import Resource Group
2+
resource "azurerm_resource_group" "rg" {
3+
name = var.resource_group_name
4+
location = var.resource_group_location
5+
}
6+
7+
# Create virtual network
8+
resource "azurerm_virtual_network" "vnet" {
9+
name = "Vnet"
10+
address_space = ["10.0.0.0/16"]
11+
location = azurerm_resource_group.rg.location
12+
resource_group_name = azurerm_resource_group.rg.name
13+
}
14+
15+
# Create subnet
16+
resource "azurerm_subnet" "subnet" {
17+
name = "Subnet"
18+
resource_group_name = azurerm_resource_group.rg.name
19+
virtual_network_name = azurerm_virtual_network.vnet.name
20+
address_prefixes = ["10.0.1.0/24"]
21+
}
22+
23+
24+
# Create network interface
25+
resource "azurerm_network_interface" "nic" {
26+
count = var.instances
27+
name = "NIC${count.index}"
28+
location = azurerm_resource_group.rg.location
29+
resource_group_name = azurerm_resource_group.rg.name
30+
31+
ip_configuration {
32+
name = "NicConfig"
33+
subnet_id = azurerm_subnet.subnet.id
34+
private_ip_address_allocation = "Dynamic"
35+
}
36+
}
37+
38+
# Create virtual machine
39+
resource "azurerm_linux_virtual_machine" "vm" {
40+
count = var.instances
41+
name = "EX42-${count.index}"
42+
location = azurerm_resource_group.rg.location
43+
resource_group_name = azurerm_resource_group.rg.name
44+
network_interface_ids = [azurerm_network_interface.nic[count.index].id]
45+
size = "Standard_DS1_v2"
46+
47+
admin_ssh_key {
48+
username = "admin"
49+
public_key = file("~/.ssh/id_rsa.pub")
50+
}
51+
52+
os_disk {
53+
caching = "ReadWrite"
54+
storage_account_type = "Standard_LRS"
55+
}
56+
57+
source_image_reference {
58+
publisher = "Canonical"
59+
offer = "UbuntuServer"
60+
sku = "18.04-LTS"
61+
version = "latest"
62+
}
63+
64+
computer_name = "VM-${count.index}"
65+
admin_username = "admin"
66+
admin_password = "wqe2sdf3"
67+
disable_password_authentication = false
68+
}

terraform/modules/azure/outputs.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "instance_name" {
2+
value = azurerm_linux_virtual_machine.vm.*.name
3+
}

terraform/modules/azure/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
variable "instances" {}
2+
3+
variable "resource_group_name" {}
4+
5+
variable "resource_group_location" {}
6+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

terraform/outputs.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "aws_instances" {
2+
value = module.aws.*.instance_name
3+
}
4+
5+
output "azure_instances" {
6+
value = module.azure.*.instance_name
7+
}

terraform/providers.tf

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
terraform {
2+
required_version = ">= 0.14.9"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 4.25"
8+
}
9+
10+
azurerm = {
11+
source = "hashicorp/azurerm"
12+
version = "~>3.17"
13+
}
14+
}
15+
}
16+
17+
provider "aws" {
18+
region = "eu-north-1"
19+
profile = "default"
20+
}
21+
22+
provider "azurerm" {
23+
features {}
24+
25+
skip_provider_registration = true
26+
}

terraform/variables.tf

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
variable "enable_aws" {
2+
description = "Enable / Disable AWS Deployment"
3+
type = bool
4+
default = true
5+
}
6+
7+
variable "enable_azure" {
8+
description = "Enable / Disable Azure Deployment"
9+
type = bool
10+
default = false
11+
}
12+
13+
variable "instance_count" {
14+
description = "Number of Instances"
15+
type = string
16+
default = 2
17+
}
18+
19+
variable "aws_subnet" {
20+
description = "AWS Subnet"
21+
type = string
22+
default = "subnet-01e0d908d980f33cc"
23+
}
24+
25+
variable "resource_group_name" {
26+
description = "Resource Group Name"
27+
type = string
28+
default = "1-a46ba6a8-playground-sandbox"
29+
}
30+
31+
variable "resource_group_location" {
32+
description = "Resource Group Location"
33+
type = string
34+
default = "centralus"
35+
}

0 commit comments

Comments
 (0)