Skip to content

Commit 8aac692

Browse files
committed
Azure TF Cloud
1 parent f077d68 commit 8aac692

File tree

6 files changed

+265
-0
lines changed

6 files changed

+265
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "Terraform-Apply-Infra-Release"
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
terraform:
10+
name: "Terraform"
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
17+
- name: Setup Terraform
18+
uses: hashicorp/setup-terraform@v1
19+
with:
20+
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
21+
22+
- name: Terraform Init
23+
id: init
24+
run: terraform init
25+
26+
- name: Terraform Plan
27+
id: plan
28+
if: github.event_name == 'push'
29+
run: terraform plan -no-color
30+
continue-on-error: true
31+
32+
- name: Terraform Plan Status
33+
if: steps.plan.outcome == 'failure'
34+
run: exit 1
35+
36+
- name: Terraform Apply
37+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
38+
run: terraform apply -auto-approve
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: "Terraform-Plan-Infra-Plan"
2+
3+
on:
4+
push:
5+
branches:
6+
- FeatureBranch1
7+
8+
jobs:
9+
terraform:
10+
name: "Terraform"
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
17+
- name: Setup Terraform
18+
uses: hashicorp/setup-terraform@v1
19+
with:
20+
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
21+
22+
- name: Terraform Init
23+
id: init
24+
run: terraform init
25+
26+
- name: Terraform Plan
27+
id: plan
28+
if: github.event_name == 'push'
29+
run: terraform plan -no-color
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "azurerm_resource_group" "demo" {
2+
name = "example-resources"
3+
location = "West Europe"
4+
}
5+
6+
## Demo now
7+
resource "azurerm_storage_account" "StorageAccountDemo" {
8+
name = "satestant000012"
9+
resource_group_name = azurerm_resource_group.demo.name
10+
location = azurerm_resource_group.demo.location
11+
account_tier = "Standard"
12+
account_replication_type = "GRS"
13+
14+
tags = {
15+
video = "azure"
16+
channel = "CloudQuickLabs"
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
terraform {
2+
required_version = ">= 1.1.0"
3+
required_providers {
4+
azurerm = {
5+
source = "hashicorp/azurerm"
6+
version = "~> 3.0.2"
7+
}
8+
}
9+
cloud {
10+
organization = "CloudQuickLabs"
11+
workspaces {
12+
name = "AzureLabs"
13+
}
14+
}
15+
}
16+
17+
provider "azurerm" {
18+
features {}
19+
}

Azure_VM_Python/provision_vm.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Import the needed credential and management objects from the libraries.
2+
from azure.identity import AzureCliCredential
3+
from azure.mgmt.resource import ResourceManagementClient
4+
from azure.mgmt.network import NetworkManagementClient
5+
from azure.mgmt.compute import ComputeManagementClient
6+
import os
7+
8+
print(f"Provisioning a virtual machine...some operations might take a minute or two.")
9+
10+
# Acquire a credential object using CLI-based authentication.
11+
credential = AzureCliCredential()
12+
13+
# Retrieve subscription ID from environment variable.
14+
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
15+
16+
17+
# Step 1: Provision a resource group
18+
19+
# Obtain the management object for resources, using the credentials from the CLI login.
20+
resource_client = ResourceManagementClient(credential, subscription_id)
21+
22+
# Constants we need in multiple places: the resource group name and the region
23+
# in which we provision resources. You can change these values however you want.
24+
RESOURCE_GROUP_NAME = "PythonAzureExample-VM-rg"
25+
LOCATION = "westus2"
26+
27+
# Provision the resource group.
28+
rg_result = resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME,
29+
{
30+
"location": LOCATION
31+
}
32+
)
33+
34+
35+
print(f"Provisioned resource group {rg_result.name} in the {rg_result.location} region")
36+
37+
# For details on the previous code, see Example: Provision a resource group
38+
# at https://docs.microsoft.com/azure/developer/python/azure-sdk-example-resource-group
39+
40+
41+
# Step 2: provision a virtual network
42+
43+
# A virtual machine requires a network interface client (NIC). A NIC requires
44+
# a virtual network and subnet along with an IP address. Therefore we must provision
45+
# these downstream components first, then provision the NIC, after which we
46+
# can provision the VM.
47+
48+
# Network and IP address names
49+
VNET_NAME = "python-example-vnet"
50+
SUBNET_NAME = "python-example-subnet"
51+
IP_NAME = "python-example-ip"
52+
IP_CONFIG_NAME = "python-example-ip-config"
53+
NIC_NAME = "python-example-nic"
54+
55+
# Obtain the management object for networks
56+
network_client = NetworkManagementClient(credential, subscription_id)
57+
58+
# Provision the virtual network and wait for completion
59+
poller = network_client.virtual_networks.begin_create_or_update(RESOURCE_GROUP_NAME,
60+
VNET_NAME,
61+
{
62+
"location": LOCATION,
63+
"address_space": {
64+
"address_prefixes": ["10.0.0.0/16"]
65+
}
66+
}
67+
)
68+
69+
vnet_result = poller.result()
70+
71+
print(f"Provisioned virtual network {vnet_result.name} with address prefixes {vnet_result.address_space.address_prefixes}")
72+
73+
# Step 3: Provision the subnet and wait for completion
74+
poller = network_client.subnets.begin_create_or_update(RESOURCE_GROUP_NAME,
75+
VNET_NAME, SUBNET_NAME,
76+
{ "address_prefix": "10.0.0.0/24" }
77+
)
78+
subnet_result = poller.result()
79+
80+
print(f"Provisioned virtual subnet {subnet_result.name} with address prefix {subnet_result.address_prefix}")
81+
82+
# Step 4: Provision an IP address and wait for completion
83+
poller = network_client.public_ip_addresses.begin_create_or_update(RESOURCE_GROUP_NAME,
84+
IP_NAME,
85+
{
86+
"location": LOCATION,
87+
"sku": { "name": "Standard" },
88+
"public_ip_allocation_method": "Static",
89+
"public_ip_address_version" : "IPV4"
90+
}
91+
)
92+
93+
ip_address_result = poller.result()
94+
95+
print(f"Provisioned public IP address {ip_address_result.name} with address {ip_address_result.ip_address}")
96+
97+
# Step 5: Provision the network interface client
98+
poller = network_client.network_interfaces.begin_create_or_update(RESOURCE_GROUP_NAME,
99+
NIC_NAME,
100+
{
101+
"location": LOCATION,
102+
"ip_configurations": [ {
103+
"name": IP_CONFIG_NAME,
104+
"subnet": { "id": subnet_result.id },
105+
"public_ip_address": {"id": ip_address_result.id }
106+
}]
107+
}
108+
)
109+
110+
nic_result = poller.result()
111+
112+
print(f"Provisioned network interface client {nic_result.name}")
113+
114+
# Step 6: Provision the virtual machine
115+
116+
# Obtain the management object for virtual machines
117+
compute_client = ComputeManagementClient(credential, subscription_id)
118+
119+
VM_NAME = "ExampleVM"
120+
USERNAME = "azureuser"
121+
PASSWORD = "ChangePa$$w0rd24"
122+
123+
print(f"Provisioning virtual machine {VM_NAME}; this operation might take a few minutes.")
124+
125+
# Provision the VM specifying only minimal arguments, which defaults to an Ubuntu 18.04 VM
126+
# on a Standard DS1 v2 plan with a public IP address and a default virtual network/subnet.
127+
128+
poller = compute_client.virtual_machines.begin_create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
129+
{
130+
"location": LOCATION,
131+
"storage_profile": {
132+
"image_reference": {
133+
"publisher": 'Canonical',
134+
"offer": "UbuntuServer",
135+
"sku": "16.04.0-LTS",
136+
"version": "latest"
137+
}
138+
},
139+
"hardware_profile": {
140+
"vm_size": "Standard_DS1_v2"
141+
},
142+
"os_profile": {
143+
"computer_name": VM_NAME,
144+
"admin_username": USERNAME,
145+
"admin_password": PASSWORD
146+
},
147+
"network_profile": {
148+
"network_interfaces": [{
149+
"id": nic_result.id,
150+
}]
151+
}
152+
}
153+
)
154+
155+
vm_result = poller.result()
156+
157+
print(f"Provisioned virtual machine {vm_result.name}")

Azure_VM_Python/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
azure-mgmt-resource
2+
azure-mgmt-compute
3+
azure-mgmt-network
4+
azure-identity

0 commit comments

Comments
 (0)