Terraform: Difference between revisions

873 bytes removed ,  5 years ago
Line 463: Line 463:
== Add a volume ==
== Add a volume ==


We are going to add a root volume to the VM. Since we’re replacing its boot disk, ''this is a destructive operation''. This is something you need to watch out for in Terraform, and one of the chief reasons for reading your plans carefully before applying. It’s unlikely you’re going to accidentally create a bunch of resources and have that cause critical issues; if you accidentally issue <code>terraform destroy</code> and follow it up with agreeing <code>yes</code> to the prompt which if I recall is bright red and has more exclamation points than a junior high diary, then that’s on you. But it can be deceptively easy to accidentally create configuration changes that require ''rebuilding'' something important.
Next, add a root volume to the VM. Since this will replace its boot disk, ''this is a destructive operation''. This is something you need to watch out for in Terraform, and one of the chief reasons for reading your plans carefully before applying. It’s unlikely you’re going to accidentally cause critical issues in creating new resources, but it can be deceptively easy to accidentally create configuration changes that require ''rebuilding'' existing VMs.


You may recall from earlier my admonition to use UUIDs for flavours and images. This is why.
Since this is a root volume, create it as part of the compute instance, as another subblock along with the network subblock:
 
At any rate, this is what we want to do this time, so let’s get to it.
 
Since this is a root volume, we’re creating it as part of the compute instance, not as an independent volume to be attached later. So we’ll add another subblock to our compute instance resource, along with our network block.


<source lang="terraform">  block_device {
<source lang="terraform">  block_device {
Line 479: Line 475:
     delete_on_termination = true
     delete_on_termination = true
   }</source>
   }</source>
The <code>uuid</code> attribute is actually the UUID of the image we want to use. This is just moved from the resource definition where it already resided and given a different name (instead of <code>image_id</code>). The other attributes are self-explanatory, except for <code>destination_type</code>, which is here set to <code>volume</code> to indicate this is to be stored with an OpenStack-provided volume rather than using disk on the hypervisor. <code>delete_on_termination</code> is important&mdash;for testing, you will probably want this to be <code>true</code> so you don’t have to remember to constantly clean up leftover volumes, but for real use you should consider setting it to <code>false</code> as a last defence against accidental deletion of resources.
Set the <code>uuid</code> attribute to the UUID of the image you want to use and remove <code>image_id</code> from the outer block definition. The other attributes are self-explanatory, except for <code>destination_type</code>, which is here set to <code>volume</code> to indicate this is to be stored with an OpenStack-provided volume rather than using disk on the hypervisor. <code>delete_on_termination</code> is important&mdash;for testing, you will probably want this to be <code>true</code> so you don’t have to remember to constantly clean up leftover volumes, but for real use you should consider setting it to <code>false</code> as a last defence against accidental deletion of resources.


<blockquote>Do ''not'' leave the <code>image_id</code> attribute defined in the outer compute instance definition! This will work, but Terraform will see a change from “boot from volume” to “boot directly from image” on every run, and so will always attempt to rebuild your instance. (This is probably a flaw in the OpenStack provider.)
<blockquote>Do ''not'' leave the <code>image_id</code> attribute defined in the outer compute instance definition! This will work, but Terraform will see a change from “boot from volume” to “boot directly from image” on every run, and so will always attempt to rebuild your instance. (This is probably a flaw in the OpenStack provider.)
Line 535: Line 531:
           + floating_ip    = (known after apply)
           + floating_ip    = (known after apply)
           ~ mac            = "fa:16:3e:3b:79:27" -> (known after apply)
           ~ mac            = "fa:16:3e:3b:79:27" -> (known after apply)
             name          = "dleske-tenant-net"
             name          = "tenant-net"
           + port          = (known after apply)
           + port          = (known after apply)
           ~ uuid          = "5c96bf54-a396-47c5-ab12-574f630bcb80" -> (known
           ~ uuid          = "5c96bf54-a396-47c5-ab12-574f630bcb80" -> (known
Line 541: Line 537:
         }
         }
     }</source>
     }</source>
So note there are several warnings of what’s going to be replaced and what’s going to change, not to mention this tossoff line:
So note there are several warnings of what’s going to be replaced and what’s going to change, not to mention this line:


<source lang="shell">Plan: 2 to add, 0 to change, 2 to destroy.</source>
<source lang="shell">Plan: 2 to add, 0 to change, 2 to destroy.</source>
2 to add (oh good!) and 2 to destroy (oh dang!). This is why we pay attention to the Terraform plan, kids.


I go ahead and after chugging away for a bit, my VM is reborn with a spiffy 10GB root volume. To get on, I first have to remove the previous build’s SSH host keys from my <code>known_hosts</code> file, and then I’m off to the races. So, the first thing I do is log on and ''apply all available updates''.
Your VM will be created with a new SSH key, so if you connected previously you'll need to remove the SSH key from your <code>known_hosts</code> file (or the equivalent). After this, the first thing to do is log on and ''apply all available updates''.


<source lang="shell">[centos@myvm ~]$ sudo yum update -y
<source lang="shell">[centos@myvm ~]$ sudo yum update -y
...
...
[ goes for ages ]</source>
[ goes for ages ]</source>
So we now have a working, Terraformed VM and a way to get to it and a place on it to store data once we get there. I think that’s a pretty good start! In upcoming tutorials I’ll delve a little deeper with some more advanced topics.
So you now have a working, Terraformed VM and a way to get to it and a place on it to store data once we get there, with the latest OS patches applied.


== The full example ==
== The full example ==
cc_staff
44

edits