AWS DevOps Blog

Color-Code Your AWS OpsWorks Stacks for Better Instance and Resource Tracking

AWS OpsWorks provides options for organizing your Amazon EC2 instances and other AWS resources. There are stacks to group related resources and isolate them from each other; layers to group instances with similar roles; and apps to organize software deployments. Each has a name to help you keep track of them.

Because it can be difficult to see if the instance you’re working on belongs to the right stack (for example, an integration or production stack) just by looking at the host name, OpsWorks provides a simple, user-defined attribute that you can use to color-code your stacks. For example, some customers use red for their production stacks. Others apply different colors to correspond to the regions in which the stacks are operating.

A stack color is simply a visual indicator to assist you while you’re working in the console. In those cases when you need to sign in to an instance (for auditing, for example, or to check log files or restart a process), it can be difficult to immediately detect when you have signed in to an instance on the wrong stack.

When you add a small, custom recipe to the setup lifecycle event, however, you can reuse the stack color for the shell prompt. Most modern terminal emulators support a 256-color mode. Changing the color of the prompt is simple.

The following code can be used to change the color of the shell prompt:

colors/recipes/default.rb

stack = search("aws_opsworks_stack").first
match = stack["color"].match(/rgb((d+), (d+), (d+))/)
r, g, b = match[1..3].map { |i| (5 * i.to_f / 255).round }

template "/etc/profile.d/opsworks-color-prompt.sh" do
  source "opsworks-color-prompt.sh.erb"
  variables(:color => 16 + b + g * 6 + 36 * r)
end

colors/templates/default/opsworks-color-prompt.sh.erb

if [ -n "$PS1" ]; then
  PS1="33[38;5;<%= @color %>m[u@h W]\$33[0m "
fi

You can use this with Chef 12, this custom cookbook, the latest Amazon Linux AMI, and Bash. You may have to adapt the cookbook for other operating systems and shells.

The stack color is not the only information you can include in the prompt. You can also add the stack and layer names of your instances to the prompt:

We invite you to try color-coding your stacks. If you have questions or other feedback, let us know in the comments.