0% found this document useful (0 votes)
21 views4 pages

ADO Terraform Template

This document outlines the steps required for migrating on-premise resources to Azure using Azure DevOps and Terraform. It includes prerequisites, project setup, repository structure, Azure DevOps pipeline creation, and post-migration validation. Additionally, it provides sample scripts for VM and database migration, as well as guidance on maintaining and updating configurations.

Uploaded by

garepallynikhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

ADO Terraform Template

This document outlines the steps required for migrating on-premise resources to Azure using Azure DevOps and Terraform. It includes prerequisites, project setup, repository structure, Azure DevOps pipeline creation, and post-migration validation. Additionally, it provides sample scripts for VM and database migration, as well as guidance on maintaining and updating configurations.

Uploaded by

garepallynikhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Prerequisites
Before starting, ensure you have the following:
- Access to an Azure subscription.
- Azure DevOps account.
- Azure Migrate tool configured.
- Terraform installed locally (for testing).
- Azure CLI installed locally (for testing).

2. Set Up Your Azure DevOps Project


a. Create a New Project:
- Log in to [Azure DevOps](https://dev.azure.com/).
- Click on "New Project" to create a project.
- Name your project (e.g., "On-Premise to Azure Migration").
- Set visibility as "Private" or "Public" as per your need.
- Click "Create".

b. Create a New Repository:


- Go to "Repos" in your project.
- Click "Initialize" to create a new repository.
- Clone the repository to your local machine to add files (optional).

3. Prepare Your Code Repository


1. Structure the Repository:
- Create folders for different stages of the migration:
```
├── terraform
│ ├── main.tf
│ ├── variables.tf
│ ├── outputs.tf
│ └── ...
├── scripts
│ ├── migrate_vms.sh
│ ├── migrate_databases.sh
│ └── ...
├── azure-pipelines.yml
└── README.md
```

2. Write Terraform Configurations:


- In the `terraform` directory, create `main.tf`, `variables.tf`, and
`outputs.tf` files for defining your infrastructure.
- Example `main.tf`:
```hcl
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg" {


name = var.resource_group_name
location = var.location
}

Add more resources here as per your infrastructure


```

- Define variables in `variables.tf` and outputs in `outputs.tf`.

4. Set Up Azure DevOps Pipeline


1. Create a Service Connection:
- Go to "Project Settings" > "Service connections".
- Click on "New service connection" > "Azure Resource Manager".
- Choose "Service principal (automatic)".
- Select the subscription and resource group where you'll deploy resources.
- Name the service connection and save it.

2. Create a New Pipeline:


- Go to "Pipelines" > "Create Pipeline".
- Select "GitHub" or "Azure Repos" where your code is stored.
- Configure your pipeline using YAML by selecting "YAML" when prompted.
- Point it to the `azure-pipelines.yml` file in your repository.

5. Write the `azure-pipelines.yml` File

Here’s a sample pipeline YAML file to automate the process:

```yaml
trigger:
- main

pool:
vmImage: 'ubuntu-latest'

variables:
azureServiceConnection: 'YourServiceConnectionName'
terraformWorkingDirectory: 'terraform'
vmMigrationScript: 'scripts/migrate_vms.sh'
databaseMigrationScript: 'scripts/migrate_databases.sh'

steps:
- task: AzureCLI@2
inputs:
azureSubscription: $(azureServiceConnection)
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
Login to Azure
az login --service-principal -u $(servicePrincipalId) -p $
(servicePrincipalKey) --tenant $(tenantId)

Setup Terraform
terraform init $(terraformWorkingDirectory)
terraform apply -auto-approve $(terraformWorkingDirectory)

Migrate VMs
bash $(vmMigrationScript)

Migrate Databases
bash $(databaseMigrationScript)
displayName: 'Provision and Migrate Resources using Terraform and Azure CLI'

- task: AzureAppServiceManage@0
inputs:
azureSubscription: $(azureServiceConnection)
WebAppName: 'YourWebAppName'
action: 'Start Azure App Service'
displayName: 'Start PaaS Services'
```
6. Detailed Explanation of the Pipeline

- Trigger: The pipeline is set to trigger on changes to the `main` branch.


- Pool: Specifies that the pipeline will run on an Ubuntu-based virtual machine.
- Variables: Defines variables for the service connection, Terraform directory, and
scripts.
- AzureCLI@2 Task:
- Logs into Azure using the service principal.
- Initializes and applies the Terraform configurations.
- Runs shell scripts to migrate VMs and databases.
- AzureAppServiceManage@0 Task: Starts the PaaS services after migration.

7. Upload Migration Scripts

- migrate_vms.sh:
- Script to use Azure Migrate for migrating VMs.
- Example:
```bash
!/bin/bash

Assuming Azure Migrate is set up and connected to your on-premise environment


az migrate discovery list --resource-group $RESOURCE_GROUP --provider "HyperV"

More commands to handle migration


```

- migrate_databases.sh:
- Script to migrate databases.
- Example:
```bash
!/bin/bash

Use Azure Database Migration Service or other tools to migrate databases


az dms project create --name mydmsproject --resource-group $RESOURCE_GROUP --
location $LOCATION --source-platform SQL --target-platform AzureDbForPostgreSql
```

8. Running the Pipeline

1. Commit Changes:
- Commit and push all your changes to the repository.
- This will trigger the pipeline if configured correctly.

2. Monitor Pipeline:
- Go to "Pipelines" in Azure DevOps.
- Monitor the pipeline as it runs through each step.
- Troubleshoot any errors that arise.

9. Post-Migration Validation

- After migration, ensure everything works correctly:


- Validate the state of VMs and databases in Azure.
- Check that the PaaS services are running smoothly.
- Review the logs and Azure DevOps pipeline output.

10. Maintenance and Updates

- Regularly update your Terraform configurations and migration scripts.


- Adjust the pipeline as needed for future migrations or changes in your
infrastructure.

You might also like