Running a script on removal of a tenant

I’m using tenanted deployments with AWS and ECS. When I add a new tenant to to the project it happily creates the instances (via terraform).

I need to run a script to tear down the AWS instance when I remove a tenant. right now I have a variable that set to “delete” or something similar and that will do the tear down instead of the build, but it would be nicer if there was a way to trigger something when removing a tenant from a project

Hi @adam_vandenhoven,

Thanks for getting in touch!

There isn’t currently a pre-built way of doing this, however, my first thought would be to perhaps leverage the Runbook feature with a scheduled trigger. The first step of the runbook could be to query the Octopus API using the octopus.client for any Tenant deletion events. If one is found it then saves the relevant information(tenant ID/Name) to an output variable and then proceeds with your environment tear down script.

This is a basic script that would locate Tenant deletions, it would need some modification to include things like a date range but should provide a good starting point.

# You can get this dll from https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path 'Octopus.Client.dll' 

$apikey = 'API-xxxxxx' #Your API Key
$octopusURI = '' # Your server address

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = new-object Octopus.Client.OctopusRepository $endpoint

$category = "Deleted"
$document = "*Tenants*"

#Using lambda expression to filter events using the FindMany method
$repository.Events.FindMany(
    {param($e) if(($e.Category -eq $category) -and ($e.RelatedDocumentIds -like $document)){
        $true
        }
    })

Your query has made me wonder if there is room to improve our Runbooks feature by linking them to our Subscriptions feature. Currently, this will send out an email or webhook message when selected events occur on a pre-set schedule. Expanding that to also have the ability to trigger a runbook and pass the event information through would fit your scenario well I think.

I hope this helps get you started, let me know if you have any further queries.

Regards,
Paul

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.