Setup Test Kitchen for AWS EC2 Support

From Bonus Bits
Jump to: navigation, search

Purpose

This article gives the steps to setup Test Kitchen to have the capability to spin up AWS EC2 instances without having to put static entries or credentials in your Kitchen configuration file/s.


Prerequisites


Install EC2 Ruby Gem

  1. Install kitchen-ec2 Ruby Gem
    1. ChefDK without Pathing Changed to ChefDK
      chef exec gem install kitchen-ec2 --no-rdoc --no-ri
      
    2. Ruby Environment
      gem install kitchen-ec2 --no-rdoc --no-ri
      

Setup Environment Variables

I use AWS profiles in my Kitchen configurations so the only two environment variables required for are the SSH Key ID and SSH Key Path

BASH Environment Variables

Add your ssh key id and path to your .bash_profile

export AWS_SSH_KEY_ID=aws-keypair-name
export AWS_SSH_KEY_PATH=$HOME/.ssh/aws-keypair-name.pem

Optionally you can add a little BASH scripting to your .bash_profile to allow you to quickly switch your environment variables between different access keys. I have several different AWS keys. So, I wrote some simple BASH functions to allow me to switch between them from the CLI.

# Kitchen-EC2 - Defaults
export AWS_SSH_KEY_ID=aws_sandbox
export AWS_SSH_KEY_PATH=$HOME/.ssh/aws_sandbox.pem
export AWS_PROFILE=aws_sandbox
export AWS_REGION=us-west-2
export AWS_VPC_ID=vpc-00000000
export AWS_SUBNET=subnet-00000000
export AWS_SECURITY_GROUP_1=sg-00000000
export AWS_SECURITY_GROUP_2=sg-00000000
export AWS_IAM_ROLE=default_ec2instance_role

function aws-showkey() {
	echo ''
	echo "Configured AWS Environment Variables"
	echo "--------------------------------------"
	echo "AWS_SSH_KEY_ID = $AWS_SSH_KEY_ID"
	echo "AWS_SSH_KEY_PATH = $AWS_SSH_KEY_PATH"
	echo "AWS_PROFILE = $AWS_PROFILE"
	echo "AWS_REGION = $AWS_REGION"
	echo "AWS_VPC_ID = $AWS_VPC_ID"
	echo "AWS_SUBNET = $AWS_SUBNET"
	echo "AWS_SECURITY_GROUP_1 = $AWS_SECURITY_GROUP_1"
	echo "AWS_SECURITY_GROUP_2 = $AWS_SECURITY_GROUP_2"
	echo "AWS_IAM_ROLE = $AWS_IAM_ROLE"
	echo ''
}

function aws-setkey(){
  # TODO: Move Logic to Ruby Script and call with this function?
  # List Keys if Null or Wrong
  function list-awskeys(){
    echo ''
    echo "Enter an Available Key"
    echo "----------------------"
    echo "1. dev"
    echo "2. prd"
    echo "3. sandbox"
  }
  # Skip if Argument is Null
  if [ -n "$1" ]
    then
      # Set Env Variable
      if [ $1 == "dev" ]
        then
          export AWS_SSH_KEY_ID=aws_dev
          export AWS_SSH_KEY_PATH=$HOME/.ssh/aws_dev.pem
          export AWS_PROFILE=aws_dev
          export AWS_REGION=us-west-2
          export AWS_VPC_ID=vpc-00000000
          export AWS_SECURITY_GROUP_1=sg-00000000
          export AWS_SECURITY_GROUP_2=sg-00000000
          export AWS_IAM_ROLE=default_ec2instance_role
          if [ -n "$2" -a $2 == "public" ]
            then
              export AWS_SUBNET=subnet-00000000
          else
              export AWS_SUBNET=subnet-10000000
          fi
      elif [ $1 == "prd" ]
        then
          export AWS_SSH_KEY_ID=aws_prd
          export AWS_SSH_KEY_PATH=$HOME/.ssh/aws_prd.pem
          export AWS_PROFILE=aws_prd
          export AWS_REGION=us-west-2
          export AWS_VPC_ID=vpc-11111111
          export AWS_SUBNET=subnet-11111111
          export AWS_SECURITY_GROUP_1=sg-11111111
          export AWS_SECURITY_GROUP_2=sg-11111111
          export AWS_IAM_ROLE=default_ec2instance_role
          if [ -n "$2" -a $2 == "public" ]
            then
              export AWS_SUBNET=subnet-11111111
          else
              export AWS_SUBNET=subnet-20000000
          fi
      elif [ $1 == "sandbox" ]
        then
          export AWS_SSH_KEY_ID=aws_sandbox
          export AWS_SSH_KEY_PATH=$HOME/.ssh/aws_sandbox.pem
          export AWS_PROFILE=aws_sandbox
          export AWS_REGION=us-west-2
          export AWS_VPC_ID=vpc-22222222
          export AWS_SUBNET=subnet-22222222
          export AWS_SECURITY_GROUP_1=sg-22222222
          export AWS_SECURITY_GROUP_2=sg-22222222
          export AWS_IAM_INSTANCE_PROFILE=sandbox_instance_profile
      else
        list-awskeys
      fi
  else
    list-awskeys
  fi
  # Display Env Variable Current Setting
  aws-showkey
}

PowerShell

[Environment]::SetEnvironmentVariable("AWS_SSH_KEY_ID", "aws_sandbox", "Machine")
[Environment]::SetEnvironmentVariable("AWS_SSH_KEY_PATH", "$ENV:USERPROFILE\.ssh\aws-sandbox.pem", "Machine")
[Environment]::SetEnvironmentVariable("AWS_SSH_KEY_ID", "aws_sandbox", "Machine")
[Environment]::SetEnvironmentVariable("AWS_SSH_KEY_ID", "aws_sandbox", "Machine")
[Environment]::SetEnvironmentVariable("AWS_SSH_KEY_ID", "aws_sandbox", "Machine")
[Environment]::SetEnvironmentVariable("AWS_SSH_KEY_ID", "aws_sandbox", "Machine")
[Environment]::SetEnvironmentVariable("AWS_SSH_KEY_ID", "aws_sandbox", "Machine")


Kitchen Configuration

driver:
  name: ec2
  associate_public_ip: false
  shared_credentials_profile: <%= ENV['AWS_PROFILE'] %>
  aws_ssh_key_id: <%= ENV['AWS_SSH_KEY_ID'] %>
  security_group_ids:
    - <%= ENV['AWS_SECURITY_GROUP_1'] %>
    - <%= ENV['AWS_SECURITY_GROUP_2'] %>
  subnet_id: <%= ENV['AWS_SUBNET'] %>
  vpc_id: <%= ENV['AWS_VPC_ID'] %>
  iam_profile_name: <%= ENV['AWS_IAM_INSTANCE_PROFILE'] %>


Related Articles