Skip to content

Commit

Permalink
Add devops-toolkit demo create Azure VM via terraform (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungbq authored Sep 1, 2024
1 parent 9931c40 commit b47383f
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Ignore transient lock info files created by terraform apply
.terraform.tfstate.lock.info

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ Explore the comprehensive guide below to gain insight into the detailed utilizat
- For detailed instructions on using specific tools, refer to: [**DevOps toolkit specific tool user guide**](./docs/usage/README.md)
- For instructions on common run modes, visit [**DevOps toolkit common run mode**](./docs/usage/run_mode.md)

## Demo

- Checkout the Demo code and instruction [here](./demo/).

## The DevOps Toolkit Core 🧰

Built on `ubuntu:22.04` base image
Expand Down
15 changes: 15 additions & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# DevOps Toolkit demo

## Quick demo

```bash
docker run --network host --rm -v ~/.dtc:/dtc tungbq/devops-toolkit:latest samples/run_sample.sh
```

## Demo Table

Keeps track of all demo resources

| Tool | Demo link | Status |
| :-------: | --------------------------------------------- | :------ |
| Terraform | [provistion-azure-vm](./provistion-azure-vm/) | ✔️ Done |
61 changes: 61 additions & 0 deletions demo/provistion-azure-vm/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions demo/provistion-azure-vm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# DEMO: Provision Azure VM with DevOps toolkit

## Navigate to Terraform demo code

```bash
cd devops-toolkit/demo/provistion-azure-vm
```

## Init devops-toolkit container

```bash
devops-toolkit-cli init demo_azure_tf
```

## Run devops-toolkit container

```bash
devops-toolkit-cli run demo_azure_tf
```

At this point, your container already started with `demo/provistion-azure-vm` code available inside. Let's move to the next step

## Login to Azure

```bash
# In devops-toolkit contaienr env
az account show
az login --use-device-code
# Follow the cmd output to login
```

## Initialize Terraform

```bash
# In devops-toolkit contaienr env
terraform init
```

## Plan Terraform

```bash
# In devops-toolkit contaienr env
terraform plan
```

## Apply Terraform

```bash
# In devops-toolkit contaienr env
terraform apply
```

Enter 'yes' to confirm

## Verify

- Now you can go to Azure Portal to verify your VM: https://portal.azure.com/#home
- Or ping the Public IP shown in the console

## Destroy Terraform

```bash
# In devops-toolkit contaienr env
terraform destroy
```

## Conclusion

You can see that in just a few steps, you can use Terraform to provision the Azure VM without install terraform on the host machine!
124 changes: 124 additions & 0 deletions demo/provistion-azure-vm/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
resource "random_pet" "rg_name" {
prefix = var.resource_group_name_prefix
}

resource "azurerm_resource_group" "rg" {
location = var.resource_group_location
name = random_pet.rg_name.id
}

# Create virtual network
resource "azurerm_virtual_network" "my_terraform_network" {
name = "myVnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}

# Create subnet
resource "azurerm_subnet" "my_terraform_subnet" {
name = "mySubnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.my_terraform_network.name
address_prefixes = ["10.0.1.0/24"]
}

# Create public IPs
resource "azurerm_public_ip" "my_terraform_public_ip" {
name = "myPublicIP"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Dynamic"
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "my_terraform_nsg" {
name = "myNetworkSecurityGroup"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}

# Create network interface
resource "azurerm_network_interface" "my_terraform_nic" {
name = "myNIC"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

ip_configuration {
name = "my_nic_configuration"
subnet_id = azurerm_subnet.my_terraform_subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.my_terraform_public_ip.id
}
}

# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
network_interface_id = azurerm_network_interface.my_terraform_nic.id
network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id
}

# Generate random text for a unique storage account name
resource "random_id" "random_id" {
keepers = {
# Generate a new ID only when a new resource group is defined
resource_group = azurerm_resource_group.rg.name
}

byte_length = 8
}

# Create storage account for boot diagnostics
resource "azurerm_storage_account" "my_storage_account" {
name = "diag${random_id.random_id.hex}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
account_tier = "Standard"
account_replication_type = "LRS"
}

# Create virtual machine
resource "azurerm_linux_virtual_machine" "my_terraform_vm" {
name = "myVM"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
network_interface_ids = [azurerm_network_interface.my_terraform_nic.id]
size = "Standard_DS1_v2"

os_disk {
name = "myOsDisk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-gen2"
version = "latest"
}

computer_name = "hostname"
admin_username = var.username

admin_ssh_key {
username = var.username
public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey
}

boot_diagnostics {
storage_account_uri = azurerm_storage_account.my_storage_account.primary_blob_endpoint
}
}
7 changes: 7 additions & 0 deletions demo/provistion-azure-vm/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}

output "public_ip_address" {
value = azurerm_linux_virtual_machine.my_terraform_vm.public_ip_address
}
22 changes: 22 additions & 0 deletions demo/provistion-azure-vm/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
terraform {
required_version = ">=0.12"

required_providers {
azapi = {
source = "azure/azapi"
version = "~>1.5"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}

provider "azurerm" {
features {}
}
24 changes: 24 additions & 0 deletions demo/provistion-azure-vm/ssh.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "random_pet" "ssh_key_name" {
prefix = "ssh"
separator = ""
}

resource "azapi_resource_action" "ssh_public_key_gen" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
resource_id = azapi_resource.ssh_public_key.id
action = "generateKeyPair"
method = "POST"

response_export_values = ["publicKey", "privateKey"]
}

resource "azapi_resource" "ssh_public_key" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
name = random_pet.ssh_key_name.id
location = azurerm_resource_group.rg.location
parent_id = azurerm_resource_group.rg.id
}

output "key_data" {
value = azapi_resource_action.ssh_public_key_gen.output.publicKey
}
17 changes: 17 additions & 0 deletions demo/provistion-azure-vm/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variable "resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}

variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}

variable "username" {
type = string
description = "The username for the local account that will be created on the new VM."
default = "azureadmin"
}

0 comments on commit b47383f

Please sign in to comment.