Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying Backup Policy To Boot Volumes #622

Closed
elwyne-mabanglo opened this issue Oct 2, 2018 · 3 comments
Closed

Applying Backup Policy To Boot Volumes #622

elwyne-mabanglo opened this issue Oct 2, 2018 · 3 comments

Comments

@elwyne-mabanglo
Copy link

Terraform Version

# Run this command to get the terraform version:

Terraform v0.11.7

  • provider.oci v2.1.17

OCI Provider Version

terraform-provider-oci_v2.1.17

Description:

Hi All,

I've created two instances using "count =2" and I'm looking to apply a backup policy to both the boot volumes. Solution I came up with was to get a attached boot volume data source and filter for the instance OCID. I manage to get it working when I look for only one instance but when I look for two. I get the following error below. Can anyone provide some assistance?

Example below:

Error

oci_core_volume_backup_policy_assignment.policy[0]: Resource data.oci_core_boot_volume_attachments.boot_volume' not found for variable 'data.oci_core_boot_volume_attachments.boot_volume.boot_volume_attachments'

oci_core_volume_backup_policy_assignment.policy[1]: Resource 'data.oci_core_boot_volume_attachments.boot_volume' not found for variable 'data.oci_core_boot_volume_attachments.boot_volume.boot_volume_attachments'

Terraform Plan

resource "oci_core_instance" "instance" {
count = 2
availability_domain = "${lookup(data.oci_identity_availability_domains.ad.availability_domains[0],"name")}"
compartment_id = "${var.compartment_ocid}"
shape = "VM.Standard2.2"
display_name = "instance${count.index}"
hostname_label = "instance${count.index}"

metadata = {
ssh_authorized_keys = "${file(var.ssh_public_key)}"
}

source_details {
source_type = "image"
source_id = "${lookup(data.oci_core_images.image.images[0],"id")}"
boot_volume_size_in_gbs = "50"
}

create_vnic_details {
assign_public_ip = "False"
private_ip = ""
subnet_id = "${lookup(data.oci_core_subnets.subnet.subnets[0],"id")}"
}
}

resource "oci_core_volume_backup_policy_assignment" "policy" {
count = 2
asset_id = "${lookup(data.oci_core_boot_volume_attachments.boot_volume.boot_volume_attachments[count.index],"boot_volume_id")}"
policy_id = "${lookup(data.oci_core_volume_backup_policies.gold_policy.volume_backup_policies[0],"id")}"
}

data "oci_core_boot_volume_attachments" "boot_volume" {
count = 2
availability_domain = "${lookup(data.oci_identity_availability_domains.ad.availability_domains[0],"name")}"
compartment_id = "${var.compartment_ocid}"
instance_id = "${oci_core_instance.instance.*.id[count.index]}"
}

@kilokahn
Copy link
Member

kilokahn commented Oct 3, 2018

@elwyne-mabanglo the [count.index] seems misplaced:
should be:

${lookup(data.oci_core_boot_volume_attachments.boot_volume[count.index].boot_volume_attachments[0],"boot_volume_id")}

instead of

${lookup(data.oci_core_boot_volume_attachments.boot_volume.boot_volume_attachments[count.index],"boot_volume_id")}

However, this kind of access is not supported via the lookup operation as noted in hashicorp/terraform#8047 (will work on flat lists only). We can try to combine element and lookup (as done in hashicorp/terraform#13905 (comment)) -- but it is not recommended because it might cause unnecessary force new behavior (because TF treats element syntax for cases where results are not known beforehand and might treat it as a diff).

We are looking to add support for a "singular" datasource for boot_volume_attachments that returns a single boot_volume_attachment (rather than a list) which will help in using the count syntax easily. Till that time, you'd need to repeat the resource.

Separately, terraform v0.12.0 promises better syntax and may solve this, but that is yet to be tested by us.

Hope this information helps!

@elwyne-mabanglo
Copy link
Author

Hi @kilokahn, thanks for your feedback.

I decided to remove "count" when creating an instance and applied what you suggested above. Was able to apply the policy to the boot volume.

Thanks!

@varmax2511
Copy link
Member

Another workaround here can be to reference the boot volume ids directly from the instances.

asset_id = "${oci_core_instance.instance.*.boot_volume_id[count.index]}"

This is illustrated in OCI Terraform Provider instance example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants