{"id":2017,"date":"2022-01-25T16:43:45","date_gmt":"2022-01-25T14:43:45","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/managed-databases-terraform\/"},"modified":"2022-01-25T16:43:45","modified_gmt":"2022-01-25T14:43:45","slug":"managed-databases-terraform","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/managed-databases-terraform\/","title":{"rendered":"How to configure Managed Databases using 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 developed by HashiCorp. Terraform allows you to safely and predictably manage everything from Cloud Servers to Object Storage and Managed Databases by codifying APIs into declarative configuration files.<\/p>\n\n\n<div class=\"wp-block-image size-full wp-image-6158 aligncenter\">\n<figure class=\"size-large\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/terraform_logo.png\" alt=\"Terraform logo\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Building your cloud setup around Terraform is a great way to configure something once and be able to deploy it again and again with no effort at all. In this guide, we will show how to configure Managed Databases 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\/\">Try Terraform for free!<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-prerequisites\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform integrates with UpCloud\u2019s infrastructure via our <a href=\"https:\/\/upcloud.com\/global\/resources\/tools\/upcloud-terraform-provider\">verified provider module<\/a>. Using the UpCloud Terraform provider is as simple as declaring the required providers in a configuration file and then adding the desired resources.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To begin with, you need to allow Terraform to access your UpCloud account by setting an account name and password in your environmental variables. If you haven\u2019t done so already, check out our <a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/get-started-terraform\">Terraform intro tutorial<\/a> to set your API credentials before continuing with the rest of this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-setting-up-configuration-directory\">Setting up a configuration directory<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Managing cloud infrastructure using Terraform makes it simple to edit, review, and version, as well as easy to share amongst team members. This includes everything from Cloud Servers to Managed Databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have already set up your Terraform directory, feel free to skip to the next section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, create a directory to hold your Terraform configuration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir ~\/terraform-upcloud-database &amp;&amp; cd ~\/terraform-upcloud-database<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To start with using the UpCloud Terraform module requires you to declare the following configuration block. A standard name for a file with the following HCL is version.tf. Create the file with the below command then open it for editing with your preferred text editor.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">touch version.tf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, add the following to the file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform {\n  required_providers {\n    upcloud = {\n      source = \"UpCloudLtd\/upcloud\"\n    }\n  }\n}\n\nprovider \"upcloud\" {\n  # Your UpCloud credentials are read from the environment variables\n  # export UPCLOUD_USERNAME=\"Username for Upcloud API user\"\n  # export UPCLOUD_PASSWORD=\"Password for Upcloud API user\"\n  # Optional configuration settings can be depclared here\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Afterwards, save the file and exit the editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then run the following command in that directory to download the necessary provider modules.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform init<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the required module declaration in place, you can get started.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, create a new Terraform configuration, for example, <tt>db-example.tf<\/tt> in your Terraform directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">touch db-example.tf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once done, you are good to continue.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-create-managed-databases\">Create Managed Databases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The UpCloud Managed Database currently supports MySQL and PostgreSQL databases, each of which can be configured using Terraform as its own resource.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, MySQL configuration would look something like the following.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># MySQL managed database with additional logical database: example2_db \nresource \"upcloud_managed_database_mysql\" \"example\" {\n  name = \"mymysql-1\"\n  plan = \"1x1xCPU-2GB-25GB\"\n  zone = \"nl-ams1\"\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As for the latter, PostgreSQL can be created with the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># PostgreSQL managed database with additional logical database: example_db \nresource \"upcloud_managed_database_postgresql\" \"example\" {\n  name  = \"postgres-1\"\n  plan  = \"1x1xCPU-2GB-25GB\"\n  zone  = \"nl-ams1\"\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, open the <tt>db-example.tf<\/tt> file in your favourite text editor. Then, include a resource section using one of the example configurations shown above.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve added your Managed Database configuration, next you need to deploy it. Terraform offers easy-to-use commands to safely and predictably deploy resources and apply changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, verify your build plan with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform plan<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This generates an execution plan that shows what actions will be taken when the plan is applied. It includes the server configuration, log-in details, storage settings, and the deployment zone.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, deploy the configuration by executing the plan with the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform apply<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Reply <tt>yes<\/tt> when asked to confirm the deployment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform performs syntax verifications again in deployment to spare you from configuration errors that could take precious time to roll back.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Separating plans and applying commands reduces mistakes and uncertainty at scale. Plans show operators what would happen when executing changes with the apply command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-mysql-custom-configurations\">MySQL custom configurations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform allows you to easily create Managed Database instances with minimal configuration. However, it also provides options for further customisation using the <tt>properties<\/tt> options.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, when configuring MySQL, you can set many additional properties besides the minimal configuration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Service with custom properties\nresource \"upcloud_managed_database_mysql\" \"example_3\" {\n  name = \"mysql-3\"\n  plan = \"2x2xCPU-4GB-50GB\"\n  zone = \"nl-ams1\"\n  properties {\n    admin_username     = \"admin\"\n    admin_password     = \"<span class=\"has-inline-color has-vivid-red-color\">{password}<\/span>\"\n    backup_hour        = 1  # Backup at 1.30AM\n    backup_minute      = 30\n    ip_filter          = [\"public_ip_1\/32\", \"public_ip_range\/24\"]\n    public_access      = true\n    max_allowed_packet = 16e+6 # 16MB\n    sort_buffer_size   = 4e+6 # 4MB\n    sql_mode           = \"NO_ENGINE_SUBSTITUTION\"\n    version            = \"8\"\n    wait_timeout       = 300\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can find a full list of configurable properties in the <a href=\"https:\/\/registry.terraform.io\/providers\/UpCloudLtd\/upcloud\/latest\/docs\/resources\/managed_database_mysql\" target=\"_blank\" rel=\"noreferrer noopener\">module documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-postgresql-custom-configurations\">PostgreSQL custom configurations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Below is an example of some of the additional configuration <tt>properties<\/tt> for PostgreSQL.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Service with custom properties\nresource \"upcloud_managed_database_postgresql\" \"example_2\" {\n  name  = \"postgres-2\"\n  plan  = \"2x2xCPU-4GB-50GB\"\n  title = \"postgres\"\n  zone  = \"nl-ams1\"\n  properties {\n    admin_username = \"admin\"\n    admin_password = \"<span class=\"has-inline-color has-vivid-red-color\">{password}<\/span>\"\n    backup_hour    = 1\n    backup_minute  = 30\n    ip_filter      = [\"public_ip_1\/32\", \"public_ip_range\/24\"]\n    public_access  = true\n    version        = \"13\"\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check out the full list of configurable properties in the <a href=\"https:\/\/registry.terraform.io\/providers\/UpCloudLtd\/upcloud\/latest\/docs\/resources\/managed_database_postgresql\" target=\"_blank\" rel=\"noreferrer noopener\">module documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-logical-databases\">Creating logical databases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Besides creating Managed Database instances, Terraform is also able to create new databases within the system as needed. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you had a Managed Database instance configured like the configuration underneath.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># MySQL managed database with additional logical database: example2_db \nresource \"upcloud_managed_database_mysql\" \"example\" {\n  name = \"mysql-1\"\n  plan = \"1x1xCPU-2GB-25GB\"\n  zone = \"nl-ams1\"\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You could then create databases using the <tt>upcloud_managed_database_logical_database<\/tt> resource by simply providing the target database service ID and giving the database a name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_managed_database_logical_database\" \"example2_db\" {\n  service = upcloud_managed_database_mysql.example.id\n  name    = \"example2_db\"\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When set, just run the Terraform apply command again to create the configured database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Likewise, if you want to remove a database from the system, delete the relevant resource within your configuration files and apply the changes using Terraform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-adding-database-users\">Adding database users<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to being able to create databases, Terraform can manage database users as well. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to add a new username to any database, create the following resource and configure the target service database ID, username and password.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_managed_database_postgresql\" \"example\" {\n  name  = \"postgres\"\n  plan  = \"1x1xCPU-2GB-25GB\"\n  title = \"postgres\"\n  zone  = \"nl-ams1\"\n}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_managed_database_user\" \"example_user\" {\n  service  = upcloud_managed_database_postgresql.example.id\n  username = \"example_user\"\n  password = \"<span class=\"has-inline-color has-vivid-red-color\">{password}<\/span>\"\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After making the changes to your Terraform configuration, run the apply command again. You can then connect to the database using the newly created credentials.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to remove a username configured via Terraform, just delete the resource from your configuration file and apply the changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-connecting-to-the-managed-database\">Connecting to the Managed Database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have created your Managed Database using Terraform, you would likely want to connect to it. To do so, you will need the right connection details on top of your username and password.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check on Terraform for the database details using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform show<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Find your service connection details. The output should show something similar to the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_managed_database_mysql\" \"example2_db\" {\n    ...\n    plan                    = \"1x1xCPU-2GB-25GB\"\n    powered                 = true\n    primary_database        = \"defaultdb\"\n    service_host            = \"mysql-1-riweqdegodht.db.upclouddatabases.com\"\n    service_password        = (sensitive value)\n    service_port            = \"11550\"\n    service_uri             = (sensitive value)\n    service_username        = \"upadmin\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can then use the service host, port, password and username to connect to your new Managed Database. The easiest way to test the connection is using a command-line client like <tt>mycli<\/tt>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Install the client on your computer or Cloud Server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Ubuntu and Debian\nsudo apt install mycli\n# CentOS\nsudo yum install mycli<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then test your database connection with the connection string.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mycli mysql:\/\/admin:<span class=\"has-inline-color has-vivid-red-color\">{password}<\/span>@mysql-1-riweqdegodht.db.upclouddatabases.com:11550\/defaultdb?ssl-mode=REQUIRED<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you enabled public access, add public- to the beginning of your service host. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mycli mysql:\/\/admin:<span class=\"has-inline-color has-vivid-red-color\">{password}<\/span>@public-mysql-1-riweqdegodht.db.upclouddatabases.com:11550\/defaultdb?ssl-mode=REQUIRED<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Should you need to use public access regularly, remember to restrict the accepted connections with the IP filtering in the database properties.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_managed_database_mysql\" \"example2_db\" {\n  ...\n  properties {\n    ...\n    ip_filter = [\"public_ip_1\/32\", \"public_ip_range\/24\"]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the <tt>public_ip\/32<\/tt> or <tt>public_ip_range\/24<\/tt> with the actual public IP addresses of your external resources.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform is a powerful tool for building cloud infrastructure from Cloud Servers to Object Storage and Managed Databases. It codifies your systems for more efficient management and adds to the value of UpCloud&#8217;s Managed Databases by making them even easier and faster to configure.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","community-category":[226,268,235],"class_list":["post-2017","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2017","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=2017"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2017\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2017"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}