0

I've created a CloudFormation template to run my web application that contains an Elastic Loadbalancer, three EC2 instances running Tomcat servers, and an Elasticache. There is a Chef recipe I run on boot that, among other things, configures each Tomcat's context.xml file to point to the Elasticache.

I'm wondering what the best way is to configure the Elasticache in the recipe so all I need to do is make a quick change in the CloudFormation template to bring up a bunch of them (stack1-elasticache, stack2-elasticache, etc.)

Right now I simple have a hard-coded variable replacement (not ideal for Chef since any change will update on every Chef'd box unless there is a separate recipe for each), but I wonder if there is a way to use the AWS CLI and Chef's lazy node evaluation to do something like:

ruby_block 'Get Elasticache endpoint' do
block do
    node.run_state['elasticache'] = `some aws command to get the Elasticache endpoint I want`
end
end

source "context.xml.erb"
variables(lazy{
        {
            :tomcat_version => elasticache
        }
    }

What's tripping me up is how to get the Elasticache name from Amazon. I plan to do something very similar to my example, incrementing the number of each new Elasticache, and their naming scheme will correspond with the Tomcats (stack1-TC1, stack2-TC2, etc.). How can I achieve this?

Rome_Leader
  • 2,518
  • 9
  • 42
  • 73

1 Answers1

1

You can check AWS ruby SDK gem, which can be used in either Cookbook library or providers to fetch run time details. Below a link and some code, which you can leverage.

http://docs.aws.amazon.com/sdkforruby/api/Aws/ElastiCache/Client.html#describe_cache_clusters-instance_method

Code:-

require 'aws-sdk'

def create_aws_interface(aws_interface)        
      if !new_resource.aws_access_key.to_s.empty? && !new_resource.aws_secret_access_key.to_s.empty?
           creds = ::Aws::Credentials.new(new_resource.aws_access_key, new_resource.aws_secret_access_key)
      else
           Chef::Log.info('use iam profile')
           creds = ::Aws::InstanceProfileCredentials.new
      end
      Aws.config[:ssl_verify_peer] = false
      aws_interface.new(credentials: creds, region: new_resource.region)
end

def ec
      ec ||= create_aws_interface(::Aws::ElastiCache::Client)
end

# It will return all Elastic cache clusters in object, which needs to be parsed further.
def getClusterID()
    resp = ec.describe_cache_clusters({
       marker: "String",
       show_cache_node_info: true,
    })
end