├── .devcontainer ├── Dockerfile └── devcontainer.json ├── .gitignore ├── .terraform.lock.hcl ├── LICENSE ├── README.md ├── cloud-init-user-data ├── examples └── ubuntu_dev1-s_full │ ├── .terraform.lock.hcl │ ├── README.md │ └── main.tf ├── main.tf ├── outputs.tf ├── variables.tf └── versions.tf /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/debian/.devcontainer/base.Dockerfile 2 | 3 | # [Choice] Debian version (use bullseye on local arm64/Apple Silicon): bullseye, buster 4 | ARG VARIANT="buster" 5 | FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT} 6 | 7 | # ** [Optional] Uncomment this section to install additional packages. ** 8 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 9 | # && apt-get -y install --no-install-recommends 10 | 11 | RUN sudo chsh -s /usr/bin/zsh vscode -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/debian 3 | { 4 | "name": "Debian", 5 | "build": { 6 | "dockerfile": "Dockerfile", 7 | // Update 'VARIANT' to pick an Debian version: bullseye, buster 8 | // Use bullseye on local arm64/Apple Silicon. 9 | "args": { "VARIANT": "bullseye" } 10 | }, 11 | 12 | // Set *default* container specific settings.json values on container create. 13 | "settings": {}, 14 | 15 | // Add the IDs of extensions you want installed when the container is created. 16 | "extensions": [], 17 | 18 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 19 | // "forwardPorts": [], 20 | 21 | // Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker. 22 | // "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ], 23 | 24 | // Uncomment when using a ptrace-based debugger like C++, Go, and Rust 25 | // "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], 26 | 27 | // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 28 | "remoteUser": "vscode", 29 | "features": { 30 | "terraform": "latest" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # .tfvars files 9 | *.tfvars 10 | -------------------------------------------------------------------------------- /.terraform.lock.hcl: -------------------------------------------------------------------------------- 1 | # This file is maintained automatically by "terraform init". 2 | # Manual edits may be lost in future updates. 3 | 4 | provider "registry.terraform.io/hashicorp/null" { 5 | version = "3.1.0" 6 | hashes = [ 7 | "h1:vpC6bgUQoJ0znqIKVFevOdq+YQw42bRq0u+H3nto8nA=", 8 | "zh:02a1675fd8de126a00460942aaae242e65ca3380b5bb192e8773ef3da9073fd2", 9 | "zh:53e30545ff8926a8e30ad30648991ca8b93b6fa496272cd23b26763c8ee84515", 10 | "zh:5f9200bf708913621d0f6514179d89700e9aa3097c77dac730e8ba6e5901d521", 11 | "zh:9ebf4d9704faba06b3ec7242c773c0fbfe12d62db7d00356d4f55385fc69bfb2", 12 | "zh:a6576c81adc70326e4e1c999c04ad9ca37113a6e925aefab4765e5a5198efa7e", 13 | "zh:a8a42d13346347aff6c63a37cda9b2c6aa5cc384a55b2fe6d6adfa390e609c53", 14 | "zh:c797744d08a5307d50210e0454f91ca4d1c7621c68740441cf4579390452321d", 15 | "zh:cecb6a304046df34c11229f20a80b24b1603960b794d68361a67c5efe58e62b8", 16 | "zh:e1371aa1e502000d9974cfaff5be4cfa02f47b17400005a16f14d2ef30dc2a70", 17 | "zh:fc39cc1fe71234a0b0369d5c5c7f876c71b956d23d7d6f518289737a001ba69b", 18 | "zh:fea4227271ebf7d9e2b61b89ce2328c7262acd9fd190e1fd6d15a591abfa848e", 19 | ] 20 | } 21 | 22 | provider "registry.terraform.io/hashicorp/template" { 23 | version = "2.2.0" 24 | hashes = [ 25 | "h1:94qn780bi1qjrbC3uQtjJh3Wkfwd5+tTtJHOb7KTg9w=", 26 | "zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386", 27 | "zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53", 28 | "zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603", 29 | "zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16", 30 | "zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776", 31 | "zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451", 32 | "zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae", 33 | "zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde", 34 | "zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d", 35 | "zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2", 36 | ] 37 | } 38 | 39 | provider "registry.terraform.io/scaleway/scaleway" { 40 | version = "2.2.0" 41 | constraints = "~> 2.0" 42 | hashes = [ 43 | "h1:4M58uMZHO9fYFPBug868UjaDo+EutwtEGSsCHi4E3UM=", 44 | "zh:016486d3e448630e29595412b8d31c8a3b2cec83fa531a86ef3d3858f6ebe45d", 45 | "zh:0ae1eb5142f866da8849475d976da21a2f7cc905f37c99e7aef810c3369e0f65", 46 | "zh:0c2c8bd7b8beb49d6320cbb4dfba647a465dc1d0f3583b01ad7f0b0559cc043a", 47 | "zh:1a2ddae054c2e21db960d25432406f01ac0aca7529838176c11a892f62987a8f", 48 | "zh:211c92dd31513b06d679f1d9d85ed39911ee32a5c03a2af93e7a5710cb6d0a64", 49 | "zh:76ae419f8ebdc39236c14f87b6c03c9b1f2c6f60081fec48d7d3531cadafcdbe", 50 | "zh:8a951e6fb2c329d0095edb607e5c760c47838956d7b0a75111692bd77158a445", 51 | "zh:9ff0638bd03a39aacf331912ff1547d31c5a5a3e1961bf3ca50a581ae2bf0cba", 52 | "zh:a3ece235aefa0a3110ecae34304122fe32a566a9d54123593e9434693b03d764", 53 | "zh:a47f101c77be03df35179474110ab05397693ce7efb74c5a133ae3cb0041bb88", 54 | "zh:a69562aaf71b7ef7b2c8ef345637a3309762f45d960560453595ea2ec2cb0e2c", 55 | "zh:aa4b8699e888b08905271a6a2c5da206ac737742b8a947a1d9ed53c875954cd5", 56 | "zh:abb3e3e235b4862ead53397dd09be40c91910931a993b9415185ea6e8492fdb4", 57 | "zh:e772b6ccb6d70a2d610c696185d3411548c73ad6e10870f70921021134ef1e67", 58 | ] 59 | } 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Scaleway development instances using Terraform and cloud-init 2 | 3 | ## Foreword 4 | 5 | This Terraform module can be used to create cheap and disposable development machines with Scaleway cloud provider. For instance, these machines are well suited to be used with [Visual Studio Code SSH remote developement feature](https://code.visualstudio.com/docs/remote/ssh). 6 | 7 | ## Examples 8 | 9 | * [Full-featured development instance on a Scaleway DEV1-S instance running Ubuntu 20.04](examples/ubuntu_dev1-s_full) 10 | 11 | ## Prerequisites 12 | 13 | * Terraform 1.0+ 14 | * A [Scaleway project](https://console.scaleway.com/project/) (default project is fine but [creating a new one](https://www.scaleway.com/en/docs/scaleway-project/) is encouraged) 15 | * Git, OpenSSH 16 | * [jq](https://stedolan.github.io/jq/) (only for magic commands) 17 | 18 | ## Introduction 19 | 20 | 1. Define shell helper function to set Terraform variables 21 | 22 | ``` 23 | set_tfvar() { ( [ $(grep "$1" terraform.tfvars 2>/dev/null | wc -l) -gt 0 ] && sed -i -e "/^$1 /s/=.*$/= $(echo $2 | sed 's|\"|\\"|g' | sed 's|/|\\/|g')/" terraform.tfvars ) || echo "$1 = $2" >> terraform.tfvars } 24 | ``` 25 | 26 | > This shell function takes two arguments (the variable key and value) and put the *key = value* in the *terraform.tfvars* file regardless of the existence of the variable (create or replace behaviour) 27 | 28 | 2. Create your SSH key pair and [add the public key in authorized keys of your Scleway project in the Scaleway console](https://console.scaleway.com/project/credentials) 29 | 30 | ``` 31 | ssh-keygen -t ed25519 -q -C 'scaleway' -N '' -f ~/.ssh/scaleway 32 | ``` 33 | 34 | > if using an existing SSH key, it is assumed its name is *~/.ssh/scaleway* 35 | 36 | Add the SSH key file in Terraform variables 37 | ``` 38 | set_tfvar ssh_key_file '"~/.ssh/scaleway"' 39 | ``` 40 | 41 | 3. [Retrieve your project credentials](https://console.scaleway.com/project/credentials) and export them: 42 | 43 | ``` 44 | export SCW_DEFAULT_PROJECT_ID= 45 | export SCW_ACCESS_KEY= 46 | export SCW_SECRET_KEY= 47 | export SCW_DEFAULT_REGION=fr-par 48 | export SCW_DEFAULT_ZONE=fr-par-1 49 | ``` 50 | 51 | ## Usage 52 | 53 | 1. Clone this repository 54 | 55 | ``` 56 | git clone https://github.com/debovema/terraform-scaleway-dev-instance.git 57 | cd terraform-scaleway-dev-instance 58 | ``` 59 | 60 | 2. Initialize Terraform 61 | 62 | ``` 63 | terraform init 64 | ``` 65 | 66 | 3. Define username 67 | 68 | ``` 69 | set_tfvar username '"developer"' 70 | ``` 71 | 72 | > Mind the double quotes 73 | 74 | 4. Select optional features 75 | 76 | * [Oh My ZSH](https://ohmyz.sh/): 77 | ``` 78 | set_tfvar feature_omz true 79 | ``` 80 | 81 | * [Docker](https://www.docker.com/): 82 | ``` 83 | set_tfvar feature_docker true 84 | ``` 85 | 86 | * [nvm](https://github.com/nvm-sh/nvm) (for Node developments): 87 | ``` 88 | set_tfvar feature_nvm true 89 | ``` 90 | 91 | * [SDKMAN!](https://sdkman.io/) (for Java developments): 92 | ``` 93 | set_tfvar feature_sdkman true 94 | ``` 95 | 96 | 5. Apply default plan 97 | ``` 98 | terraform apply 99 | ``` 100 | 101 | 6. SSH to the (first) created host 102 | 103 | ``` 104 | eval `terraform output --json ssh_commands | jq -r ".[0]"` 105 | ``` 106 | 107 | > To display the SSH command used to connect: 108 | > ``` 109 | > echo $(terraform output --json ssh_commands | jq -r ".[0]") 110 | > ``` -------------------------------------------------------------------------------- /cloud-init-user-data: -------------------------------------------------------------------------------- 1 | #cloud-config 2 | 3 | # users 4 | users: 5 | - name: ${user} 6 | sudo: ALL=(ALL) NOPASSWD:ALL 7 | shell: /bin/zsh 8 | groups: docker 9 | 10 | # packages 11 | package_update: true 12 | package_upgrade: true 13 | 14 | packages: 15 | - curl 16 | - jq 17 | - git 18 | - sudo 19 | - unzip 20 | - zip 21 | - zsh 22 | 23 | runcmd: 24 | # copy authorized SSH keys for the created user 25 | - 'mkdir -p /home/${user}/.ssh' 26 | - 'cp /root/.ssh/authorized_keys /home/${user}/.ssh/authorized_keys' 27 | - 'chown ${user}:root /home/${user}/.ssh/authorized_keys' 28 | %{if feature_omz ~} 29 | # install Oh My ZSH for root 30 | - 'git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git /root/.oh-my-zsh' 31 | - 'cp /root/.oh-my-zsh/templates/zshrc.zsh-template /root/.zshrc' 32 | - 'sed -i "s/ZSH_THEME=\".*\"/ZSH_THEME=\"ys\"/" /root/.zshrc' 33 | - 'chsh -s /usr/bin/zsh' 34 | # install Oh My ZSH for user 35 | - 'runuser -l ${user} -c "git clone --depth=1 https://github.com/robbyrussell/oh-my-zsh.git /home/${user}/.oh-my-zsh"' 36 | - 'runuser -l ${user} -c "cp /home/${user}/.oh-my-zsh/templates/zshrc.zsh-template /home/${user}/.zshrc"' 37 | - 'sed -i "s/ZSH_THEME=\".*\"/ZSH_THEME=\"ys\"/" /home/${user}/.zshrc' 38 | %{endif~} 39 | %{if feature_docker ~} 40 | # install Docker 41 | - 'apt-get update' 42 | - 'apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release' 43 | - 'curl -fsSL https://download.docker.com/linux/${distrib}/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg' 44 | - 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/${distrib} $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null' 45 | - 'apt-get update' 46 | - 'apt-get install -y docker-ce docker-ce-cli containerd.io' 47 | %{endif~} 48 | %{if feature_nvm ~} 49 | # install NVM 50 | - 'runuser -l ${user} -c "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/$(curl -s https://api.github.com/repos/nvm-sh/nvm/releases/latest | jq -r .tag_name)/install.sh | bash"' 51 | - 'runuser -l ${user} -c "touch /home/${user}/.zshrc"' 52 | - | 53 | cat << 'EOF' >> /home/${user}/.zshrc 54 | export NVM_DIR="$([ -z "$${XDG_CONFIG_HOME-}" ] && printf %s "$${HOME}/.nvm" || printf %s "$${XDG_CONFIG_HOME}/nvm")" 55 | [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm 56 | EOF 57 | %{endif~} 58 | %{if feature_sdkman ~} 59 | # install SDKMAN! 60 | - 'runuser -l ${user} -c "curl -s "https://get.sdkman.io" | bash"' 61 | - 'runuser -l ${user} -c "touch /home/${user}/.zshrc"' 62 | - | 63 | cat << 'EOF' >> /home/${user}/.zshrc 64 | source "$HOME/.sdkman/bin/sdkman-init.sh" 65 | EOF 66 | %{endif~} 67 | # 68 | power_state: 69 | delay: "+1" 70 | timeout: 0 71 | mode: reboot 72 | message: Reboot after system upgrade 73 | condition: True 74 | -------------------------------------------------------------------------------- /examples/ubuntu_dev1-s_full/.terraform.lock.hcl: -------------------------------------------------------------------------------- 1 | # This file is maintained automatically by "terraform init". 2 | # Manual edits may be lost in future updates. 3 | 4 | provider "registry.terraform.io/hashicorp/null" { 5 | version = "3.1.0" 6 | hashes = [ 7 | "h1:vpC6bgUQoJ0znqIKVFevOdq+YQw42bRq0u+H3nto8nA=", 8 | "zh:02a1675fd8de126a00460942aaae242e65ca3380b5bb192e8773ef3da9073fd2", 9 | "zh:53e30545ff8926a8e30ad30648991ca8b93b6fa496272cd23b26763c8ee84515", 10 | "zh:5f9200bf708913621d0f6514179d89700e9aa3097c77dac730e8ba6e5901d521", 11 | "zh:9ebf4d9704faba06b3ec7242c773c0fbfe12d62db7d00356d4f55385fc69bfb2", 12 | "zh:a6576c81adc70326e4e1c999c04ad9ca37113a6e925aefab4765e5a5198efa7e", 13 | "zh:a8a42d13346347aff6c63a37cda9b2c6aa5cc384a55b2fe6d6adfa390e609c53", 14 | "zh:c797744d08a5307d50210e0454f91ca4d1c7621c68740441cf4579390452321d", 15 | "zh:cecb6a304046df34c11229f20a80b24b1603960b794d68361a67c5efe58e62b8", 16 | "zh:e1371aa1e502000d9974cfaff5be4cfa02f47b17400005a16f14d2ef30dc2a70", 17 | "zh:fc39cc1fe71234a0b0369d5c5c7f876c71b956d23d7d6f518289737a001ba69b", 18 | "zh:fea4227271ebf7d9e2b61b89ce2328c7262acd9fd190e1fd6d15a591abfa848e", 19 | ] 20 | } 21 | 22 | provider "registry.terraform.io/hashicorp/template" { 23 | version = "2.2.0" 24 | hashes = [ 25 | "h1:94qn780bi1qjrbC3uQtjJh3Wkfwd5+tTtJHOb7KTg9w=", 26 | "zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386", 27 | "zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53", 28 | "zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603", 29 | "zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16", 30 | "zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776", 31 | "zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451", 32 | "zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae", 33 | "zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde", 34 | "zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d", 35 | "zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2", 36 | ] 37 | } 38 | 39 | provider "registry.terraform.io/scaleway/scaleway" { 40 | version = "2.1.0" 41 | constraints = "~> 2.0" 42 | hashes = [ 43 | "h1:IfyjOGvS3B9fsA1s4XSr0ajVlufSMq/bbqAX8DODdcY=", 44 | "zh:0de647cba4e5a5973984a0e223aa5b6dc06022a0ed9ddc491d4cc1543d4516f0", 45 | "zh:1400a65eaacab234db4734966e79d8891def0e5b9a10f7bad34efcfd6b42ac78", 46 | "zh:1441133bdae2cbaf34203d831a49c535177533023281e1f884bc699adcdcd94d", 47 | "zh:1d3efd87c78f6ab27e4df189510eb84e0b9f1f4eb0d8c0b983e6795824ceb6b3", 48 | "zh:30db64a0dcc43784062ae09dc2b2741fba657577804e7f130aeca2bed8ee6f8c", 49 | "zh:359d420de3ca104c73acd6a0d84af445ee9d6a2759b7131a4f378e786f8cc027", 50 | "zh:6f6a1781251881309289f4ce2aa4289a8715f33ac50603dcb9beae047450892f", 51 | "zh:731de7d68e140f21004506a3afdf7c1af0e285c986e98c53ebde113deb5d4b94", 52 | "zh:90f4fcdce03b46da0b1da88888d9998a61f2f5f50566f88693bc98bdff9117ff", 53 | "zh:a88b770b34cddf506c0c38c98dd124dc7388c6c1baafe546f60f09ba6a1e5691", 54 | "zh:ae7f8250c07e38e86e15bb4f27de57756d49914ee0146298dd0b6f140380a62c", 55 | "zh:cc3f1ad2cb22e50a578cdda1e02bf94f8ce85cebf691263e4b7da8e864e3195d", 56 | "zh:cfb66ebd80a07f160da683d05c6ce03faa2c3218998eaa2df7b1e745026bd4b8", 57 | ] 58 | } 59 | -------------------------------------------------------------------------------- /examples/ubuntu_dev1-s_full/README.md: -------------------------------------------------------------------------------- 1 | # Example : Full-featured development instance on a Scaleway DEV1-S instance running Ubuntu 20.04 2 | 3 | Read the [module documentation](../../README.md) for further explanation. 4 | 5 | ## Prerequisites 6 | 7 | * Terraform 1.0+ 8 | * A [Scaleway project](https://console.scaleway.com/project/) (default project is fine but [creating a new one](https://www.scaleway.com/en/docs/scaleway-project/) is encouraged) 9 | * curl, OpenSSH 10 | * [jq](https://stedolan.github.io/jq/) (only for magic commands) 11 | 12 | ## Introduction 13 | 14 | 1. Create your SSH key pair and [add the public key in authorized keys of your Scleway project in the Scaleway console](https://console.scaleway.com/project/credentials) 15 | 16 | ``` 17 | ssh-keygen -t rsa -b 4096 -q -C 'scaleway' -N '' -f ~/.ssh/scaleway 18 | ``` 19 | 20 | > if using an existing SSH key, it is assumed its name is *~/.ssh/scaleway* 21 | 22 | 2. [Retrieve your project credentials](https://console.scaleway.com/project/credentials) and export them: 23 | 24 | ``` 25 | export SCW_DEFAULT_PROJECT_ID= 26 | export SCW_ACCESS_KEY= 27 | export SCW_SECRET_KEY= 28 | export SCW_DEFAULT_REGION=fr-par 29 | export SCW_DEFAULT_ZONE=fr-par-1 30 | ``` 31 | 32 | ## Usage 33 | 34 | 1. Retrieve the (root) Terraform module of this example 35 | 36 | ``` 37 | mkdir ubuntu_dev1-s_full 38 | cd ubuntu_dev1-s_full 39 | curl -fsSL -O https://raw.githubusercontent.com/debovema/terraform-scaleway-dev-instance/main/examples/ubuntu_dev1-s_full/main.tf 40 | ``` 41 | 42 | 2. Initialize Terraform 43 | 44 | ``` 45 | terraform init 46 | ``` 47 | 48 | 3. Apply default plan 49 | ``` 50 | terraform apply 51 | ``` 52 | 53 | 4. SSH to the (first) created host 54 | 55 | ``` 56 | eval `terraform output --json ssh_commands | jq -r ".[0]"` 57 | ``` 58 | 59 | > To display the SSH command used to connect: 60 | > ``` 61 | > echo $(terraform output --json ssh_commands | jq -r ".[0]") 62 | > ``` -------------------------------------------------------------------------------- /examples/ubuntu_dev1-s_full/main.tf: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------- 2 | // Variables 3 | variable "dev_ssh_key_file" { default = "~/.ssh/scaleway" } 4 | variable "dev_username" { default = "developer" } 5 | 6 | //-------------------------------------------------------------------- 7 | // Modules 8 | module "dev" { 9 | source = "app.terraform.io/scwdev/dev/scaleway" 10 | version = "0.0.2" 11 | 12 | feature_docker = "true" 13 | feature_nvm = "true" 14 | feature_omz = "true" 15 | feature_sdkman = "true" 16 | ssh_key_file = var.dev_ssh_key_file 17 | username = var.dev_username 18 | } 19 | 20 | //-------------------------------------------------------------------- 21 | // Outputs 22 | output "public_ips" { 23 | value = module.dev.public_ips 24 | } 25 | 26 | output "ssh_commands" { 27 | value = module.dev.ssh_commands 28 | } 29 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | data "template_file" "userdata" { 2 | template = file("${path.module}/cloud-init-user-data") 3 | 4 | vars = { 5 | distrib = regex("^ubuntu|debian", var.server_image) 6 | user = var.username 7 | feature_docker = var.feature_docker 8 | feature_nvm = var.feature_nvm 9 | feature_omz = var.feature_omz 10 | feature_sdkman = var.feature_sdkman 11 | } 12 | } 13 | 14 | resource "scaleway_instance_server" "node" { 15 | count = var.node_count 16 | 17 | name = "${var.node_name}-${count.index}" 18 | 19 | image = var.server_image 20 | type = var.server_type 21 | enable_dynamic_ip = true 22 | 23 | # initialization sequence 24 | cloud_init = data.template_file.userdata.rendered 25 | } 26 | 27 | resource "null_resource" "wait_for_init" { 28 | count = var.node_count 29 | 30 | provisioner "remote-exec" { 31 | connection { 32 | host = scaleway_instance_server.node[count.index].public_ip 33 | user = "root" 34 | private_key = file(var.ssh_key_file) 35 | } 36 | 37 | inline = [ 38 | "touch /var/log/cloud-init-output.log", 39 | "tail -f /var/log/cloud-init-output.log &", 40 | "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do sleep 10; done;", 41 | ] 42 | } 43 | 44 | provisioner "local-exec" { 45 | command = "sleep 80" # wait more than 1 minute for the instance to be rebooted 46 | } 47 | } -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "public_ips" { 2 | value = scaleway_instance_server.node.*.public_ip 3 | } 4 | 5 | output "ssh_commands" { 6 | value = [ 7 | for ip in scaleway_instance_server.node.*.public_ip : "ssh -i ${var.ssh_key_file} ${var.username}@${ip}" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "node_count" { 2 | type = string 3 | default = "1" 4 | } 5 | 6 | variable "node_name" { 7 | type = string 8 | default = "cloud-init" 9 | } 10 | 11 | variable "server_image" { 12 | type = string 13 | default = "ubuntu_focal" 14 | validation { 15 | condition = can(regex("^(debian_|ubuntu_).*$", var.server_image)) 16 | error_message = "The server_image value must start with ubuntu_ or debian_ (ubuntu_focal, debian_buster, ...)." 17 | } 18 | } 19 | 20 | variable "server_type" { 21 | type = string 22 | default = "DEV1-S" 23 | validation { 24 | condition = can(regex("^(DEV1-(S|M|L|XL))|(GP1-(XS|S|M|L|XL))$", var.server_type)) 25 | error_message = "The server_type value must be a valid Scaleway instance type (DEV1-S, DEV1-M, DEV1-L, DEV1-XL, GP1-XS, GP1-S, GP1-M, GP1-L, GP1-XL)." 26 | } 27 | } 28 | 29 | variable "ssh_key_file" { 30 | type = string 31 | } 32 | 33 | variable "username" { 34 | type = string 35 | default = "user" 36 | } 37 | 38 | # optional features 39 | variable "feature_omz" { 40 | type = bool 41 | description = "Whether to install Oh My ZSH or not" 42 | default = false 43 | } 44 | 45 | variable "feature_docker" { 46 | type = bool 47 | description = "Whether to install Docker or not" 48 | default = false 49 | } 50 | 51 | variable "feature_nvm" { 52 | type = bool 53 | description = "Whether to install NVM or not" 54 | default = false 55 | } 56 | 57 | variable "feature_sdkman" { 58 | type = bool 59 | description = "Whether to install SDKMAN! or not" 60 | default = false 61 | } 62 | -------------------------------------------------------------------------------- /versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | scaleway = { 4 | source = "scaleway/scaleway" 5 | version = "~> 2.0" 6 | } 7 | } 8 | required_version = ">= 1.0" 9 | } 10 | --------------------------------------------------------------------------------