New Features
New Features
August 22, 2024

New Feature: Terraform & OpenTofu Ephemeral Workspaces

By
Ryan Fee

Are you tired of spending money on unused resources and having an environment full of old development resources provisioned by Terraform or OpenTofu? Scalr's new ephemeral workspace feature will help you reduce your costs and avoid having old resources that are no longer used.

Why are Ephemeral Workspaces Needed

Before diving into this feature's details, let's consider a common use case many organizations face. Your team uses Terraform to manage infrastructure, creating multiple workspaces for various projects, environments, and experiments. Over time, you notice a troubling trend: a growing number of unused workspaces cluttering your infrastructure, each potentially associated with forgotten or unnecessary resources.

These unused workspaces not only create confusion and technical debt but can also lead to significant and unnecessary infrastructure costs. Resources provisioned in these workspaces continue to run, consuming computing power, storage, and potential security risk, even when they're no longer needed.

How It Works

When you create a new Terraform or OpenTofu workspace using Scalr's ephemeral workspace feature, you set a predetermined lifespan for that workspace. Once the specified time elapses, Scalr automatically initiates a destructive run, which destroys all of the resources and then deletes the workspace, including the state files associated with the workspace.

New workspace creation

This automated cleanup process ensures that:

  1. Unused resources are promptly identified and removed.
  2. Infrastructure costs are kept under control by eliminating forgotten or unnecessary resources.
  3. Your Scale environments remain clean and manageable without the clutter of obsolete workspaces.

Other things to note about the functionality:

  • This is features is currently only available when creating a new workspace.
  • The Terraform run will be stopped if it violates any OPA policies, and users will need to resolve the policies to allow the run to continue.
  • The destruction will respect the “auto-apply” settings in the workspace. If the “Auto apply changes when a Terraform plan is successful” is disabled, then Scalr will automatically start the Terraform plan, and if successful, the run will then wait for an approver to approve the Terraform apply before the resources are destroyed.  If the “Auto apply changes when a Terraform plan is successful” is enabled, Scalr will automatically continue with the Terraform apply and destroy the resources.
  • Users who have the ability to update the workspace can remove the automatic destruction before it happens:
Configured auto-destroy time

Use Cases for Ephemeral Workspaces

Scalr's Terraform ephemeral workspaces feature can be applied to various use cases:

  1. Development and Testing Environments: Create short-lived development or testing environments that automatically clean themselves up after a specified period, ensuring that resources are only consumed when actively needed.
  2. Temporary Demo Environments: Quickly spin up demo environments for client presentations or proof-of-concept work, confident that they won't continue running (and billing) long after the demo is over.
  3. Training and Onboarding: Set up temporary infrastructure for training sessions or new employee onboarding, which can be automatically torn down after the training period ends.

Best Practices for Using Ephemeral Workspaces

To make the most of this new feature, consider the following best practices:

  1. Clearly define workspace lifespans: When creating a new workspace, consider the expected lifespan of your resources when setting up an ephemeral workspace.
  2. Use descriptive naming conventions: Adopt a naming strategy that clearly identifies ephemeral workspaces and their purpose.
  3. Implement proper tagging: Use tags to associate resources with specific projects or teams, making tracking and managing ephemeral infrastructure easier.
  4. Regularly review auto-destroy schedules: Periodically review and adjust the auto-destroy schedules for your ephemeral workspaces to ensure they align with your current needs.

Summary

By automatically cleaning up unused resources and streamlining workspace management, this new feature helps organizations optimize their infrastructure costs, improve security, and enhance overall operational efficiency.

Interested in other ways to clean up your Scalr environment? Check out the stale workspace report in Scalr, which will show out of date or unused workspaces to clean up.

This is available on the Scalr paid and free tier! Try it out today!

Note: While this blog references Terraform, everything mentioned in here also applies to OpenTofu. New to OpenTofu? It is a fork of Terraform 1.5.7 as a result of the license change from MPL to BUSL by HashiCorp. OpenTofu is an open-source alternative to Terraform that is governed by the Linux Foundation. All features available in Terraform 1.5.7 or earlier are also available in OpenTofu. Find out the history of OpenTofu here.

Don't take our word for it, try it for yourself.

A screenshot of the modules page in the Scalr Platform