Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial terraform config for azure arm VM #14

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
#
# SPDX-License-Identifier: Apache-2.0

# Nix
result
result-*

# Terraform
.terraform
.terraform.*
terraform.tfstate
terraform.tfstate.backup
.idea
.idea
.direnv
10 changes: 8 additions & 2 deletions terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ If you still don't have nix package manager on your local host, install it follo
Then, clone this repository:
```bash
$ git clone https://github.com/tiiuae/ghaf-infra.git
$ cd ghaf-infra
$ cd ghaf-infra/
```

All commands in this document are executed from nix-shell inside the `terraform` directory.
Expand All @@ -44,7 +44,7 @@ This project stores the terraform state in a remote storage in an azure storage

When starting a new infrastructure you need to initialize the terraform state storage:
```bash
$ cd azure-storage
$ cd azure-storage/
$ terraform init
$ terraform apply
```
Expand All @@ -56,6 +56,12 @@ Following describes the intended workflow, with commands executed from the nix-s
First, change the terraform code by modifying the relevant files in this directory. Then:

```bash
# Terraform comands are executed under the terraform directory:
$ cd terraform/

# Initialize terraform working directory
$ terraform init

# Format the terraform code files:
$ terraform fmt

Expand Down
138 changes: 93 additions & 45 deletions terraform/azure-ghaf-infra.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ terraform {
source = "carlpett/sops"
}
}
# Backend for storing tfstate (see ./azure-storage)
backend "azurerm" {
resource_group_name = "ghaf-infra-storage"
storage_account_name = "ghafinfrastatestorage"
container_name = "ghaf-infra-tfstate-container"
key = "ghaf-infra.tfstate"
}
}
provider "azurerm" {
features {}
Expand All @@ -19,19 +26,10 @@ provider "azurerm" {
data "sops_file" "ghaf_infra" {
source_file = "secrets.yaml"
}
# Backend for storing tfstate (see ./azure-storage)
terraform {
backend "azurerm" {
resource_group_name = "ghaf-infra-storage"
storage_account_name = "ghafinfrastatestorage"
container_name = "ghaf-infra-tfstate-container"
key = "ghaf-infra.tfstate"
}
}
# Resource group
resource "azurerm_resource_group" "ghaf_infra_tf_dev" {
name = "ghaf-infra-tf-dev"
location = "swedencentral"
location = "northeurope"
henrirosten marked this conversation as resolved.
Show resolved Hide resolved
}
# Virtual Network
resource "azurerm_virtual_network" "ghaf_infra_tf_vnet" {
Expand All @@ -47,34 +45,7 @@ resource "azurerm_subnet" "ghaf_infra_tf_subnet" {
virtual_network_name = azurerm_virtual_network.ghaf_infra_tf_vnet.name
address_prefixes = ["10.0.2.0/24"]
}
# Network interface
resource "azurerm_network_interface" "ghaf_infra_tf_network_interface" {
name = "ghaf-infratf286-z1"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
ip_configuration {
name = "my_nic_configuration"
subnet_id = azurerm_subnet.ghaf_infra_tf_subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.ghaf_infra_tf_public_ip.id
}
}
# Availability Set
resource "azurerm_availability_set" "ghaf_infra_tf_availability_set" {
name = "ghaf-infra-tf-availability-set"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
platform_fault_domain_count = 2
platform_update_domain_count = 2
}
# Public IPs
resource "azurerm_public_ip" "ghaf_infra_tf_public_ip" {
name = "ghaf-infra-tf-public-ip"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
allocation_method = "Dynamic"
}
# Network Security Group and Rule
# Network Security Group
resource "azurerm_network_security_group" "ghaf_infra_tf_nsg" {
name = "ghaf-infra-tf-nsg"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
Expand All @@ -91,18 +62,41 @@ resource "azurerm_network_security_group" "ghaf_infra_tf_nsg" {
destination_address_prefix = "*"
}
}
# Example Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "ghafinfra_tf" {
name = "ghafinfratf"

################################################################################

# testhost

# Public IP
resource "azurerm_public_ip" "testhost_public_ip" {
name = "testhost-public-ip"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
allocation_method = "Static"
}
# Network interface
resource "azurerm_network_interface" "testhost_ni" {
name = "testhost-nic"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
ip_configuration {
name = "testhost_nic_configuration"
subnet_id = azurerm_subnet.ghaf_infra_tf_subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.testhost_public_ip.id
}
}
# Example Linux Virtual Machine (testhost)
resource "azurerm_linux_virtual_machine" "testhost_vm" {
name = "testhost"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
availability_set_id = azurerm_availability_set.ghaf_infra_tf_availability_set.id
network_interface_ids = [
azurerm_network_interface.ghaf_infra_tf_network_interface.id
azurerm_network_interface.testhost_ni.id
]
size = "Standard_B8ms"
os_disk {
name = "ghafinfratfdisk1"
name = "testhost-disk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
disk_size_gb = 512
Expand All @@ -121,4 +115,58 @@ resource "azurerm_linux_virtual_machine" "ghafinfra_tf" {
# https://learn.microsoft.com/troubleshoot/azure/virtual-machines/ed25519-ssh-keys
public_key = data.sops_file.ghaf_infra.data["vm_admin_rsa_pub"]
}
}
}

################################################################################

# azarm

# Public IP
resource "azurerm_public_ip" "azarm_public_ip" {
name = "azarm-public-ip"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
allocation_method = "Static"
}
# Network interface
resource "azurerm_network_interface" "azarm_ni" {
name = "azarm-nic"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
ip_configuration {
name = "azarm_nic_configuration"
subnet_id = azurerm_subnet.ghaf_infra_tf_subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.azarm_public_ip.id
}
}
# Azure arm builder (azarm)
resource "azurerm_linux_virtual_machine" "azarm_vm" {
name = "azarm"
location = azurerm_resource_group.ghaf_infra_tf_dev.location
resource_group_name = azurerm_resource_group.ghaf_infra_tf_dev.name
network_interface_ids = [
azurerm_network_interface.azarm_ni.id
]
size = "Standard_D8ps_v5"
os_disk {
name = "azarm-disk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
disk_size_gb = 512
}
source_image_reference {
publisher = "canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-arm64"
version = "latest"
}
admin_username = data.sops_file.ghaf_infra.data["vm_admin_name"]
disable_password_authentication = true
admin_ssh_key {
username = data.sops_file.ghaf_infra.data["vm_admin_name"]
public_key = data.sops_file.ghaf_infra.data["vm_admin_rsa_pub"]
}
}

################################################################################
2 changes: 1 addition & 1 deletion terraform/azure-storage/tfstate-storage.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resource "azurerm_resource_group" "rg" {
}


# Create storage container
# Storage container

resource "azurerm_storage_account" "tfstate" {
name = "ghafinfrastatestorage"
Expand Down