Ephemeral Environments - ECS & Terraform

Introduction

Customers want to be able to test the feature branches quickly and efficiently before merging to master. One way of doing this is by deploying the feature branch artifact on to infrastructure similar to your production environment. Once tests are complete, the user can tear down the infrastructure and merge the code to master. Here at Harness, we do something similar to the idea of PR Driven Development. Every PR results in an ephemeral environment being spun up by Terraform, the new feature code is deployed and is tested in the environment by automation testing and QA. Once fully tested, the environment is spun down, pr is merged to master and the new artifact is built and deployed to prod.

Benefits:

  • Provides the developer with an easy way to test fresh code on a fresh environment
  • It’s a cost-effective way of testing new features
  • Provides a realistic environment for the application
  • We can run automated tests and manual tests and then tear down the infrastructure when we’re done.

How do we do this in Harness?

To preface, this setup is tailored to an ECS Setup. We can do this on any dynamically created infrastructure such as EC2,

Setup:

  • Terraform Code to provide the desired infrastructure
  • Artifact Repository to host the image
  • Harness configured to deploy feature artifact

Integrate your Infrastructure Provisioner

  • In the application, navigate to Infrastructure provisioning
  • Select the GitHub repository hosting your Terraform Code

In order for Harness to know what infrastructure you are deploying to, we need to leverage the terraform outputs. A sample Outputs.tf file may look like:

output "cluster" {

value = "${var.cluster}"

}

output "region" {

value = "${var.region}"

}

output "harness" {

value = "blackbelt"

}

Integrate with your Environment

  • Using the Terraform outputs, reference the outputs in your dynamically created environment within the Harness Application.
  • Refer to the outputs like so:

${terraform.region}

${terraform.cluster}

${terraform.harness}

Workflow Implementation

Select a Canary Workflow to leverage the Dynamic Infrastructure provisioning with ideal testing deployment methodology.

In the Pre Deployment step, please select Add Command and select a Terraform Provision step. Configure it to the Infrastructure Provisioner you created in the application and populate the respective values.

In Phase 1, we create an ECS deployment onto the desired infrastructure like so:

We can run verification on the service:

  • Smoke Tests
  • HTTP Verification
  • AppD
  • Splunk

Putting it all together

At the Post Deployment add a Terraform Destroy, this means all the testing and verification for the artifact is complete and we want to tear down the environment to save on cost!

When constructing the Harness workflow: