Skip to content
Notifications
Clear all

Guide: Replicating EC2 backup strategy to Azure using Terraform.

2 Posts
2 Users
0 Reactions
10 Views
(@grafana_guy_night)
Honorable Member
Joined: 6 months ago
Posts: 427
Topic starter   [#27214]

Hi everyone! I recently switched from AWS to Azure for a project. On AWS, I used a simple Terraform setup to manage daily EBS snapshots with a 7-day retention. I'm trying to replicate that logic in Azure for managed disks.

Here's my starting point for an Azure VM's OS disk. I'm using `azurerm_managed_disk` and `azurerm_snapshot`. My goal is to create a snapshot each day and clean up any older than a week.

```hcl
resource "azurerm_snapshot" "daily_backup" {
name = "daily-${var.vm_name}-${formatdate("YYYYMMDD", timestamp())}"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
create_option = "Copy"
source_uri = azurerm_managed_disk.os_disk.id

lifecycle {
ignore_changes = [name]
}
}
```

My main questions:
1. How do you handle the rolling deletion? In AWS I used a `count` based on `aws_ebs_snapshot`. Not sure the best pattern here.
2. Is `source_uri` the right way to go from a managed disk? I saw `source_resource_id` as an option too.

Any pointers on making this production-ready would be awesome! Still learning the Azure way of doing things 😅



   
Quote
(@emilyk4)
Reputable Member
Joined: 3 months ago
Posts: 216
 

That `source_uri` vs `source_resource_id` point is interesting. I've been using `source_resource_id` in my own basic tests for managed disks and it seems to work. The Azure documentation can be a bit tangled on that.

For the daily creation and cleanup, I've seen people handle it outside Terraform with a scheduled Azure Automation runbook. Is keeping it all in Terraform with a count-based approach actually recommended, or does it get too messy?



   
ReplyQuote