{"id":82839,"date":"2021-01-18T20:57:19","date_gmt":"2021-01-18T17:57:19","guid":{"rendered":"https:\/\/computingforgeeks.com\/?p=82839"},"modified":"2023-11-06T23:10:36","modified_gmt":"2023-11-06T20:10:36","slug":"create-lxc-containers-using-terraform","status":"publish","type":"post","link":"https:\/\/computingforgeeks.com\/create-lxc-containers-using-terraform\/","title":{"rendered":"How To Create LXC Containers using Terraform"},"content":{"rendered":"\n<p>LXC, or Linux Containers is a userspace interface that provides containment features of the Linux kernel. LXC lets users create Linux containers  which are as close as possible to a standard Linux installation but using the same Kernel as the host machine.<\/p>\n\n\n\n<p>Terraform on the other hand is an Infrastructure as code tool used for automation of infrastructure deployment. Terraform is widely used with cloud providers to automate management of their infrastructure as it provides a wide range of mechanisms both for deployment and management of existing infrastructure. <\/p>\n\n\n\n<p>Terraform uses configuration files which describes the components needed to run a single application or your entire datacenter, and desired state will be effected.<\/p>\n\n\n\n<p>This guide we shall discuss how to use Terraform to deploy LXC containers on Ubuntu LTS. The pointers below highlight what we shall achieve at the end of this guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install LXC on Ubuntu<\/li>\n\n\n\n<li>Install Terraform on Ubuntu<\/li>\n\n\n\n<li>Create LXC container using Terraform<\/li>\n\n\n\n<li>Attach volume to a container using Terraform<\/li>\n\n\n\n<li>Configure container network using Terraform<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s dive right in!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1) Install LXC on Ubuntu \/ Debian<\/h2>\n\n\n\n<p>We need to install and initialize LXC on our Ubuntu host. Use the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Update and upgrade your system:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update &amp;&amp; sudo apt -y full-upgrade\n&#91; -f \/var\/run\/reboot-required ] &amp;&amp; sudo reboot -f<\/code><\/pre>\n\n\n\n<p>2. Install LXD \/ LXC<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> sudo snap install lxd<\/mark>\nSetup snap \"lxd\" (23680) security profiles                                                                                                                                                           -\n2022-10-13T20:02:46+03:00 INFO Waiting for conflicting change in progress: conflicting snap snapd with task \"discard-snap\"\nlxd 5.6-794016a from Canonical\u2713 installed<\/code><\/pre>\n\n\n\n<p>Confirm installation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> which lxc<\/mark>\n\/snap\/bin\/lxc\n\n$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">which lxd<\/mark>\n\/snap\/bin\/lxd<\/code><\/pre>\n\n\n\n<p>3. Initialize LXC <\/p>\n\n\n\n<p>Run the command below to initialize LXC and setup some defaults<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo lxd init<\/code><\/pre>\n\n\n\n<p>You will have to answer some questions according to how you want to setup your LXC environment. e.g.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">sudo lxd init<\/mark>\nWould you like to use LXD clustering? (yes\/no) &#91;default=no]:  \nDo you want to configure a new storage pool? (yes\/no) &#91;default=yes]:  \nName of the new storage pool &#91;default=default]:  \nName of the storage backend to use (btrfs, dir, lvm, zfs, ceph) &#91;default=zfs]:  \nCreate a new ZFS pool? (yes\/no) &#91;default=yes]:  \nWould you like to use an existing empty disk or partition? (yes\/no) &#91;default=no]:  \nSize in GB of the new loop device (1GB minimum) &#91;default=5GB]:  \nWould you like to connect to a MAAS server? (yes\/no) &#91;default=no]:  \nWould you like to create a new local network bridge? (yes\/no) &#91;default=yes]:  \nWhat should the new bridge be called? &#91;default=lxdbr0]:  \nWhat IPv4 address should be used? (CIDR subnet notation, \u201cauto\u201d or \u201cnone\u201d) &#91;default=auto]:  \nWhat IPv6 address should be used? (CIDR subnet notation, \u201cauto\u201d or \u201cnone\u201d) &#91;default=auto]:  \nWould you like LXD to be available over the network? (yes\/no) &#91;default=no]:  \nWould you like stale cached images to be updated automatically? (yes\/no) &#91;default=yes]  \nWould you like a YAML \"lxd init\" preseed to be printed? (yes\/no) &#91;default=no]:<\/code><\/pre>\n\n\n\n<p>We now have an LXC\/LXD infrastructure ready to start deployment of Linux Containers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2) Install Terraform on Ubuntu \/ Debian<\/h2>\n\n\n\n<p>Use the steps below to install Terraform on your host.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Add the HashiCorp GPG key<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -fsSL https:\/\/apt.releases.hashicorp.com\/gpg | sudo gpg --dearmor -o \/etc\/apt\/trusted.gpg.d\/hashicorp.gpg<\/code><\/pre>\n\n\n\n<p>2. Add HashiCorp repository<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-add-repository \"deb &#91;arch=amd64] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\"<\/code><\/pre>\n\n\n\n<p>3. Install Terraform<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install terraform<\/code><\/pre>\n\n\n\n<p>4. Enable tab completion<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform -install-autocomplete\nsource ~\/.bashrc||source ~\/.zshrc<\/code><\/pre>\n\n\n\n<p>We have our terraform installed and ready to be used. You can verify installed version by using the command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">terraform -version<\/mark>\nTerraform v1.3.9\non linux_amd64<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3) Create LXC container using Terraform<\/h2>\n\n\n\n<p>The next step is to configure Terraform so we can use it to install LXC containers. We shall be using LXD Terraform provider to connect provision resources. Create a new terraform main.tf configuration file that will define the provider to be used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tee main.tf&lt;&lt;EOF\nterraform {\n  required_providers {\n    lxd = {\n      source = \"terraform-lxd\/lxd\"\n    }\n  }\n}\nEOF<\/code><\/pre>\n\n\n\n<p>Initialize to download provider and create necessary files.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">terraform init<\/mark>\nInitializing the backend...\n\nInitializing provider plugins...\n- Finding latest version of terraform-lxd\/lxd...\n- Installing terraform-lxd\/lxd v1.9.1...\n- Installed terraform-lxd\/lxd v1.9.1 (self-signed, key ID 62BC1162214B5D1E)\n\nPartner and community providers are signed by their developers.\nIf you'd like to know more about provider signing, you can read about it here:\nhttps:&#47;&#47;www.terraform.io\/docs\/cli\/plugins\/signing.html\n\nTerraform has created a lock file .terraform.lock.hcl to record the provider\nselections it made above. Include this file in your version control repository\nso that Terraform can guarantee to make the same selections by default when\nyou run \"terraform init\" in the future.\n\nTerraform has been successfully initialized!\n\nYou may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.<\/code><\/pre>\n\n\n\n<p>We have to update our main.tf file to add container resource definitions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">vim main.tf<\/mark>\nterraform {\n  required_providers {\n    lxd = {\n      source = \"terraform-lxd\/lxd\"\n    }\n  }\n}\nprovider \"lxd\" {\n  generate_client_certificates = true\n  accept_remote_certificate    = true\n}\n\n<em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"># Create LXC Container\n<\/mark><\/em>resource \"lxd_container\" \"<em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-cyan-blue-color\">server1<\/mark><\/em>\" {\n  name      = \"<em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">server1<\/mark><\/em>\"\n  image     = \"<em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">ubuntu:22.04<\/mark><\/em>\"\n  ephemeral = false\n  profiles = &#91;\"default\"]\n}<\/code><\/pre>\n\n\n\n<p>Check what will be deployed buy running the command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> terraform plan<\/mark>\nTerraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:\n  + create\n\nTerraform will perform the following actions:\n\n  # lxd_container.server1 will be created\n  + resource \"lxd_container\" \"server1\" {\n      + ephemeral        = false\n      + id               = (known after apply)\n      + image            = \"ubuntu:22.04\"\n      + ip_address       = (known after apply)\n      + ipv4_address     = (known after apply)\n      + ipv6_address     = (known after apply)\n      + mac_address      = (known after apply)\n      + name             = \"server1\"\n      + privileged       = false\n      + profiles         = &#91;\n          + \"default\",\n        ]\n      + start_container  = true\n      + status           = (known after apply)\n      + target           = (known after apply)\n      + type             = (known after apply)\n      + wait_for_network = true\n    }\n\nPlan: 1 to add, 0 to change, 0 to destroy.<\/code><\/pre>\n\n\n\n<p>Run the <code>terraform apply<\/code> command to apply the changes. You will be prompted to confirm if the script should proceed making changes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> terraform apply<\/mark>\nTerraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:\n  + create\n\nTerraform will perform the following actions:\n\n  # lxd_container.server1 will be created\n  + resource \"lxd_container\" \"server1\" {\n      + ephemeral        = false\n      + id               = (known after apply)\n      + image            = \"ubuntu:22.04\"\n      + ip_address       = (known after apply)\n      + ipv4_address     = (known after apply)\n      + ipv6_address     = (known after apply)\n      + mac_address      = (known after apply)\n      + name             = \"server1\"\n      + privileged       = false\n      + profiles         = &#91;\n          + \"default\",\n        ]\n      + start_container  = true\n      + status           = (known after apply)\n      + target           = (known after apply)\n      + type             = (known after apply)\n      + wait_for_network = true\n    }\n\nPlan: 1 to add, 0 to change, 0 to destroy.\n\nDo you want to perform these actions?\n  Terraform will perform the actions described above.\n  Only 'yes' will be accepted to approve.\n\n  Enter a value: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">yes<\/mark>\nlxd_container.server1: Creating...\nlxd_container.server1: Still creating... &#91;10s elapsed]\nlxd_container.server1: Still creating... &#91;20s elapsed]\nlxd_container.server1: Still creating... &#91;30s elapsed]\nlxd_container.server1: Still creating... &#91;40s elapsed]\nlxd_container.server1: Still creating... &#91;50s elapsed]\nlxd_container.server1: Creation complete after 52s &#91;id=server1]\n\nApply complete! Resources: 1 added, 0 changed, 0 destroyed.<\/code><\/pre>\n\n\n\n<p>You can now check and see if your instance is created:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lxc list<\/code><\/pre>\n\n\n\n<p>Sample output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"110\" src=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-1024x110.png\" alt=\"\" class=\"wp-image-82917\" title=\"\" srcset=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-1024x110.png 1024w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-300x32.png 300w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-768x82.png 768w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-696x75.png 696w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform.png 1065w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You can access the created server using the command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> lxc exec server1 \/bin\/bash<\/mark>\nroot@server1:~# <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">ip ad<\/mark>\n1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000\n    link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1\/8 scope host lo\n       valid_lft forever preferred_lft forever\n    inet6 ::1\/128 scope host\n       valid_lft forever preferred_lft forever\n4: eth0@if5: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000\n    link\/ether 00:16:3e:f3:29:d9 brd ff:ff:ff:ff:ff:ff link-netnsid 0\n    inet 10.21.25.225\/24 metric 100 brd 10.21.25.255 scope global dynamic eth0\n       valid_lft 3443sec preferred_lft 3443sec\n    inet6 fd42:bf39:6882:ccb3:216:3eff:fef3:29d9\/64 scope global dynamic mngtmpaddr noprefixroute\n       valid_lft 3508sec preferred_lft 3508sec\n    inet6 fe80::216:3eff:fef3:29d9\/64 scope link\n       valid_lft forever preferred_lft forever<\/code><\/pre>\n\n\n\n<p>Remember to use a name of your choice for <code>server1<\/code> during container creation and accessing the container.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4) Attaching Volume to a container using Terraform<\/h2>\n\n\n\n<p>You can also create a volume and attach it to your container. We will first create a storage pool of <code>type=dir<\/code> and assign a path on the server where the for the pool.<\/p>\n\n\n\n<p>We then will create a volume then attach the volume to our container. We then will define where the new volume shall be mounted on the container.<\/p>\n\n\n\n<p>Update your main.tf file to have all the components discussed above as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    lxd = {\n      source = \"terraform-lxd\/lxd\"\n    }\n  }\n}\n\nprovider \"lxd\" {\n  generate_client_certificates = true\n  accept_remote_certificate    = true\n}\n\nresource \"lxd_storage_pool\" \"<em>pool1<\/em>\" {\n  name = \"<em>mypool<\/em>\"\n  driver = \"dir\"\n  config = {\n    source = \"\/var\/lib\/lxd\/storage-pools\/mypool\"\n  }\n}\n\nresource \"lxd_volume\" \"volume1\" {\n  name = \"myvolume\"\n  pool = \"${lxd_storage_pool.pool1.name}\"\n}\n\nresource \"lxd_container\" \"server1\" {\n  name      = \"server1\"\n  image     = \"ubuntu:22.04\"\n  ephemeral = false\n  profiles = &#91;\"default\"]\n\ndevice {\n    name = \"volume1\"\n    type = \"disk\"\n    properties = {\n      path = \"\/opt\/\"\n      source = \"${lxd_volume.volume1.name}\"\n      pool = \"${lxd_storage_pool.pool1.name}\"\n    }\n   }                                                                                                                            \n  }<\/code><\/pre>\n\n\n\n<p>Run the<a href=\"https:\/\/spacelift.io\/blog\/terraform-plan\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/spacelift.io\/blog\/terraform-plan\" rel=\"noreferrer noopener\"> terraform plan command<\/a> to see what changes will be effected when you apply.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">terraform apply<\/mark>\n\nlxd_container.server1: Refreshing state... &#91;id=server1]\n\nAn execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  + create\n  ~ update in-place\n\nTerraform will perform the following actions:\n\n  # lxd_container.server1 will be updated in-place\n  ~ resource \"lxd_container\" \"server1\" {\n        id               = \"server1\"\n        name             = \"server1\"\n        # (14 unchanged attributes hidden)\n\n      + device {\n          + name       = \"volume1\"\n          + properties = {\n              + \"path\"   = \"\/opt\/\"\n              + \"pool\"   = \"mypool\"\n              + \"source\" = \"myvolume\"\n            }\n          + type       = \"disk\"\n        }\n    }\n\n  # lxd_storage_pool.pool1 will be created\n  + resource \"lxd_storage_pool\" \"pool1\" {\n      + config = {\n          + \"source\" = \"\/var\/lib\/lxd\/storage-pools\/mypool\"\n        }\n      + driver = \"dir\"\n      + id     = (known after apply)\n      + name   = \"mypool\"\n    }\n\n  # lxd_volume.volume1 will be created\n  + resource \"lxd_volume\" \"volume1\" {\n      + expanded_config = (known after apply)\n      + id              = (known after apply)\n      + name            = \"myvolume\"\n      + pool            = \"mypool\"\n      + type            = \"custom\"\n    }\n\nPlan: 2 to add, 1 to change, 0 to destroy.\n<\/code><\/pre>\n\n\n\n<p>Then apply the changes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">terraform apply<\/mark>\n....\nDo you want to perform these actions?\n  Terraform will perform the actions described above.\n  Only 'yes' will be accepted to approve.\n\n  Enter a value: yes\n\nlxd_storage_pool.pool1: Creating...\nlxd_storage_pool.pool1: Creation complete after 0s &#91;id=mypool]\nlxd_volume.volume1: Creating...\nlxd_volume.volume1: Creation complete after 0s &#91;id=mypool\/myvolume\/custom]\nlxd_container.server1: Modifying... &#91;id=server1]\nlxd_container.server1: Modifications complete after 0s &#91;id=server1]\n\nApply complete! Resources: 2 added, 1 changed, 0 destroyed.<\/code><\/pre>\n\n\n\n<p>In the screenshot below, we can see that the volume has been attached to <code>\/opt<\/code> of the container.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"252\" src=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-2.png\" alt=\"\" class=\"wp-image-82920\" title=\"\" srcset=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-2.png 491w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2021\/01\/how-to-create-LXC-containers-using-Terraform-2-300x154.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5) Configure container network using Terraform<\/h2>\n\n\n\n<p>The last part of this article is about how to setup container network and use it to provision conainers.<\/p>\n\n\n\n<p>This part involved creating a profile, LXC uses namespaces(profiles) do define the containers. We will create a new profile configure the attributes including the creation of a new network bridge and <a href=\"https:\/\/computingforgeeks.com\/configure-dhcp-server-on-windows-server\/\">DHCP server<\/a>. We shall then provision our containers in the new namespace\/profile to make use of the new changes we shall have made.<\/p>\n\n\n\n<p>Modify your main.tf file and add the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resource \"lxd_network\" \"new_default\" {\n  name = \"new_default\"\n\n  config = {\n    \"ipv4.address\" = \"10.150.19.1\/24\"\n    \"ipv4.nat\"     = \"true\"\n    \"ipv6.address\" = \"fd42:474b:622d:259d::1\/64\"\n    \"ipv6.nat\"     = \"true\"\n  }\n}\n\nresource \"lxd_profile\" \"profile1\" {\n  name = \"profile1\"\n\n  device {\n    name = \"eth0\"\n    type = \"nic\"\n\n    properties = {\n      nictype = \"bridged\"\n      parent  = \"${lxd_network.new_default.name}\"\n    }\n  }\n device {\n    type = \"disk\"\n    name = \"root\"\n\n    properties = {\n      pool = \"default\"\n      path = \"\/\"\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>We have named our profile as <strong>profile1<\/strong>. A network bridge has also been created.<\/p>\n\n\n\n<p>We will need to modify the existing container state to use the new profile if we wish to use the new network subnet. The new main.tf configuration file should be as below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    lxd = {\n      source = \"terraform-lxd\/lxd\"\n      version = \"1.5.0\"\n    }\n  }\n}\n\n\nresource \"lxd_storage_pool\" \"pool1\" {\n  name = \"mypool\"\n  driver = \"dir\"\n  config = {\n    source = \"\/var\/lib\/lxd\/storage-pools\/mypool\"\n  }\n}\n\nresource \"lxd_volume\" \"volume1\" {\n  name = \"myvolume\"\n  pool = \"${lxd_storage_pool.pool1.name}\"\n}\n\nresource \"lxd_network\" \"new_default\" {\n  name = \"new_default\"\n\n  config = {\n    \"ipv4.address\" = \"10.150.19.1\/24\"\n    \"ipv4.nat\"     = \"true\"\n    \"ipv6.address\" = \"fd42:474b:622d:259d::1\/64\"\n    \"ipv6.nat\"     = \"true\"\n  }\n}\n\nresource \"lxd_profile\" \"profile1\" {\n  name = \"profile1\"\n  device {\n    name = \"eth0\"\n    type = \"nic\"\n\n    properties = {\n      nictype = \"bridged\"\n      parent  = \"${lxd_network.new_default.name}\"\n    }\n  }\ndevice {\n    type = \"disk\"\n    name = \"root\"\n\n    properties = {\n      pool = \"default\"\n      path = \"\/\"\n    }\n  }\n}\n\nresource \"lxd_container\" \"server1\" {\n  name      = \"server1\"\n  image     = \"ubuntu:18.04\"\n  ephemeral = false\n  profiles = &#91;\"profile1\"]\n\ndevice {\n    name = \"volume1\"\n    type = \"disk\"\n    properties = {\n      path = \"\/opt\/\"\n      source = \"${lxd_volume.volume1.name}\"\n      pool = \"${lxd_storage_pool.pool1.name}\"\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>Apply the changes then check if your container network has changed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@host:~\/terraform#<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> lxc list<\/mark>\n+---------+---------+---------------------+-----------------------------------------------+------------+-----------+\n|  NAME   |  STATE  |        IPV4         |                     IPV6                      |    TYPE    | SNAPSHOTS |\n+---------+---------+---------------------+-----------------------------------------------+------------+-----------+\n| server1 | RUNNING | 10.150.19.13 (eth0) | fd42:474b:622d:259d:216:3eff:fe8c:e44e (eth0) | PERSISTENT | 0         |\n+---------+---------+---------------------+-----------------------------------------------+------------+-----------+<\/code><\/pre>\n\n\n\n<p>We have achieved all the objectives for this article as stated in the beginning. I hope you were able to follow keenly and deploy your first container using Terraform. Check out these interesting articles about Terraform:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/computingforgeeks.com\/how-to-store-terraform-state-in-consul-kv-store\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Store Terraform State in Consul KV Store<\/a><\/li>\n\n\n\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/computingforgeeks.com\/how-to-provision-vms-on-ovirt-rhev-with-terraform\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Provision VMs on oVirt \/ RHEV with Terraform<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>LXC, or Linux Containers is a userspace interface that provides containment features of the Linux kernel. LXC lets users create Linux containers which are as close as possible to a standard Linux installation but using the same Kernel as the host machine. Terraform on the other hand is an Infrastructure as code tool used for &#8230; <a title=\"How To Create LXC Containers using Terraform\" class=\"read-more\" href=\"https:\/\/computingforgeeks.com\/create-lxc-containers-using-terraform\/\" aria-label=\"Read more about How To Create LXC Containers using Terraform\">Read more<\/a><\/p>\n","protected":false},"author":17,"featured_media":77461,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[329,316,299,6176],"tags":[218,381,559],"class_list":["post-82839","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation","category-containers","category-how-to","category-terraform","tag-containers","tag-lxc","tag-terraform"],"_links":{"self":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/82839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/comments?post=82839"}],"version-history":[{"count":0,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/82839\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media\/77461"}],"wp:attachment":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media?parent=82839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/categories?post=82839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/tags?post=82839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}