{"id":2002,"date":"2022-02-23T17:51:38","date_gmt":"2022-02-23T15:51:38","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/export-cloud-services-import-terraform\/"},"modified":"2022-02-23T17:51:38","modified_gmt":"2022-02-23T15:51:38","slug":"export-cloud-services-import-terraform","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/export-cloud-services-import-terraform\/","title":{"rendered":"How to export cloud resources and import to Terraform"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/upcloud.com\/global\/blog\/upcloud-verified-terraform-provider\/\">Terraform<\/a> is a simple yet powerful open-source infrastructure management tool that allows you to build, change, and version infrastructure safely and efficiently. While Terraform is great for building new cloud services, you can now also easily export UpCloud Control Panel managed configurations for importing to Terraform!<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/terraform_logo.png\" alt=\"Terraform logo\" class=\"wp-image-6158\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">In this guide, we will show you how to export your cloud services as ready-to-use Terraform configuration files and how to import these into Terraform. The process is really quick and easy allowing anyone to migrate to managing their cloud infrastructure using Terraform.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Test out Terraform on UpCloud!<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started with Terraform<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Managing your cloud infrastructure on UpCloud is really straightforward using our Terraform provider module. It&#8217;s publicly available on our <a href=\"https:\/\/github.com\/UpCloudLtd\/terraform-provider-upcloud\" target=\"_blank\" rel=\"noopener\">GitHub<\/a> as well as on <a href=\"https:\/\/registry.terraform.io\/providers\/UpCloudLtd\/upcloud\/latest\" target=\"_blank\" rel=\"noopener\">Terraform&#8217;s Registry<\/a> which makes it simple to install. If you are new to Terraform on UpCloud or Terraform in general, have a look at our <a href=\"https:\/\/upcloud.com\/global\/community\/tutorials\/get-started-terraform\/\" target=\"_blank\" rel=\"noopener\">getting started tutorial to install Terraform<\/a> and set up your Terraform configuration directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Exporting cloud resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Terraform command-line tool allows importing existing resources simply by providing it with matching configurations. However, this could be quite cumbersome even with smaller services let alone on 100+ cloud server infrastructure! To solve this, we offer an option to programmatically generate Terraform configuration from your existing UpCloud services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get started, head over to your <a href=\"https:\/\/hub.upcloud.com\/terraform\/\" target=\"_blank\" rel=\"noopener\">UpCloud Control Panel and the Terraform section<\/a>. Then click the <em>Export my infrastructure<\/em> button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/terraform-export.webp\" alt=\"Exporting cloud resources for Terraform\" class=\"wp-image-23740\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Select the resources you wish to manage via Terraform from now on. Note that selecting a resource will automatically select all of its dependencies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Your cloud resources are arranged according to their types; servers, networks, databases, for example.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/terraform-export-servers.webp\" alt=\"Selecting cloud resources for export\" class=\"wp-image-23743\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When you&#8217;ve made your selections, click the <em>Next<\/em> button to continue.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lastly, confirm you have selected the cloud resources you want to export. Then click the <em>Next<\/em> button to run the export process.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/terraform-export-summary.webp\" alt=\"Summary for cloud resources to be exported\" class=\"wp-image-23744\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once you&#8217;ve exported cloud resources at your UpCloud Control Panel, you will be presented with a Terraform configuration file and instructions on how to import it. Proceed to the next section to continue to import your resources to Terraform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Importing Terraform configurations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The exported configuration is presented in a single file and can be copied as such. However, you can also separate your resources into their own files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Following our tutorial on <a href=\"https:\/\/upcloud.com\/global\/community\/tutorials\/get-started-terraform\/\" target=\"_blank\" rel=\"noopener\">how to get started with Terraform<\/a>, you might have already created a configuration directory. If not, do so now by simply creating a folder where you wish to save your Terraform configuration. For example in your home directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir -p ~\/terraform\/prod<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then create a file called <tt>main.tf<\/tt> in that directory and copy your Terraform configuration to it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure you include the <em>terraform<\/em> and <em>provider<\/em> sections as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/terraform-export-config.webp\" alt=\"Exported Terraform configuration\" class=\"wp-image-23741\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When you&#8217;ve saved your Terraform configuration, you will need to initialise your Terraform directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform init<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, run the import commands as displayed in the Terraform import instructions in your UpCloud Control Panel. The example below shows a list of commands. Note the <tt>&amp;&amp;<\/tt> at the end of each line meaning the whole list can be executed as one command.<br><img loading=\"lazy\" decoding=\"async\" width=\"1250\" height=\"365\" class=\"alignnone size-full wp-image-23745\" src=\"https:\/\/upcloud.com\/media\/terraform-import-commands.webp\" alt=\"Terraform import commands\" \/><br>When running the import commands, you should see a confirmation like the output example underneath for each import.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Import successful!\n\nThe resources that were imported are shown above. These resources are now in\nyour Terraform state and will henceforth be managed by Terraform.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, test the import by running the <tt>terraform plan<\/tt> command to verify your configuration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform plan<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you then want to make changes to your cloud services, simply amend the Terraform configuration files and apply the changes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform apply<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it! You should now have successfully exported at least some of your cloud resources and imported them to Terraform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Importing your cloud resources to Terraform allows you to codify your infrastructure for easier maintenance and management. Furthermore, the export feature at your UpCloud Control Panel makes it easier than ever before to get started with Terraform.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After importing your first Terraform config, you might want to learn how to make the most of Terraform. For example, check out our tutorial on <a href=\"https:\/\/upcloud.com\/global\/community\/tutorials\/terraform-variables\/\" target=\"_blank\" rel=\"noopener\">how to use Terraform variables<\/a> to make it quick and easy to modify or replicate your cloud services.<\/p>\n","protected":false},"author":3,"featured_media":23768,"comment_status":"open","ping_status":"closed","template":"","community-category":[235],"class_list":["post-2002","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2002"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2002\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2002"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}