Share via


Anpassat resursval och namngivning med azure export för Terraform

Azure Export for Terraform innehåller olika alternativ för att anpassa vilka resurser du exporterar.

I den här artikeln får du lära dig fördelar och nackdelar för varje alternativ.

  • Använda användargränssnittet
  • Använda frågeläge
  • Använda en mappningsfil

Använda användargränssnittet

När du kör Azure Export for Terraform i interaktivt läge visas de angivna resurserna (via de parametrar du anger när du kör). Som standard exporteras alla resurser.

Ta bort fungerar som en växlingsknapp för att hoppa över eller inkludera resurser. Om du vill ta bort resurser från att exporteras använder du piltangenterna för att välja önskad resurs och trycker på Ta bort. Resursen uppdateras för att visa "Hoppa över".

Om du vill ångra hoppa över-åtgärden kontrollerar du att den överhoppade resursen är markerad och trycker på Ta bort igen.

Proffsen:

  • Kräver användning av en enda växlingsnyckel.
  • Du behöver inte känna till de resurser du vill ha innan du kör kommandot.

Nackdelar:

  • Åtgärden kan vara tidskrävande om du har många resurser att bläddra igenom och hoppa över.

Använda frågeläge

Att använda ett filter med azure resource graph-frågesyntax är en kraftfull teknik när du vet exakt vilka filter du behöver.

aztfexport query [option] <ARG_where_predicate>

Anta till exempel att du har en resursgrupp med namnet myResourceGroup som har många resurser, inklusive en nätverksresurs. Om du bara vill exportera nätverksresursen kan du använda följande syntax:

aztfexport query -n "resourceGroup =~ 'myResourceGroup' and type contains 'Microsoft.Network'"

Proffsen:

  • Ett enda kommando utan manuell redigering krävs.
  • Stöder ett obegränsat antal filter.
  • Hanterar stora mängder resurser effektivt.

Nackdelar:

  • Det är enkelt att exkludera resurser som du behöver exportera.
  • Kräver kunskap om Azure Resource Graph-syntax.

Använda en mappningsfil

Följande syntax visar grunderna för att exportera en uppsättning resurser som definieras i en resursmappningsfil:

aztfexport mapping-file [option] <resource_mapping_file>

Du kan använda en mappningsfil i interaktiva eller icke-interaktiva lägen:

  • Interaktivt läge: Tryck på s när du kör interaktivt i resurslistevyn.
  • Icke-interaktivt läge: Du kan generera mappningsfilen i alla exportkommandon (resource, , resource-groupquery, mapping file) genom att lägga till --generate-mapping-file flaggan.

Om dina användningsfall kräver förexportändringar kan du manuellt konstruera eller redigera mappningsfilen. Här följer några exempel på när du vill redigera din egen mappningsfil manuellt:

Användningsfall Steg
Du har många resurser i en resursgrupp men behöver bara exportera några få. Ta bort JSON-objekten från valfri redigerare och spara filen innan du exporterar.
Du vill byta namn på alla resurser på ett konsekvent sätt. Ändra egenskapen resource-name till det namn som matchar företagets efterlevnadsstandarder.
Du måste omstrukturera en uppsättning resurser efter deras resurstyp , till exempel nätverk eller beräkning. Använd redigeringsprogrammet för att hitta alla Microsoft.Network eller Microsoft.Compute resurser.

Anta till exempel att du kör följande kommando för en resursgrupp som innehåller en virtuell dator:

aztfexport rg --generate-mapping-file --non-interactive myResourceGroup

Resultatet liknar följande JSON-fil:

