|
| 1 | +# How to deploy using Terraform |
| 2 | + |
| 3 | +[Terraform](https://www.terraform.io/) is an infrastructure automation tool to provision and manage resources in clouds or data centers. To deploy Charmed MySQL using Terraform and Juju, you can use the [Juju Terraform Provider](https://registry.terraform.io/providers/juju/juju/latest). |
| 4 | + |
| 5 | +The easiest way is to start from [these examples of terraform modules](https://github.com/canonical/terraform-modules) prepared by Canonical. This page will guide you through a deployment using an example module for MySQL on machines. |
| 6 | + |
| 7 | +For an in-depth introduction to the Juju Terraform Provider, read [this Discourse post](https://discourse.charmhub.io/t/6939). |
| 8 | + |
| 9 | +[note] |
| 10 | +**Note**: Storage support was added in [Juju Terraform Provider version 0.13+](https://github.com/juju/terraform-provider-juju/releases/tag/v0.13.0). |
| 11 | +[/note] |
| 12 | + |
| 13 | +## Summary |
| 14 | +* [Install Terraform tooling](#install-terraform-tooling) |
| 15 | +* [Verify the deployment](#verify-the-deployment) |
| 16 | +* [Apply the deployment](#apply-the-deployment) |
| 17 | +* [Check deployment status](#check-deployment-status) |
| 18 | +* [Clean up](#clean-up) |
| 19 | +--- |
| 20 | + |
| 21 | +## Install Terraform tooling |
| 22 | + |
| 23 | +This guide assumes Juju is installed and you have an LXD controller already bootstrapped. For more information, check the [Set up the environment](/t/9924) tutorial page. |
| 24 | + |
| 25 | +Let's install Terraform Provider and example modules: |
| 26 | +```shell |
| 27 | +sudo snap install terraform --classic |
| 28 | +``` |
| 29 | +Switch to the LXD provider and create a new model: |
| 30 | +```shell |
| 31 | +juju switch lxd |
| 32 | +juju add-model my-model |
| 33 | +``` |
| 34 | +Clone examples and navigate to the MySQL machine module: |
| 35 | +```shell |
| 36 | +git clone https://github.com/canonical/terraform-modules.git |
| 37 | +cd terraform-modules/modules/machine/mysql |
| 38 | +``` |
| 39 | + |
| 40 | +Initialise the Juju Terraform Provider: |
| 41 | +```shell |
| 42 | +terraform init |
| 43 | +``` |
| 44 | + |
| 45 | +## Verify the deployment |
| 46 | + |
| 47 | +Open the `main.tf` file to see the brief contents of the Terraform module: |
| 48 | + |
| 49 | +```tf |
| 50 | +resource "juju_application" "machine_mysql" { |
| 51 | + name = var.mysql_application_name |
| 52 | + model = var.juju_model_name |
| 53 | + trust = true |
| 54 | +
|
| 55 | + charm { |
| 56 | + name = "mysql" |
| 57 | + channel = var.mysql_charm_channel |
| 58 | + } |
| 59 | +
|
| 60 | + units = 1 |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Run `terraform plan` to get a preview of the changes that will be made: |
| 65 | + |
| 66 | +```shell |
| 67 | +terraform plan -var "juju_model_name=my-model" |
| 68 | +``` |
| 69 | + |
| 70 | +## Apply the deployment |
| 71 | + |
| 72 | +If everything looks correct, deploy the resources (skip the approval): |
| 73 | + |
| 74 | +```shell |
| 75 | +terraform apply -auto-approve -var "juju_model_name=my-model" |
| 76 | +``` |
| 77 | + |
| 78 | +## Check deployment status |
| 79 | + |
| 80 | +Check the deployment status with |
| 81 | + |
| 82 | +```shell |
| 83 | +juju status --model lxd:my-model --watch 1s |
| 84 | +``` |
| 85 | + |
| 86 | +Sample output: |
| 87 | + |
| 88 | +```shell |
| 89 | +Model Controller Cloud/Region Version SLA Timestamp |
| 90 | +my-model lxd-controller localhost/localhost 3.5.3 unsupported 12:49:34Z |
| 91 | + |
| 92 | +App Version Status Scale Charm Channel Rev Exposed Message |
| 93 | +mysql 8.0.36-0ubun... active 1 mysql 8.0/stable 240 no |
| 94 | + |
| 95 | +Unit Workload Agent Machine Public address Ports Message |
| 96 | +mysql/0* active idle 0 10.101.248.225 3306,33060/tcp Primary |
| 97 | + |
| 98 | +Machine State Address Inst id Base AZ Message |
| 99 | +0 started 10.101.248.225 juju-c4a403-0 [email protected] Running |
| 100 | +``` |
| 101 | + |
| 102 | +Continue to operate the charm as usual from here or apply further Terraform changes. |
| 103 | + |
| 104 | +## Clean up |
| 105 | + |
| 106 | +To keep the house clean, remove the newly deployed Charmed PostgreSQL by running |
| 107 | +```shell |
| 108 | +terraform destroy -var "juju_model_name=my-model" |
| 109 | +``` |
| 110 | + |
| 111 | +Sample output: |
| 112 | +```shell |
| 113 | +juju_application.machine_mysql: Refreshing state... [id=my-model:mysql] |
| 114 | + |
| 115 | +Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: |
| 116 | + - destroy |
| 117 | + |
| 118 | +Terraform will perform the following actions: |
| 119 | + |
| 120 | + # juju_application.machine_mysql will be destroyed |
| 121 | + - resource "juju_application" "machine_mysql" { |
| 122 | + - constraints = "arch=amd64" -> null |
| 123 | + - id = "my-model:mysql" -> null |
| 124 | + - model = "my-model" -> null |
| 125 | + - name = "mysql" -> null |
| 126 | + - placement = "0" -> null |
| 127 | + - storage = [ |
| 128 | + - { |
| 129 | + - count = 1 -> null |
| 130 | + - label = "database" -> null |
| 131 | + - pool = "rootfs" -> null |
| 132 | + - size = "99G" -> null |
| 133 | + }, |
| 134 | + ] -> null |
| 135 | + - trust = true -> null |
| 136 | + - units = 1 -> null |
| 137 | + |
| 138 | + - charm { |
| 139 | + - base = "[email protected]" - > null |
| 140 | + - channel = "8.0/stable" -> null |
| 141 | + - name = "mysql" -> null |
| 142 | + - revision = 240 -> null |
| 143 | + - series = "jammy" -> null |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | +Plan: 0 to add, 0 to change, 1 to destroy. |
| 148 | + |
| 149 | +Changes to Outputs: |
| 150 | + - application_name = "mysql" -> null |
| 151 | + |
| 152 | +Do you really want to destroy all resources? |
| 153 | + Terraform will destroy all your managed infrastructure, as shown above. |
| 154 | + There is no undo. Only 'yes' will be accepted to confirm. |
| 155 | + |
| 156 | + Enter a value: yes |
| 157 | + |
| 158 | +juju_application.machine_mysql: Destroying... [id=my-model:mysql] |
| 159 | +juju_application.machine_mysql: Destruction complete after 0s |
| 160 | + |
| 161 | +Destroy complete! Resources: 1 destroyed. |
| 162 | +``` |
| 163 | +--- |
| 164 | +[note] |
| 165 | +For more examples of Terraform modules for VM, see the other directories in the [`terraform-modules` repository](https://github.com/canonical/terraform-modules/tree/main/modules/machine). |
| 166 | +[/note] |
| 167 | + |
| 168 | +Feel free to [contact us](/t/11867) if you have any question and [collaborate with us on GitHub](https://github.com/canonical/terraform-modules)! |
0 commit comments