Last active
January 12, 2020 09:23
-
-
Save Roms1383/dda6bf9af5746548de074135b19ec0a0 to your computer and use it in GitHub Desktop.
handle Serverless provisioning from Terraform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# $1 is the name of the repository | |
# $2 is the optional tag of the repository | |
REMOVE=$(node should-remove.js microservice) | |
if [[ $REMOVE == "true" ]]; then | |
git clone https://github.com/owner/$1.git | |
cd $1/ | |
if [ "$2" != "" ]; then | |
git checkout tags/$2 -b $2 | |
fi | |
yarn install | |
yarn serverless remove | |
cd .. | |
rm -rf $1/ | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# $1 is the name of the repository | |
# $2 is the optional tag of the repository | |
git clone https://github.com/owner/$1.git | |
cd $1/ | |
if [ "$2" != "" ]; then | |
git checkout tags/$2 -b $2 | |
fi | |
yarn install | |
yarn serverless deploy | |
cd .. | |
rm -rf $1/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
locals { microservice_version = "v1.9.0" } | |
resource "null_resource" "microservice" { | |
# either do not define count or set count = 0 for soft deletion | |
depends_on = [null_resource.some_other_resource] | |
triggers = { version = "${local.microservice_version}" } | |
provisioner "local-exec" { | |
# deploy from serverless | |
command = "bash deploy.sh microservice ${local.microservice_version}" | |
# required environment variables | |
environment = { ... } | |
} | |
provisioner "local-exec" { | |
# deploy from serverless | |
command = "bash conditional-removal.sh microservice ${local.microservice_version}" | |
when = destroy | |
# required environment variables | |
environment = { ... } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const hcltojson = require('hcl-to-json') | |
const config = fs.readFileSync(path.join(__dirname, 'main.tf'), { encoding: 'utf8' }) | |
// $1 is the name of the repository | |
const [repo] = process.argv.slice(2) | |
const { resource } = hcltojson(config) | |
const { null_resource } = resource | |
const definition = null_resource[repo] | |
const count = definition.hasOwnProperty('count') ? definition.count : 1 | |
// not marked for deletion | |
if (count === 1) console.log(false) | |
// count should either be undefined or set to 0 | |
else if (count > 1) throw new Error('this script cannot handle multiple count of the same null_resource') | |
// has been marked for deletion | |
else console.log(true) |
Sadly I tested to create a Terraform module locally but even if the script currently executes and does the job, this way is clearly stated as deprecated and might stop to work in future versions of Terraform.
My current version is :
Terraform v0.12.19
+ provider.null v2.1.2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a side note it seems possible to handle multiple
count
on thenull_resource
by introspecting bothterraform.tfstate
andterraform.tfstate.backup
and guessing which one have been added, deleted or had its version updated.Also it's probably possible to do everything in
bash
in case we don't want to rely onJavaScript
for files parsing purpose, but that might require hardcoreawk
commands or installing additional package likejq
(which then would require additional scripts / packages work to work on platforms other than Linux).A better alternative would be to create a Terraform Module to handle all this automatically, but it's far beyond my sample purpose :)