Creating Terraform infrastructure from existing VMs in Azure can be a straightforward process.
First, you'll need to install the Azure CLI and Terraform on your machine.
To connect your Azure subscription to Terraform, you'll need to generate a service principal and provide it with the necessary permissions.
The Azure provider in Terraform allows you to create and manage Azure resources, including VMs, storage accounts, and networks.
You can use the Azure CLI to retrieve the ID of your existing VM, which will be used to create a Terraform resource.
By following these steps, you can successfully create Terraform infrastructure from existing VMs in Azure.
Worth a look: Aws Create S3 Bucket Cli
File Preparation
To prepare your Terraform file, start by adding the azurerm backend, which requires details from the storage account created previously, including the storage account name, container name, resource group, and key of your workload.
You'll need to provide these details to configure the backend correctly.
First, initialize the Terraform workspace by running the command to set up the environment for applying changes.
Next, create a simple resource group, which will serve as the foundation for your Terraform configuration.
After that, apply the changes to the Terraform workspace to ensure everything is set up correctly.
Worth a look: Terraform Azure Storage Account
Importing Resource
To import an existing Azure resource into Terraform, you'll need to use the `terraform import` command. This command requires two attributes: the Terraform Resource Path and the Azure Resource ID.
The Terraform Resource Path is the unique path and identifier for the Terraform Resource in the Terraform code, consisting of the resource type and local name for the resource. For example, in the previous snippet, the resource path is `azurerm_virtual_network.terrafirm-hub-vnet`.
The Azure Resource ID is the full Azure Resource Manager (ARM) ID within Microsoft Azure for the existing resource. This ID can be found within the Azure Portal for the existing resource, and it's used to identify the resource in Azure.
Here's a sample `terraform import` command to import an existing Azure resource:
```
terraform import azurerm_virtual_network.terrafirm-hub-vnet /subscriptions/subscription_id/resourceGroups/resource_group_name/providers/Microsoft.Network/virtualNetworks/virtual_network_name
```
Note that the Azure Resource ID should be replaced with the actual ID of the existing resource in Azure.
Collecting data about the existing resources is an optional step, but it can help you understand the processes and existing resources better. You can create a Terraform configuration file that reads and outputs data from the existing resources, and then use the `terraform import` command to import the resource details into the Terraform state.
For another approach, see: What Is Azure Storage
The full process to bring existing resources under Terraform control looks like this:
1. Collect data about the existing resources (optional)
2. Create a Terraform configuration file
3. Import resource details to Terraform state
4. Test
By following these steps, you can import existing infrastructure into Terraform and bring it under management.
Consider reading: How to Create Terraform from Existing Resources Azure
Create Config File
You'll need to create a Terraform configuration file to provision and manage your existing Azure resources.
The Terraform configuration file must be written so it can provision and manage the two resources with all relevant properties.
You can use most of the property names and values you collected in the previous step, but be selective about what you add to the configuration file, as some properties are read-only or empty.
This configuration file is crucial for Terraform to understand your existing resources and their properties.
In this file, you'll define the resource, making sure the Terraform resource name is unique within the project.
Terraform requires each resource to have a unique name in the project, as this is used to identify the resource within the Terraform State.
High-Level Process
To bring existing resources under Terraform control, you need to instruct Terraform that there are existing resources living somewhere on a platform it does not yet know about. To make Terraform aware of these resources, they need to be added to the Terraform state.
The full process to bring existing resources under Terraform control looks like this:
- Collect data about the existing resources (optional)
- Create a Terraform configuration file
- Import resource details to Terraform state
- Test
In the case of importing an existing resource, such as a storage account, you'll need to specify the ID of the resource and the resource it should be imported to in the Terraform configuration file. After adding the import block of code, run `terraform apply` once again to update the Terraform state.
Starting Point
To create a Terraform configuration from an existing VM in Azure, you'll need to start with a basic setup. Your files should look similar to the example provided, with three main files: `provider.tf`, `variables.tf`, and `main.tf`.
The `provider.tf` file should include the Azure provider, specifying the source and version. For instance, you might have `provider "azurerm" { source = "hashicorp/azurerm" version = "~>3.1" }`.
You'll also need to define variables in the `variables.tf` file, such as the resource group name, location, and container group name. These variables can be used throughout your configuration. The example shows default values for these variables, but you may need to adjust them to match your specific setup.
In the `main.tf` file, you'll define the resources you want to create, such as an Azure resource group and a container group. The example uses the `azurerm_resource_group` and `azurerm_container_group` resources, specifying the name, location, and other properties. You'll also need to define a unique identifier for the container group using the `locals` block and the `sha1` function.
Here's a summary of the main files and their contents:
- `provider.tf`: specifies the Azure provider and its version
- `variables.tf`: defines variables for the resource group name, location, and container group name
- `main.tf`: defines the resources to be created, including the Azure resource group and container group
Azure Resource
To import an existing Azure resource into Terraform, you'll need to use the terraform import command, which requires two attributes: the Terraform Resource Path and the Azure Resource ID.
The Terraform Resource Path is a unique identifier for the resource in the Terraform project, consisting of the resource type and local name separated by a period. For example, azurerm_virtual_network.terrafirm-hub-vnet.
The Azure Resource ID is the full Azure Resource Manager (ARM) ID for the existing resource, which can be found in the Azure Portal by navigating to the resource and opening the Properties pane.
To import the resource, you'll need to run the terraform import command, specifying the Terraform Resource Path and Azure Resource ID.
Here's a sample terraform import command:
terraform import azurerm_virtual_network.terrafirm-hub-vnet /subscriptions/subscriptionID/resourceGroups/resourceGroupName/providers/Microsoft.Network/virtualNetworks/terrafirm-hub-vnet
After importing the resource, you can verify that it's been added to the Terraform state by opening the terraform.tfstate file and checking that the resource is listed.
Frequently Asked Questions
How do I clone an existing Azure VM?
To clone an existing Azure VM, log in to Azure, select the VM to clone, and follow the steps to create a new VM from a managed disk. This process involves creating a snapshot, managed disk, and new VM, with the original VM remaining operational.
Sources
- https://build5nines.com/terraform-import-existing-azure-resources-into-state-tfstate/
- https://www.azurecitadel.com/terraform/fundamentals/import/
- https://www.restack.io/p/terraform-knowledge-azure-vm-management
- https://camargo-wes.medium.com/how-to-import-an-existing-azure-resource-into-your-terraform-state-with-azure-storage-backend-2f6cbdee5927
- https://marcelzehner.ch/2020/07/04/how-to-bring-existing-azure-resources-under-terraform-management/
Featured Images: pexels.com