-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d9ac5a7
Showing
3 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
name: Terraform Validate | ||
|
||
run-name: ${{ github.actor }} - ${{ github.ref_name }} | ||
|
||
on: | ||
pull_request: | ||
branches: ["main"] | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
terraform_validate: | ||
name: "Format and Validate Code" | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Terraform | ||
uses: hashicorp/setup-terraform@v3 | ||
with: | ||
terraform_version: 1.8.4 | ||
|
||
- name: Terraform Fmt | ||
run: terraform fmt -check -recursive -diff | ||
|
||
- name: Check count function | ||
run: | | ||
if ! grep -Eq 'count\s+=' network.tf; then | ||
echo "Count function not found in network.tf!" | ||
exit 1 | ||
fi | ||
echo "Count function is present." | ||
- name: Check for_each function | ||
run: | | ||
if ! grep -Eq 'for_each\s+=' vmss.tf; then | ||
echo "For_each function not found in vmss.tf!" | ||
exit 1 | ||
fi | ||
echo "For_each function is present." | ||
- name: Check lifecycle block | ||
run: | | ||
if ! grep -q 'lifecycle {' ./*.tf; then | ||
echo "lifecycle block not found!" | ||
exit 1 | ||
fi | ||
echo "lifecycle block is present." | ||
- name: Check dynamic blocks | ||
run: | | ||
if ! grep -q 'dynamic "security_rule"' ./*.tf; then | ||
echo "dynamic blocks for network security rules not found!" | ||
exit 1 | ||
fi | ||
echo "dynamic blocks are present." | ||
- name: Check built-in functions usage | ||
run: | | ||
if ! grep -q 'upper(' ./*.tf || ! grep -q 'join(' ./*.tf || ! grep -q '\[for' ./*.tf; then | ||
echo "built-in functions not used correctly!" | ||
exit 1 | ||
fi | ||
echo "built-in functions are present." | ||
- name: Terraform Init | ||
run: terraform init | ||
|
||
- name: Terraform Validate | ||
run: terraform validate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# HCL Language Features with Terraform | ||
|
||
This task involves utilizing various HCL language features such as count, for_each, Terraform lifecycle, dynamic blocks, and built-in functions. | ||
|
||
## Prerequisites | ||
|
||
- Basic understanding of Terraform and Azure. | ||
- Terraform installed on your machine. | ||
- Azure CLI installed and configured. | ||
|
||
## Hands-on Task | ||
|
||
1. Fork this repository. | ||
2. Create multiple instances of a resource using the `count` meta-argument. | ||
3. Create multiple network interfaces using the `for_each` meta-argument. | ||
* Add a resource block that creates multiple network interfaces using `for_each`. | ||
* Use a local value to define a list of network interface names. | ||
* Iterate over the list using `for_each` to create the resources. | ||
4. Add a lifecycle block to prevent accidental virtual machine resource deletion. | ||
5. Add dynamic blocks for network security rules. | ||
* Define a list of network security rules in a local value. | ||
* Use a dynamic block to iterate over the list and create the security rules. | ||
6. Use built-in functions to manipulate strings and other data. | ||
* Create an output block that converts the virtual machine name to uppercase. | ||
* Create an output block that joins multiple tag values into a single string. | ||
* Create an output block that uses a for loop to get the IDs of all virtual machines. | ||
7. Organize the Terraform code into multiple files for better readability and maintenance. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
terraform { | ||
required_providers { | ||
azurerm = { | ||
source = "hashicorp/azurerm" | ||
version = "3.105.0" | ||
} | ||
} | ||
} | ||
|
||
provider "azurerm" { | ||
features {} | ||
} | ||
|
||
variable "prefix" { | ||
default = "tfvmex" | ||
} | ||
|
||
resource "azurerm_resource_group" "example" { | ||
name = "${var.prefix}-resources" | ||
location = "West Europe" | ||
} | ||
|
||
resource "azurerm_virtual_network" "main" { | ||
name = "${var.prefix}-network" | ||
address_space = ["10.0.0.0/16"] | ||
location = azurerm_resource_group.example.location | ||
resource_group_name = azurerm_resource_group.example.name | ||
} | ||
|
||
resource "azurerm_subnet" "internal" { | ||
name = "internal" | ||
resource_group_name = azurerm_resource_group.example.name | ||
virtual_network_name = azurerm_virtual_network.main.name | ||
address_prefixes = ["10.0.2.0/24"] | ||
} | ||
|
||
resource "azurerm_network_interface" "main" { | ||
name = "${var.prefix}-nic" | ||
location = azurerm_resource_group.example.location | ||
resource_group_name = azurerm_resource_group.example.name | ||
|
||
ip_configuration { | ||
name = "testconfiguration1" | ||
subnet_id = azurerm_subnet.internal.id | ||
private_ip_address_allocation = "Dynamic" | ||
} | ||
} | ||
|
||
resource "azurerm_virtual_machine" "main" { | ||
name = "${var.prefix}-vm" | ||
location = azurerm_resource_group.example.location | ||
resource_group_name = azurerm_resource_group.example.name | ||
network_interface_ids = [azurerm_network_interface.main.id] | ||
vm_size = "Standard_DS1_v2" | ||
|
||
storage_image_reference { | ||
publisher = "Canonical" | ||
offer = "0001-com-ubuntu-server-jammy" | ||
sku = "22_04-lts" | ||
version = "latest" | ||
} | ||
storage_os_disk { | ||
name = "myosdisk1" | ||
caching = "ReadWrite" | ||
create_option = "FromImage" | ||
managed_disk_type = "Standard_LRS" | ||
} | ||
os_profile { | ||
computer_name = "hostname" | ||
admin_username = "testadmin" | ||
admin_password = "Password1234!" | ||
} | ||
os_profile_linux_config { | ||
disable_password_authentication = false | ||
} | ||
tags = { | ||
environment = "staging" | ||
} | ||
} |