Terraform: Difference between revisions

Line 261: Line 261:
== Adding a network ==
== Adding a network ==


All we need at this stage is the name of the network we need on the VM. The name of this network differs from cloud to cloud within Compute Canada, but typically they are on a 192.168.X.Y network, and sometimes named for the OpenStack project. In this case the network name in my project is <code>dleske-tenant-net</code>, so I add a <code>network</code> resource sub-block to my VM definition so I have:
The name of the private network differs from project to project and the naming convention can differ from cloud to cloud within Compute Canada, but typically they are on a 192.168.X.Y network, and can be found in the CLI using `network list` or on Horizon under ''Network''->''Networks''.  If your project's private network is <code>my-tenant-net</code>, you will add a <code>network</code> resource sub-block to your VM definition similar to the following:


<source lang="terraform">resource "openstack_compute_instance_v2" "myvm" {
<source lang="terraform">resource "openstack_compute_instance_v2" "myvm" {
Line 271: Line 271:


   network {
   network {
     name = "dleske-tenant-net"
     name = "my-tenant-net"
   }
   }
}</source>
}</source>
I then try again.
 
Try again:


<source lang="shell">$ terraform apply
<source lang="shell">$ terraform apply
Line 332: Line 333:


Apply complete! Resources: 1 added, 0 changed, 0 destroyed.</source>
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.</source>
And there we have it, a VM created by Terraform. If you’re following along (please do!) you should see your new VM on Horizon or in the output of <code>server list</code> in your OpenStack terminal window:
 
You now have a VM created by Terraform. You should see your new VM on Horizon or in the output of <code>server list</code> in your OpenStack terminal window:


<pre class="plaintext">(openstack) server list -c ID -c Name -c Status
<pre class="plaintext">(openstack) server list -c ID -c Name -c Status
Line 343: Line 345:
| 9b42cbf3-3782-4472-bdd0-9028bbb73460 | lbr    | ACTIVE |
| 9b42cbf3-3782-4472-bdd0-9028bbb73460 | lbr    | ACTIVE |
+--------------------------------------+--------+--------+</pre>
+--------------------------------------+--------+--------+</pre>
You can see here I have three other VMs created previously which are surviving.
In this example output, there are three other VMs created previously which survive untouched by Terraform.
 
=== Recap ===


=== Where we are now ===
You now have a working VM which has successfully been initialized and is on the private network. You can’t log in and check it out however because you haven’t assigned a floating IP to this host, so it’s not directly accessible from outside the tenant.


We now have a working VM which has successfully been initialized and is on the private network. We can’t log in and check it out however because we haven’t assigned a floating IP to this host, so it’s not directly accessible from outside the tenant.
If you had another host in that tenant with a floating IP, you could use that host as a jump host (sometimes called a ''bastion host'') to the new VM, as they will both be on the same private network. This is a good strategy to use for nodes that do not need to be directly accessible from the internet, such as a database server, or just to preserve floating IPs, which are a limited resource.


If we had another host in that tenant with a floating IP, we could use that host as a jump host (sometimes called a ''bastion host'') to the new VM, as they will both be on the same private network. This is a good strategy to use for nodes that do not need to be directly accessible from the internet, such as a database server, or just to preserve floating IPs, which are a limited resource until ipv6 becomes a thing.
For now, add a floating IP to your new VM.  


For this case, we’ll add a floating IP to our new VM. First, though, note there is now a file in your workspace called <code>terraform.tfstate</code>. This was created by Terraform during the application of the new configuration and confirmation of its success. The state file contains details about the managed resources Terraform uses to determine how to arrive at a new state described by configuration updates. In general, you will not need to look at this file, but know that without it, Terraform cannot properly manage resources and if you delete it, you will need to restore it or recreate it, or manage those resources without Terraform.
First, though, note there is now a file in your workspace called <code>terraform.tfstate</code>. This was created by Terraform during the application of the new configuration and confirmation of its success. The state file contains details about the managed resources Terraform uses to determine how to arrive at a new state described by configuration updates. In general, you will not need to look at this file, but know that without it, Terraform cannot properly manage resources and if you delete it, you will need to restore it or recreate it, or manage those resources without Terraform.


== Add a floating IP ==
== Add a floating IP ==
cc_staff
44

edits