{
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/vm-MyResourceGroup/extensions/OmsAgentForLinux": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/vm-MyResourceGroup/extensions/OmsAgentForLinux",
		"resource_type": "azurerm_virtual_machine_extension",
		"resource_name": "res-0"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup",
		"resource_type": "azurerm_resource_group",
		"resource_name": "res-1"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/sshPublicKeys/vm-MyResourceGroup_key": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/sshPublicKeys/vm-MyResourceGroup_key",
		"resource_type": "azurerm_ssh_public_key",
		"resource_name": "res-2"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/vm-MyResourceGroup": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/vm-MyResourceGroup",
		"resource_type": "azurerm_linux_virtual_machine",
		"resource_name": "res-3"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkInterfaces/vm-myResourceGroup-vm-d146": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkInterfaces/vm-myResourceGroup-vm-d146",
		"resource_type": "azurerm_network_interface",
		"resource_name": "res-4"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkInterfaces/vm-myResourceGroup-vm-d146/networkSecurityGroups/L3N1YnNjcmlwdGlvbnMvZGJmM2I2Y2ItYzFkMC00ZDA0LTk0YjktNTE1MDliOGQzM2ZkL3Jlc291cmNlR3JvdXBzL2hhc2hpY29uZi12bS1kZW1vL3Byb3ZpZGVycy9NaWNyb3NvZnQuTmV0d29yay9uZXR3b3JrU2VjdXJpdHlHcm91cHMvdm0taGFzaGljb25mLXZtLWRlbW8tbnNn": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkInterfaces/vm-myResourceGroup-vm-d146|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkSecurityGroups/vm-MyResourceGroup-nsg",
		"resource_type": "azurerm_network_interface_security_group_association",
		"resource_name": "res-5"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkSecurityGroups/vm-MyResourceGroup-nsg": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkSecurityGroups/vm-MyResourceGroup-nsg",
		"resource_type": "azurerm_network_security_group",
		"resource_name": "res-6"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/publicIPAddresses/vm-MyResourceGroup-ip": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/publicIPAddresses/vm-MyResourceGroup-ip",
		"resource_type": "azurerm_public_ip",
		"resource_name": "res-7"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/MyResourceGroup-vnet": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/MyResourceGroup-vnet",
		"resource_type": "azurerm_virtual_network",
		"resource_name": "res-8"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/MyResourceGroup-vnet/subnets/default": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/MyResourceGroup-vnet/subnets/default",
		"resource_type": "azurerm_subnet",
		"resource_name": "res-9"
	}
}

Endast objektvärdet i mappningsfilen har betydelse. Nyckeln (standardvärdet för Azure resource_id) är bara en identifierare i det här läget.

Anta nu att vi vill behålla resursgruppen och eventuella beräkningsrelaterade resurser och ändra resource_name värdet.

Vi kan uppdatera mappningsfilen på följande sätt:

{
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup",
		"resource_type": "azurerm_resource_group",
		"resource_name": "myResourceGroup"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
		"resource_type": "azurerm_linux_virtual_machine",
		"resource_name": "myVM"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/sshPublicKeys/myKey": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/sshPublicKeys/myKey",
		"resource_type": "azurerm_ssh_public_key",
		"resource_name": "myKey"
	},
	"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/vm-myResourceGroup/extensions/OmsAgentForLinux": {
		"resource_id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/vm-myResourceGroup/extensions/OmsAgentForLinux",
		"resource_type": "azurerm_virtual_machine_extension",
		"resource_name": "myVMExtension"
	}
}

När du har redigerat mappningsfilen exporterar du mappningsfilen med följande kommando:

aztfexport map -n "aztfexportResourceMapping.json"

Proffsen:

  • Eftersom du redigerar en fil kan du använda en redigerare för att hitta och ersätta det du behöver för att ta bort eller redigera.
  • JSON-utdata möjliggör unika funktioner , till exempel skript för att filtrera.
  • Kan byta namn på resurser så att de matchar namngivningsstandarderna.
  • Kan omstrukturera JSON till flera mappningsfiler.
  • Hanterar stora mängder resurser väl.

Nackdelar:

  • För enkla scenarier kan den här tekniken vara overkill.
  • Kräver manuella ändringar.

Sammanfattning

I den här artikeln har du lärt dig om de olika alternativen för att filtrera resurser när du exporterar med Azure Export for Terraform.

Nästa steg