Skip to content

Commit bbfee63

Browse files
[DPE-7796] Tweak Terraform modules (#689)
1 parent 3063901 commit bbfee63

File tree

8 files changed

+97
-78
lines changed

8 files changed

+97
-78
lines changed

.github/workflows/ci.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,22 @@ on:
2424
jobs:
2525
lint:
2626
name: Lint
27-
uses: canonical/data-platform-workflows/.github/workflows/[email protected]
27+
runs-on: ubuntu-latest
28+
timeout-minutes: 5
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v5
32+
- name: Install terraform
33+
run: |
34+
sudo snap install terraform --classic
35+
- name: Install tox & poetry
36+
run: |
37+
pipx install tox
38+
pipx install poetry
39+
- name: Run linters
40+
run: |
41+
tox run -e lint
42+
tox run -e lint-terraform
2843
2944
unit-test:
3045
name: Unit test charm

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*.py[cod]
55
.coverage
66
coverage.xml
7+
.terraform
8+
.terraform.lock.hcl
79
.tox/
810
.idea/
911
.vscode/

docs/how-to/deploy/terraform.md

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11

22
# How to deploy using Terraform
33

4-
[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).
5-
6-
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.
4+
[Terraform](https://www.terraform.io/) is an infrastructure automation tool to provision and manage resources in clouds or data centers.
5+
To deploy Charmed MySQL using Terraform and Juju, you can use the [Juju Terraform Provider](https://registry.terraform.io/providers/juju/juju/latest).
76

87
For an in-depth introduction to the Juju Terraform Provider, read [this Discourse post](https://discourse.charmhub.io/t/6939).
98

10-
```{note}
11-
Storage support was added in [Juju Terraform Provider version 0.13+](https://github.com/juju/terraform-provider-juju/releases/tag/v0.13.0).
12-
```
13-
149
## Install Terraform tooling
1510

16-
This guide assumes Juju is installed and you have an LXD controller already bootstrapped. For more information, check the [Charmed MySQL tutorial](/tutorial/index).
11+
This guide assumes Juju is installed, and you have an LXD controller already bootstrapped. For more information, check the [Charmed MySQL tutorial](/tutorial/index).
1712

1813
Let's install Terraform Provider and example modules:
1914
```shell
2015
sudo snap install terraform --classic
2116
```
17+
2218
Switch to the LXD provider and create a new model:
2319
```shell
2420
juju switch lxd
2521
juju add-model my-model
2622
```
27-
Clone examples and navigate to the MySQL machine module:
23+
24+
Clone the MySQL operator repository and navigate to the terraform module:
2825
```shell
29-
git clone https://github.com/canonical/terraform-modules.git
30-
cd terraform-modules/modules/machine/mysql
26+
git clone https://github.com/canonical/mysql-operator.git
27+
cd terraform
3128
```
3229

3330
Initialise the Juju Terraform Provider:
@@ -37,35 +34,18 @@ terraform init
3734

3835
## Verify the deployment
3936

40-
Open the `main.tf` file to see the brief contents of the Terraform module:
41-
42-
```tf
43-
resource "juju_application" "machine_mysql" {
44-
name = var.mysql_application_name
45-
model = var.juju_model_name
46-
trust = true
47-
48-
charm {
49-
name = "mysql"
50-
channel = var.mysql_charm_channel
51-
}
52-
53-
units = 1
54-
}
55-
```
56-
57-
Run `terraform plan` to get a preview of the changes that will be made:
37+
Open the `main.tf` file to see the brief contents of the Terraform module, and run `terraform plan` to get a preview of the changes that will be made:
5838

5939
```shell
60-
terraform plan -var "juju_model_name=my-model"
40+
terraform plan -var "model_name=my-model"
6141
```
6242

6343
## Apply the deployment
6444

6545
If everything looks correct, deploy the resources (skip the approval):
6646

6747
```shell
68-
terraform apply -auto-approve -var "juju_model_name=my-model"
48+
terraform apply -auto-approve -var "model_name=my-model"
6949
```
7050

7151
## Check deployment status
@@ -83,7 +63,7 @@ Model Controller Cloud/Region Version SLA Timestamp
8363
my-model lxd-controller localhost/localhost 3.5.3 unsupported 12:49:34Z
8464

8565
App Version Status Scale Charm Channel Rev Exposed Message
86-
mysql 8.0.36-0ubun... active 1 mysql 8.0/stable 240 no
66+
mysql 8.0.41-0ubun... active 1 mysql 8.0/stable 366 no
8767

8868
Unit Workload Agent Machine Public address Ports Message
8969
mysql/0* active idle 0 10.101.248.225 3306,33060/tcp Primary
@@ -96,22 +76,22 @@ Continue to operate the charm as usual from here or apply further Terraform chan
9676

9777
## Clean up
9878

99-
To keep the house clean, remove the newly deployed Charmed MySQL by running
79+
To keep the house clean, remove the newly deployed MySQL charm by running
10080
```shell
101-
terraform destroy -var "juju_model_name=my-model"
81+
terraform destroy -var "model_name=my-model"
10282
```
10383

10484
Sample output:
10585
```shell
106-
juju_application.machine_mysql: Refreshing state... [id=my-model:mysql]
86+
juju_application.mysql_server: Refreshing state... [id=my-model:mysql]
10787

10888
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
10989
- destroy
11090

11191
Terraform will perform the following actions:
11292

113-
# juju_application.machine_mysql will be destroyed
114-
- resource "juju_application" "machine_mysql" {
93+
# juju_application.mysql_server will be destroyed
94+
- resource "juju_application" "mysql_server" {
11595
- constraints = "arch=amd64" -> null
11696
- id = "my-model:mysql" -> null
11797
- model = "my-model" -> null
@@ -122,7 +102,7 @@ Terraform will perform the following actions:
122102
- count = 1 -> null
123103
- label = "database" -> null
124104
- pool = "rootfs" -> null
125-
- size = "99G" -> null
105+
- size = "10G" -> null
126106
},
127107
] -> null
128108
- trust = true -> null
@@ -132,7 +112,7 @@ Terraform will perform the following actions:
132112
- base = "[email protected]" -> null
133113
- channel = "8.0/stable" -> null
134114
- name = "mysql" -> null
135-
- revision = 240 -> null
115+
- revision = 366 -> null
136116
- series = "jammy" -> null
137117
}
138118
}
@@ -148,15 +128,12 @@ Do you really want to destroy all resources?
148128

149129
Enter a value: yes
150130

151-
juju_application.machine_mysql: Destroying... [id=my-model:mysql]
152-
juju_application.machine_mysql: Destruction complete after 0s
131+
juju_application.mysql_server: Destroying... [id=my-model:mysql]
132+
juju_application.mysql_server: Destruction complete after 0s
153133

154134
Destroy complete! Resources: 1 destroyed.
155135
```
156-
---
157-
```{note}
158-
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).
159-
```
160136

161-
Feel free to [contact us](/reference/contacts) if you have any question and [collaborate with us on GitHub](https://github.com/canonical/terraform-modules)!
137+
---
162138

139+
Feel free to [contact us](/reference/contacts) if you have any question and collaborate with us on GitHub!

terraform/main.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
resource "juju_application" "machine_mysql" {
1+
resource "juju_application" "mysql_server" {
22
name = var.app_name
3-
model = var.juju_model_name
3+
model = var.model_name
44

55
charm {
66
name = "mysql"
7+
base = var.base
78
channel = var.channel
89
revision = var.revision
9-
base = var.base
1010
}
1111

1212
storage_directives = {
1313
database = var.storage_size
1414
}
1515

16-
units = var.units
17-
constraints = var.constraints
18-
config = var.config
16+
config = var.config
17+
constraints = var.constraints
18+
endpoint_bindings = var.endpoint_bindings
19+
units = var.units
1920
}

terraform/outputs.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
output "application_name" {
2-
value = juju_application.machine_mysql.name
1+
output "app_name" {
2+
description = "Name of the MySQL Server VM application"
3+
value = juju_application.mysql_server.name
34
}
45

56
output "provides" {
7+
description = "Map of all the provided endpoints"
68
value = {
79
database = "database",
810
cos_agent = "cos-agent",
911
}
1012
}
1113

1214
output "requires" {
15+
description = "Map of all the required endpoints"
1316
value = {
1417
certificates = "certificates"
1518
s3_parameters = "s3-parameters"
19+
tracing = "tracing"
1620
}
1721
}

terraform/variables.tf

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
1-
variable "juju_model_name" {
2-
description = "Juju model name"
1+
variable "model_name" {
2+
description = "Juju model to deploy to"
33
type = string
44
}
55

66
variable "app_name" {
7-
description = "Name of the application in the Juju model."
7+
description = "Name of the juju application"
88
type = string
99
default = "mysql"
1010
}
1111

12+
variable "base" {
13+
description = "Application base"
14+
type = string
15+
default = "[email protected]"
16+
}
17+
18+
variable "config" {
19+
description = "Application configuration. Details at https://charmhub.io/mysql/configurations"
20+
type = map(string)
21+
default = {}
22+
}
23+
24+
variable "constraints" {
25+
description = "Juju constraints for the application"
26+
type = string
27+
default = "arch=amd64"
28+
}
29+
1230
variable "channel" {
13-
description = "Charm channel to use when deploying"
31+
description = "Charm channel to deploy from"
1432
type = string
1533
default = "8.0/stable"
1634
}
1735

1836
variable "revision" {
19-
description = "Revision number to deploy charm"
37+
description = "Charm revision to deploy"
2038
type = number
2139
default = null
2240
}
2341

24-
variable "base" {
25-
description = "Application base"
26-
type = string
27-
default = "[email protected]"
28-
}
29-
3042
variable "units" {
3143
description = "Number of units to deploy"
3244
type = number
33-
default = 1
34-
}
35-
36-
variable "constraints" {
37-
description = "Juju constraints to apply for this application."
38-
type = string
39-
default = "arch=amd64"
45+
default = 3
4046
}
4147

4248
variable "storage_size" {
@@ -45,8 +51,10 @@ variable "storage_size" {
4551
default = "10G"
4652
}
4753

48-
variable "config" {
49-
description = "Application configuration. Details at https://charmhub.io/mysql/configurations"
50-
type = map(string)
51-
default = {}
54+
variable "endpoint_bindings" {
55+
description = "Map of endpoint bindings"
56+
type = set(map(string))
57+
default = []
5258
}
59+
60+

terraform/versions.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
terraform {
22
required_version = ">= 1.6.6"
3+
34
required_providers {
45
juju = {
56
source = "juju/juju"
6-
version = ">= 0.14.0"
7+
version = ">= 0.20.0, < 1.0.0"
78
}
89
}
910
}

tox.ini

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ env_list = lint, unit
99
src_path = "{tox_root}/src"
1010
tests_path = "{tox_root}/tests"
1111
lib_path = "{tox_root}/lib/charms/mysql"
12+
terraform_path = "{tox_root}/terraform"
1213
all_path = {[vars]src_path} {[vars]tests_path} {[vars]lib_path}
1314

1415
[testenv]
@@ -43,6 +44,16 @@ commands =
4344
poetry run ruff format --check --diff {[vars]all_path}
4445
find {[vars]all_path} -type f \( -name "*.sh" -o -name "*.bash" \) -exec poetry run shellcheck --color=always \{\} +
4546

47+
[testenv:lint-terraform]
48+
description = Check code against Terraform style standards
49+
allowlist_externals =
50+
{[testenv]allowlist_externals}
51+
terraform
52+
commands =
53+
terraform fmt -check -diff -recursive {[vars]terraform_path}
54+
terraform -chdir={[vars]terraform_path} init -backend=false
55+
terraform -chdir={[vars]terraform_path} validate
56+
4657
[testenv:unit]
4758
description = Run unit tests
4859
set_env =

0 commit comments

Comments
 (0)