|
| 1 | +/** |
| 2 | +* Copyright 2024 Google LLC |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +resource "google_service_account" "default" { |
| 18 | + provider = google-beta |
| 19 | + account_id = "my-sa" |
| 20 | + display_name = "Custom SA for VM Instance" |
| 21 | +} |
| 22 | + |
| 23 | +resource "google_compute_instance" "default" { |
| 24 | + provider = google-beta |
| 25 | + name = "my-instance" |
| 26 | + machine_type = "n2-standard-2" |
| 27 | + zone = "us-central1-a" |
| 28 | + tags = ["foo", "bar"] |
| 29 | + |
| 30 | + boot_disk { |
| 31 | + initialize_params { |
| 32 | + image = "debian-cloud/debian-11" |
| 33 | + labels = { |
| 34 | + my_label = "value" |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Local SSD disk |
| 40 | + scratch_disk { |
| 41 | + interface = "NVME" |
| 42 | + } |
| 43 | + |
| 44 | + network_interface { |
| 45 | + network = "default" |
| 46 | + access_config { |
| 47 | + // Ephemeral public IP |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + service_account { |
| 52 | + # Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles. |
| 53 | + email = google_service_account.default.email |
| 54 | + scopes = ["cloud-platform"] |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +resource "google_backup_dr_backup_vault" "default" { |
| 59 | + provider = google-beta |
| 60 | + location = "us-central1" |
| 61 | + backup_vault_id = "my-vault" |
| 62 | + description = "This is a second backup vault built by Terraform." |
| 63 | + backup_minimum_enforced_retention_duration = "100000s" |
| 64 | + |
| 65 | + labels = { |
| 66 | + foo = "bar1" |
| 67 | + bar = "baz1" |
| 68 | + } |
| 69 | + |
| 70 | + annotations = { |
| 71 | + annotations1 = "bar1" |
| 72 | + annotations2 = "baz1" |
| 73 | + } |
| 74 | + |
| 75 | + force_update = "true" |
| 76 | + force_delete = "true" |
| 77 | + allow_missing = "true" |
| 78 | +} |
| 79 | + |
| 80 | +resource "google_backup_dr_backup_plan" "default" { |
| 81 | + provider = google-beta |
| 82 | + location = "us-central1" |
| 83 | + backup_plan_id = "my-bp" |
| 84 | + resource_type = "compute.googleapis.com/Instance" |
| 85 | + backup_vault = google_backup_dr_backup_vault.default.name |
| 86 | + |
| 87 | + backup_rules { |
| 88 | + rule_id = "rule-1" |
| 89 | + backup_retention_days = 2 |
| 90 | + |
| 91 | + standard_schedule { |
| 92 | + recurrence_type = "HOURLY" |
| 93 | + hourly_frequency = 6 |
| 94 | + time_zone = "UTC" |
| 95 | + |
| 96 | + backup_window { |
| 97 | + start_hour_of_day = 12 |
| 98 | + end_hour_of_day = 18 |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +# [START backupdr_create_backupplanassociation] |
| 105 | + |
| 106 | +# Before creating a backup plan association, you need to create backup plan (google_backup_dr_backup_plan) |
| 107 | +# and compute instance (google_compute_instance). |
| 108 | +resource "google_backup_dr_backup_plan_association" "default" { |
| 109 | + provider = google-beta |
| 110 | + location = "us-central1" |
| 111 | + backup_plan_association_id = "my-bpa" |
| 112 | + resource = google_compute_instance.default.id |
| 113 | + resource_type = "compute.googleapis.com/Instance" |
| 114 | + backup_plan = google_backup_dr_backup_plan.default.name |
| 115 | +} |
| 116 | + |
| 117 | +# [END backupdr_create_backupplanassociation] |
0 commit comments