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