This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| wiki:ai:bicep-terraform-comparison [2025/06/11 14:01] – created ddehamer | wiki:ai:bicep-terraform-comparison [2025/06/11 14:31] (current) – ddehamer | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Bicep Terraform Comparison ====== | ====== Bicep Terraform Comparison ====== | ||
| + | |||
| + | ===== Conclusion ===== | ||
| I worked on two scripts, both doing the same thing, one in Bicep and one in Terraform. | I worked on two scripts, both doing the same thing, one in Bicep and one in Terraform. | ||
| - | They both produced the exact same results. | + | They both produced the exact same results. |
| I was able to deploy the below main.bicep in one command: | I was able to deploy the below main.bicep in one command: | ||
| Line 18: | Line 20: | ||
| </ | </ | ||
| + | ===== Bicep Code ===== | ||
| + | |||
| + | main.bicep | ||
| + | < | ||
| + | param location string = resourceGroup().location | ||
| + | param adminUsername string = ' | ||
| + | @secure() | ||
| + | param adminPassword string | ||
| + | |||
| + | var vnetName = ' | ||
| + | var subnet1Name = ' | ||
| + | var subnet2Name = ' | ||
| + | var vm1Name = ' | ||
| + | var vm2Name = ' | ||
| + | var nsgName = ' | ||
| + | var routeTableName = ' | ||
| + | |||
| + | resource vnet ' | ||
| + | name: vnetName | ||
| + | location: location | ||
| + | properties: { | ||
| + | addressSpace: | ||
| + | addressPrefixes: | ||
| + | } | ||
| + | subnets: [ | ||
| + | { | ||
| + | name: subnet1Name | ||
| + | properties: { | ||
| + | addressPrefix: | ||
| + | networkSecurityGroup: | ||
| + | id: nsg.id | ||
| + | } | ||
| + | routeTable: { | ||
| + | id: routeTable.id | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | { | ||
| + | name: subnet2Name | ||
| + | properties: { | ||
| + | addressPrefix: | ||
| + | networkSecurityGroup: | ||
| + | id: nsg.id | ||
| + | } | ||
| + | routeTable: { | ||
| + | id: routeTable.id | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource nsg ' | ||
| + | name: nsgName | ||
| + | location: location | ||
| + | properties: { | ||
| + | securityRules: | ||
| + | { | ||
| + | name: ' | ||
| + | properties: { | ||
| + | priority: 1001 | ||
| + | direction: ' | ||
| + | access: ' | ||
| + | protocol: ' | ||
| + | sourcePortRange: | ||
| + | destinationPortRange: | ||
| + | sourceAddressPrefix: | ||
| + | destinationAddressPrefix: | ||
| + | } | ||
| + | } | ||
| + | { | ||
| + | name: ' | ||
| + | properties: { | ||
| + | priority: 1002 | ||
| + | direction: ' | ||
| + | access: ' | ||
| + | protocol: ' | ||
| + | sourcePortRange: | ||
| + | destinationPortRange: | ||
| + | sourceAddressPrefix: | ||
| + | destinationAddressPrefix: | ||
| + | } | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource routeTable ' | ||
| + | name: routeTableName | ||
| + | location: location | ||
| + | properties: {} | ||
| + | } | ||
| + | |||
| + | resource nic1 ' | ||
| + | name: ' | ||
| + | location: location | ||
| + | properties: { | ||
| + | ipConfigurations: | ||
| + | { | ||
| + | name: ' | ||
| + | properties: { | ||
| + | subnet: { | ||
| + | id: vnet.properties.subnets[0].id | ||
| + | } | ||
| + | privateIPAllocationMethod: | ||
| + | } | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource nic2 ' | ||
| + | name: ' | ||
| + | location: location | ||
| + | properties: { | ||
| + | ipConfigurations: | ||
| + | { | ||
| + | name: ' | ||
| + | properties: { | ||
| + | subnet: { | ||
| + | id: vnet.properties.subnets[1].id | ||
| + | } | ||
| + | privateIPAllocationMethod: | ||
| + | } | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource vm1 ' | ||
| + | name: vm1Name | ||
| + | location: location | ||
| + | properties: { | ||
| + | hardwareProfile: | ||
| + | vmSize: ' | ||
| + | } | ||
| + | osProfile: { | ||
| + | computerName: | ||
| + | adminUsername: | ||
| + | adminPassword: | ||
| + | linuxConfiguration: | ||
| + | disablePasswordAuthentication: | ||
| + | } | ||
| + | customData: base64(''' | ||
| + | #!/bin/bash | ||
| + | apt update | ||
| + | apt install -y apache2 | ||
| + | systemctl enable apache2 | ||
| + | systemctl start apache2 | ||
| + | ''' | ||
| + | } | ||
| + | storageProfile: | ||
| + | imageReference: | ||
| + | publisher: ' | ||
| + | offer: ' | ||
| + | sku: ' | ||
| + | version: ' | ||
| + | } | ||
| + | osDisk: { | ||
| + | createOption: | ||
| + | managedDisk: | ||
| + | storageAccountType: | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | networkProfile: | ||
| + | networkInterfaces: | ||
| + | { | ||
| + | id: nic1.id | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource vm2 ' | ||
| + | name: vm2Name | ||
| + | location: location | ||
| + | properties: { | ||
| + | hardwareProfile: | ||
| + | vmSize: ' | ||
| + | } | ||
| + | osProfile: { | ||
| + | computerName: | ||
| + | adminUsername: | ||
| + | adminPassword: | ||
| + | linuxConfiguration: | ||
| + | disablePasswordAuthentication: | ||
| + | } | ||
| + | customData: base64(''' | ||
| + | #!/bin/bash | ||
| + | apt update | ||
| + | apt install -y apache2 | ||
| + | systemctl enable apache2 | ||
| + | systemctl start apache2 | ||
| + | ''' | ||
| + | } | ||
| + | storageProfile: | ||
| + | imageReference: | ||
| + | publisher: ' | ||
| + | offer: ' | ||
| + | sku: ' | ||
| + | version: ' | ||
| + | } | ||
| + | osDisk: { | ||
| + | createOption: | ||
| + | managedDisk: | ||
| + | storageAccountType: | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | networkProfile: | ||
| + | networkInterfaces: | ||
| + | { | ||
| + | id: nic2.id | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | vm.bicep | ||
| + | < | ||
| + | param vmName string | ||
| + | param subnetId string | ||
| + | param location string | ||
| + | param adminUsername string | ||
| + | @secure() | ||
| + | param adminPassword string | ||
| + | |||
| + | resource nic ' | ||
| + | name: ' | ||
| + | location: location | ||
| + | properties: { | ||
| + | ipConfigurations: | ||
| + | { | ||
| + | name: ' | ||
| + | properties: { | ||
| + | subnet: { | ||
| + | id: subnetId | ||
| + | } | ||
| + | privateIPAllocationMethod: | ||
| + | } | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource vm ' | ||
| + | name: vmName | ||
| + | location: location | ||
| + | properties: { | ||
| + | hardwareProfile: | ||
| + | vmSize: ' | ||
| + | } | ||
| + | osProfile: { | ||
| + | computerName: | ||
| + | adminUsername: | ||
| + | adminPassword: | ||
| + | linuxConfiguration: | ||
| + | disablePasswordAuthentication: | ||
| + | } | ||
| + | customData: base64(' | ||
| + | #!/bin/bash | ||
| + | apt update | ||
| + | apt install -y apache2 | ||
| + | systemctl enable apache2 | ||
| + | systemctl start apache2 | ||
| + | ') | ||
| + | } | ||
| + | storageProfile: | ||
| + | imageReference: | ||
| + | publisher: ' | ||
| + | offer: ' | ||
| + | sku: ' | ||
| + | version: ' | ||
| + | } | ||
| + | osDisk: { | ||
| + | createOption: | ||
| + | managedDisk: | ||
| + | storageAccountType: | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | networkProfile: | ||
| + | networkInterfaces: | ||
| + | { | ||
| + | id: nic.id | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Terraform Code ===== | ||
| + | |||
| + | main.tf | ||
| + | < | ||
| + | terraform { | ||
| + | required_providers { | ||
| + | azurerm = { | ||
| + | source | ||
| + | version = ">= 3.63.0" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | provider " | ||
| + | features {} | ||
| + | subscription_id = " | ||
| + | } | ||
| + | |||
| + | variable " | ||
| + | default = " | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | address_space | ||
| + | location | ||
| + | resource_group_name = " | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | location | ||
| + | resource_group_name = " | ||
| + | |||
| + | security_rule { | ||
| + | name = " | ||
| + | priority | ||
| + | direction | ||
| + | access | ||
| + | protocol | ||
| + | source_port_range | ||
| + | destination_port_range | ||
| + | source_address_prefix | ||
| + | destination_address_prefix = " | ||
| + | } | ||
| + | |||
| + | security_rule { | ||
| + | name = " | ||
| + | priority | ||
| + | direction | ||
| + | access | ||
| + | protocol | ||
| + | source_port_range | ||
| + | destination_port_range | ||
| + | source_address_prefix | ||
| + | destination_address_prefix = " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | location | ||
| + | resource_group_name = " | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | resource_group_name | ||
| + | virtual_network_name = azurerm_virtual_network.vnet.name | ||
| + | address_prefixes | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | resource_group_name | ||
| + | virtual_network_name = azurerm_virtual_network.vnet.name | ||
| + | address_prefixes | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | subnet_id | ||
| + | network_security_group_id = azurerm_network_security_group.nsg.id | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | subnet_id | ||
| + | network_security_group_id = azurerm_network_security_group.nsg.id | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | subnet_id | ||
| + | route_table_id = azurerm_route_table.rt.id | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | subnet_id | ||
| + | route_table_id = azurerm_route_table.rt.id | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | location | ||
| + | resource_group_name = " | ||
| + | ip_configuration { | ||
| + | name = " | ||
| + | subnet_id | ||
| + | private_ip_address_allocation = " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | location | ||
| + | resource_group_name = " | ||
| + | ip_configuration { | ||
| + | name = " | ||
| + | subnet_id | ||
| + | private_ip_address_allocation = " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | resource_group_name | ||
| + | location | ||
| + | size = " | ||
| + | admin_username | ||
| + | admin_password | ||
| + | disable_password_authentication = false | ||
| + | network_interface_ids = [azurerm_network_interface.nic1.id] | ||
| + | |||
| + | os_disk { | ||
| + | caching | ||
| + | storage_account_type = " | ||
| + | name = " | ||
| + | } | ||
| + | |||
| + | source_image_reference { | ||
| + | publisher = " | ||
| + | offer = " | ||
| + | sku = " | ||
| + | version | ||
| + | } | ||
| + | |||
| + | custom_data = base64encode(<< | ||
| + | #!/bin/bash | ||
| + | apt update | ||
| + | apt install -y apache2 | ||
| + | systemctl enable apache2 | ||
| + | systemctl start apache2 | ||
| + | EOF | ||
| + | ) | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | resource_group_name | ||
| + | location | ||
| + | size = " | ||
| + | admin_username | ||
| + | admin_password | ||
| + | disable_password_authentication = false | ||
| + | network_interface_ids = [azurerm_network_interface.nic2.id] | ||
| + | |||
| + | os_disk { | ||
| + | caching | ||
| + | storage_account_type = " | ||
| + | name = " | ||
| + | } | ||
| + | |||
| + | source_image_reference { | ||
| + | publisher = " | ||
| + | offer = " | ||
| + | sku = " | ||
| + | version | ||
| + | } | ||
| + | |||
| + | custom_data = base64encode(<< | ||
| + | #!/bin/bash | ||
| + | apt update | ||
| + | apt install -y apache2 | ||
| + | systemctl enable apache2 | ||
| + | systemctl start apache2 | ||
| + | EOF | ||
| + | ) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== AWS Comparison ===== | ||
| + | |||
| + | Bonus to show how similar AWS and Azure are with Terraform: | ||
| + | |||
| + | ==== AWS Terraform ==== | ||
| + | |||
| + | main_aws.tf | ||
| + | < | ||
| + | provider " | ||
| + | region = " | ||
| + | } | ||
| + | |||
| + | variable " | ||
| + | default = " | ||
| + | } | ||
| + | |||
| + | variable " | ||
| + | default = " | ||
| + | } | ||
| + | |||
| + | variable " | ||
| + | default = " | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | cidr_block = var.vpc_cidr | ||
| + | tags = { | ||
| + | Name = " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | vpc_id | ||
| + | cidr_block = var.subnet1_cidr | ||
| + | availability_zone = " | ||
| + | tags = { | ||
| + | Name = " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | vpc_id | ||
| + | cidr_block = var.subnet2_cidr | ||
| + | availability_zone = " | ||
| + | tags = { | ||
| + | Name = " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | vpc_id = aws_vpc.main.id | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | vpc_id = aws_vpc.main.id | ||
| + | |||
| + | route { | ||
| + | cidr_block = " | ||
| + | gateway_id = aws_internet_gateway.gw.id | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | subnet_id | ||
| + | route_table_id = aws_route_table.rt.id | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | subnet_id | ||
| + | route_table_id = aws_route_table.rt.id | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | name = " | ||
| + | description = "Allow SSH and HTTP" | ||
| + | vpc_id | ||
| + | |||
| + | ingress { | ||
| + | from_port | ||
| + | to_port | ||
| + | protocol | ||
| + | cidr_blocks = [" | ||
| + | } | ||
| + | |||
| + | ingress { | ||
| + | from_port | ||
| + | to_port | ||
| + | protocol | ||
| + | cidr_blocks = [" | ||
| + | } | ||
| + | |||
| + | egress { | ||
| + | from_port | ||
| + | to_port | ||
| + | protocol | ||
| + | cidr_blocks = [" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | ami = " | ||
| + | instance_type | ||
| + | subnet_id | ||
| + | vpc_security_group_ids | ||
| + | key_name | ||
| + | associate_public_ip_address = true | ||
| + | |||
| + | user_data = << | ||
| + | #!/bin/bash | ||
| + | apt update | ||
| + | apt install -y apache2 | ||
| + | systemctl enable apache2 | ||
| + | systemctl start apache2 | ||
| + | EOF | ||
| + | |||
| + | tags = { | ||
| + | Name = " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | resource " | ||
| + | ami = " | ||
| + | instance_type | ||
| + | subnet_id | ||
| + | vpc_security_group_ids | ||
| + | key_name | ||
| + | associate_public_ip_address = true | ||
| + | |||
| + | user_data = << | ||
| + | #!/bin/bash | ||
| + | apt update | ||
| + | apt install -y apache2 | ||
| + | systemctl enable apache2 | ||
| + | systemctl start apache2 | ||
| + | EOF | ||
| + | |||
| + | tags = { | ||
| + | Name = " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ai_knowledge|AI Knowledge]] | ||