|
| 1 | +--- |
| 2 | +page_title: "Using Scaleway Messaging and Queuing service with NATS Terraform provider" |
| 3 | +description: |- |
| 4 | +Using Scaleway Messaging and Queuing service with NATS Terraform provider |
| 5 | +--- |
| 6 | + |
| 7 | +# How to use Scaleway Messaging and Queuing config |
| 8 | + |
| 9 | +This guide shows the combination of Scaleway Messaging and Queuing configuration with to terraform NATS Jetstream |
| 10 | +provider. Il will allow you to provision and manage NATS Jetstream resources. |
| 11 | + |
| 12 | +At the end of this guide, you will have a running NATS server and a configured NATS CLI to interact with it. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +* First, you will need to set up a new Terraform configuration file |
| 17 | + with [Scaleway](https://registry.terraform.io/providers/scaleway/scaleway/latest/docs/resources/mnq_namespace) |
| 18 | + and [Jetstream provider](https://registry.terraform.io/providers/nats-io/jetstream/latest/docs/guides/setup). |
| 19 | + |
| 20 | +```hcl |
| 21 | +terraform { |
| 22 | + required_providers { |
| 23 | + scaleway = { |
| 24 | + source = "scaleway/scaleway" |
| 25 | + } |
| 26 | + jetstream = { |
| 27 | + source = "nats-io/jetstream" |
| 28 | + version = "~> 0.0.34" |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | +
|
| 33 | +provider "scaleway" { |
| 34 | + region = "fr-par" |
| 35 | + access_key = "<SCW_ACCESS_KEY>" |
| 36 | + secret_key = "<SCW_SECRET_KEY>" |
| 37 | + project_id = "<SCW_DEFAULT_PROJECT_ID>" |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +* Next, you should create a Messaging and Queuing namespace. Check our example below. |
| 42 | + |
| 43 | +```hcl |
| 44 | +resource "scaleway_mnq_namespace" "main" { |
| 45 | + name = "mnq-ns" |
| 46 | + protocol = "nats" |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +* Before creating your credentials, be aware that due to an |
| 51 | + ongoing [issue](https://github.com/hashicorp/terraform/issues/516) since 2014, secrets stored in `terraform.tfstate` |
| 52 | + remain in plain text. While there are methods to remove secrets from state files, they are unreliable and may not |
| 53 | + function properly with updates to Terraform. It is not recommended to use these workarounds. |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +* Otherwise, the official update on December3, 2020: |
| 58 | + Terraform 0.14 has added the ability to mark variables as sensitive, which helps keep them out of your logs, so you |
| 59 | + should add `sensitive = true` to variables! |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +* You can create Credentials easily using the Scaleway provider. |
| 64 | + Check our example below and more about authenticating with a Credentials |
| 65 | + File [here](https://docs.nats.io/using-nats/developer/connecting/creds) |
| 66 | + |
| 67 | +```hcl |
| 68 | +resource "scaleway_mnq_credential" "main" { |
| 69 | + name = "creds-ns" |
| 70 | + namespace_id = scaleway_mnq_namespace.main.id |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +* At this point you have your Namespace and your Credential that means you have a running NATS Server ready to be used. |
| 75 | + |
| 76 | +* Grab a copy of the [NATS CLI](https://github.com/nats-io/jetstream/releases) and configure it with your |
| 77 | + endpoint. To be practical let's use `contexts` that you can store and easily select the relevant context. Check more |
| 78 | + details [here](https://docs.nats.io/using-nats/nats-tools/nats_cli#configuration-contexts). |
| 79 | + |
| 80 | +```shell |
| 81 | +nats context save example --server nats://nats.mnq.fr-par.scw.cloud:4222 --description 'Prod.Net Server' |
| 82 | +``` |
| 83 | + |
| 84 | +* The output should look like this: |
| 85 | + |
| 86 | +```shell |
| 87 | +NATS Configuration Context "example" |
| 88 | + |
| 89 | + Description: Prod.Net Server |
| 90 | + Server URLs: nats://nats.mnq.fr-par.scw.cloud:4222 |
| 91 | + Path: /Your/path/context/example.json |
| 92 | +``` |
| 93 | + |
| 94 | +* Try to select your configuration using the NATS CLI: |
| 95 | + |
| 96 | +```shell |
| 97 | +nats context select |
| 98 | +? Select a Context [Use arrows to move, type to filter] |
| 99 | +> example |
| 100 | +``` |
| 101 | + |
| 102 | +* Finally, configure the CLI with the namespace credentials. |
| 103 | + |
| 104 | +```shell |
| 105 | +NATS Configuration Context "example" |
| 106 | + |
| 107 | + Description: Prod.Net Server |
| 108 | + Server URLs: nats://nats.mnq.fr-par.scw.cloud:4222 |
| 109 | + Credentials: /Your/path/secret/admin.creds (OK) |
| 110 | + Path: /Your/path/context/example.json |
| 111 | +``` |
| 112 | + |
| 113 | +* You are ready to use the |
| 114 | + NATS [JetStream Provider](https://registry.terraform.io/providers/nats-io/jetstream/latest/docs): |
| 115 | + |
| 116 | +```hcl |
| 117 | +provider "jetstream" { |
| 118 | + servers = scaleway_mnq_namespace.manin.endpoint |
| 119 | + credentials = "path/ngs_stream_admin.creds" |
| 120 | + # credential_data = "<SCW_CREDENTIAL_AS_STRING>" |
| 121 | +} |
| 122 | +``` |
0 commit comments