{"id":3058,"date":"2021-06-23T13:30:00","date_gmt":"2021-06-23T10:30:00","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tools\/upcloud-packer-builder\/"},"modified":"2021-06-23T13:30:00","modified_gmt":"2021-06-23T10:30:00","slug":"upcloud-packer-builder","status":"publish","type":"tools","link":"https:\/\/upcloud.com\/global\/resources\/tools\/upcloud-packer-builder\/","title":{"rendered":"UpCloud Packer Builder"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a rel=\"noreferrer noopener\" href=\"https:\/\/www.packer.io\/\" target=\"_blank\">Packer<\/a> is an easy-to-use automation solution for creating any type of machine images. It embraces modern configuration management by allowing automated software installation and setup within Packer-built images.&nbsp;UpCloud Packer builder integrates Packer with our Cloud Servers and makes creating private templates fast!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Packer plugin<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/UpCloudLtd\/packer-plugin-upcloud\" target=\"_blank\" rel=\"noreferrer noopener\">UpCloud Packer builder<\/a> is a plugin for Packer to simplify template configuration and make deploying custom Cloud Servers quick and easy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Packer can be downloaded for most operating systems as well as installed using common package managers. To install the precompiled binary, you will need to download the appropriate package for your OS. On most popular Linux distributions, you can install Packer using your package manager after adding their repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># Debian and Ubuntu\ncurl -fsSL https:\/\/apt.releases.hashicorp.com\/gpg | sudo apt-key add -\nsudo apt-add-repository \\\n\"deb [arch=amd64] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\"\nsudo apt-get update &amp;&amp; sudo apt-get install packer<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># CentOS\nsudo yum install -y yum-utils\nsudo yum-config-manager --add-repo \\\nhttps:\/\/rpm.releases.hashicorp.com\/RHEL\/hashicorp.repo\nsudo yum -y install packer<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Packer builder leverages the UpCloud Go API to interface with the UpCloud API. You will need to provide a username and password with the access rights to the API functions to authenticate. We recommend setting up a new workspace member account with only the API privileges for security purposes. You can do this at your <a href=\"https:\/\/hub.upcloud.com\/people\" target=\"_blank\" rel=\"noreferrer noopener\">UpCloud Control Panel<\/a>. Learn more about creating API credentials at our guide for <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/getting-started-upcloud-api\" target=\"_blank\" rel=\"noreferrer noopener\">getting started with UpCloud API<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enter the API user credentials in your terminal with the following two commands. Replace the&nbsp;username and&nbsp;password with your user details.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">export PKR_VAR_UPCLOUD_USERNAME=username\nexport PKR_VAR_UPCLOUD_PASSWORD=password<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should also save these in your profile file to avoid having to repeat the export command every time you open a new terminal. Simply add the same lines to the ~\/.profile or ~\/.bashrc file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring custom images<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Packer uses the <a href=\"https:\/\/github.com\/hashicorp\/hcl\" target=\"_blank\" rel=\"noreferrer noopener\">Hashicorp configuration language<\/a> (HCL) format for the configuration files to define the template you wish to build.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a sample template, which you can also find on our Packer builder GitHub page in the <a href=\"https:\/\/github.com\/UpCloudLtd\/packer-plugin-upcloud\/tree\/main\/example\" target=\"_blank\" rel=\"noreferrer noopener\">examples\/<\/a> directory. It reads your UpCloud API credentials from the environment variables and creates an Ubuntu 20.04 LTS server in the nl-ams1 region.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once deployed, Packer builds the template by following the provisioner instructions. In this example, Packer updates the server software and adds an SSH key to the root user before creating a custom image based on the Cloud Server installation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">variable \"UPCLOUD_USERNAME\" {\n  type = string\n  default = \"\"\n}\nvariable \"UPCLOUD_PASSWORD\" {\n  type = string\n  sensitive = true\n  default = \"\"\n}\npacker {\n    required_plugins {\n        upcloud = {\n            version = \"&gt;=v1.0.0\"\n            source = \"github.com\/UpCloudLtd\/upcloud\"\n        }\n    }\n}\n\nsource \"upcloud\" \"test\" {\n  username = \"${var.UPCLOUD_USERNAME}\"\n  password = \"${var.UPCLOUD_PASSWORD}\"\n  zone = \"nl-ams1\"\n  storage_name = \"ubuntu server 20.04\"\n  template_prefix = \"ubuntu-server\"\n}\n\nbuild {\n  sources = [\"source.upcloud.com\"]\n  provisioner \"shell\" {\n    inline = [\n      \"apt-get update\",\n      \"apt-get upgrade -y\",\n      \"echo 'ssh-rsa-key' | tee \/root\/.ssh\/authorized_keys\"\n    ]\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You will need to provide a username and a password with the access rights to the API functions to authenticate. We recommend setting up a new Member account with only the API privileges for security purposes. You can do this at your <a href=\"https:\/\/hub.upcloud.com\/account\">UpCloud Control Panel<\/a> in the My Account menu under the User Accounts tab. Check out our <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/getting-started-upcloud-api\" target=\"_blank\" rel=\"noreferrer noopener\">getting started with the UpCloud API<\/a> tutorial to learn more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Packer is a great way to standardise your cloud deployments and save time! If you are new to Packer, make sure to head over to our tutorial on <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/upcloud-packer-builder\">how to create custom images using the UpCloud Packer builder<\/a>.<\/p>\n","protected":false},"featured_media":21687,"template":"","tools-category":[292],"class_list":["post-3058","tools","type-tools","status-publish","has-post-thumbnail","hentry","tools-category-system-imaging"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tools\/3058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tools"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tools"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tools\/3058\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=3058"}],"wp:term":[{"taxonomy":"tools-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tools-category?post=3058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}