Last active
January 19, 2020 06:43
-
-
Save Roms1383/bbaf293ec95c6209bf7174ae5dbdacf2 to your computer and use it in GitHub Desktop.
Medium - Mate Terraform and Serverless
This file contains 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
module "sls_user" { | |
source = "./microservice" | |
microservice_version = "1.0.0" | |
} | |
module "sls_product" { | |
source = "./microservice" | |
microservice_version = "1.0.0" | |
# microservice_active = false | |
} |
This file contains 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
# WARNING : this file should be located in ./microservice/module.tf | |
# variable name 'version' is already reserved by Terraform, hence the alternate name here | |
variable "microservice_version" { | |
type = string | |
} | |
variable "microservice_active" { | |
type = bool | |
default = true | |
} | |
resource "null_resource" "microservice" { | |
triggers = { version = "${var.microservice_version}" } | |
count = var.microservice_active ? 1 : 0 | |
provisioner "local-exec" { | |
# here you would write the command to deploy with Serverless | |
command = "echo \"install ${var.microservice_version} && serverless deploy\"" | |
} | |
provisioner "local-exec" { | |
# here you would write the command to remove with Serverless | |
command = "echo \"serverless remove\"" | |
when = destroy | |
} | |
} |
This file contains 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
{ | |
"name": "medium-mate-terraform-with-serverless", | |
"version": "1.0.0", | |
"main": "index.js", | |
"author": "Romain KELIFA", | |
"license": "MIT", | |
"scripts": { | |
"tf:init": "terraform init", | |
"tf:plan": "terraform plan -out terraform.plan && terraform show -json terraform.plan > terraform.plan.json", | |
"tf:taint": "bash taint.sh", | |
"tf:apply": "terraform apply --auto-approve", | |
"tf:clean": "rm -f terraform.plan terraform.plan.json", | |
"deploy": "yarn tf:init && yarn tf:plan && yarn tf:taint && yarn tf:apply" | |
} | |
} |
This file contains 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 plan = fs.readFileSync(path.join(__dirname, 'terraform.plan.json'), { encoding: 'utf8' }) | |
const modules = fs.readFileSync(path.join(__dirname, '.terraform', 'modules', 'modules.json'), { encoding: 'utf8' }) | |
const is = {} | |
is.serverless = key => JSON.parse(modules) | |
.Modules | |
.filter(({ Source }) => Source === './microservice') // define Source according to your module source | |
.find(({ Key }) => Key === key) | |
const taint = JSON.parse(plan) | |
.resource_changes | |
.filter(({ module_address }) => is.serverless(module_address.split('.')[1])) // filter the serverless modules | |
.filter(({ change }) => (change.actions.includes('create') && change.actions.includes('delete'))) // output of `triggers` | |
.map(({ address }) => address) | |
taint.forEach(address => console.log(address)) |
This file contains 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
#!/usr/bin/env node | |
for ADDRESS in $(node should-taint.js) | |
do | |
terraform taint $ADDRESS | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment