Skip to content

A Terraform module to create development instances on Scaleway servers initialized with cloud-init

License

Notifications You must be signed in to change notification settings

debovema/terraform-scaleway-dev-instance

Repository files navigation

Scaleway development instances using Terraform and cloud-init

Foreword

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.

Examples

Prerequisites

Introduction

  1. Define shell helper function to set Terraform variables
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 }

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)

  1. Create your SSH key pair and add the public key in authorized keys of your Scleway project in the Scaleway console
ssh-keygen -t ed25519 -q -C 'scaleway' -N '' -f ~/.ssh/scaleway

if using an existing SSH key, it is assumed its name is ~/.ssh/scaleway

Add the SSH key file in Terraform variables

set_tfvar ssh_key_file '"~/.ssh/scaleway"'
  1. Retrieve your project credentials and export them:
export SCW_DEFAULT_PROJECT_ID=<REDACTED>
export SCW_ACCESS_KEY=<REDACTED>
export SCW_SECRET_KEY=<REDACTED>
export SCW_DEFAULT_REGION=fr-par
export SCW_DEFAULT_ZONE=fr-par-1

Usage

  1. Clone this repository
git clone https://github.com/debovema/terraform-scaleway-dev-instance.git
cd terraform-scaleway-dev-instance
  1. Initialize Terraform
terraform init
  1. Define username
set_tfvar username '"developer"'

Mind the double quotes

  1. Select optional features
set_tfvar feature_omz true
set_tfvar feature_docker true
  • nvm (for Node developments):
set_tfvar feature_nvm true
set_tfvar feature_sdkman true
  1. Apply default plan
terraform apply
  1. SSH to the (first) created host
eval `terraform output --json ssh_commands | jq -r ".[0]"`

To display the SSH command used to connect:

echo $(terraform output --json ssh_commands | jq -r ".[0]")