Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix destroy cmd usage #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,57 +1,70 @@
variable "cmd" {
type = string
description = "The command used to create the resource."
}

variable "destroy_cmd" {
type = string
description = "The command used to destroy the resource."
default = "true"
}

variable "account_id" {
type = string
description = "The account that holds the role to assume in. Will use providers account by default"
default = "0"
default = ""
}

variable "role" {
type = string
description = "The role to assume in order to run the cli command."
default = "0"
default = ""
}

variable "dependency_ids" {
description = "IDs or ARNs of any resources that are a dependency of the resource created by this module."
type = "list"
type = list(string)
default = []
}

data "aws_caller_identity" "id" {}

locals {
account_id = "${var.account_id == 0 ? data.aws_caller_identity.id.account_id : var.account_id}"
account_id = var.account_id == "" ? data.aws_caller_identity.id.account_id : var.account_id
assume_role_cmd = "source ${path.module}/assume_role.sh ${local.account_id} ${var.role}"
}

resource "null_resource" "cli_resource" {
triggers = {
role = var.role
cmd = var.cmd
destroy_cmd = var.destroy_cmd
assume_role_cmd = local.assume_role_cmd
}
provisioner "local-exec" {
when = "create"
command = "/bin/bash -c '${var.role == 0 ? "" : "${local.assume_role_cmd} && "}${var.cmd}'"
when = create
command = "/bin/bash -c '${self.triggers.role == "" ? "" : "${self.triggers.assume_role_cmd} && "}${self.triggers.cmd == "" ? "true" : self.triggers.cmd}'"
}

provisioner "local-exec" {
when = "destroy"
command = "/bin/bash -c '${var.role == 0 ? "" : "${local.assume_role_cmd} && "}${var.destroy_cmd}'"
when = destroy
command = "/bin/bash -c '${self.triggers.role == "" ? "" : "${self.triggers.assume_role_cmd} && "}${self.triggers.destroy_cmd == "" ? "true" : self.triggers.destroy_cmd}'"
}

# By depending on the null_resource, the cli resource effectively depends on the existance
# of the resources identified by the ids provided via the dependency_ids list variable.
depends_on = ["null_resource.dependencies"]
depends_on = [
null_resource.dependencies
]
}

resource "null_resource" "dependencies" {
triggers = {
dependencies = "${join(",", var.dependency_ids)}"
dependencies = join(",", var.dependency_ids)
}
}

output "id" {
description = "The ID of the null_resource used to provison the resource via cli. Useful for creating dependencies between cli resources"
value = "${null_resource.cli_resource.id}"
value = null_resource.cli_resource.id
}
8 changes: 8 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
null = {
source = "hashicorp/null"
version = "~> 3.1"
}
}
}