The following will show how easy is to implement an ephemeral workspace using Terrakube custom schedules and templates with the remote CLI-driven workflow.
The first step will be to create a new organization, lets call it "playground".
Once we have the playground organization, we need to add a team with access to create templates like the following:
We will also need a team with access to create/delete a workspace only, like the following:
Teams names will depend on your Dex configuration.
Once we have the two teams in our new playground organization, we can proceed and create a new template that we will be using to auto destroy all the workspace:
Lets call it "delete-playground" and it will have the following code:
Now we can update the default template that is used when we are using the remote CLI-driven workflow, this template is inside every organization by default and is called "Terraform-Plan/Apply-Cli", usually we don't need to update this template but we will do some small changes to enable ephemeral workspaces in the playground organization.
We need to go the the templates inside the organization settings and edit the template
We will add a new step in the template, this will allow to schedule a job that will be using the "delete-playground" template that we have created above.
We need to use the following template code:
If we pay special attention we just add a new section where we define that the schedule will run every five minutes after the Terraform apply is completed.
In this example we will schedule the template every five minutes for testing purposes.
The schedule is using Quartz format, to learn more about this use this link.
Now we need to define our Terraform code, lets use the following simple example:
Run terraform login to connect to our Terrakube instance:
Now we can run terraform init to initialize our workspace inside the playground organization, lets use "myplayground" for the name of our new workspace
Let's run terraform apply and create our resources:
Our new workspace is created and if we go the organization we can see all the information
The job will be running to create the resources:
We can see that our job is completed and the setup auto destroy have created a new schedule for our workspace:
We could go to the schedules tab:
This schedule will run in 5 minutes:
After waiting for 5 minutes we will see that Terrakube have created a new Job automatically
If we check the details for the new job we can see that a terraform destroy will be executed:
All of the workspace resources are deleted and the workspace will be deleted automatically after the destroy is completed.
Once the Job is completed, our workspace information is deleted from Terrakube
If we are using AWS, AZURE, GCP or any other terraform provider that allow to inject the credentials using environment variables we could use "global variables" to define those.
Global variables will be injected automatically to any workspace inside the playground organization.