【问题标题】:Dynamically Getting Elasticache Endpoint in Chef Recipe?在 Chef 食谱中动态获取 Elasticache 端点?
【发布时间】:2016-12-19 10:13:28
【问题描述】:

我创建了一个 CloudFormation 模板来运行我的 Web 应用程序,其中包含一个 Elastic Loadbalancer、三个运行 Tomcat 服务器的 EC2 实例和一个 Elasticache。我在启动时运行了一个 Chef 配方,其中包括配置每个 Tomcat 的 context.xml 文件以指向 Elasticache。

我想知道在配方中配置 Elasticache 的最佳方法是什么,所以我需要做的就是快速更改 CloudFormation 模板以调出一堆(stack1-elasticache、stack2 -elasticache 等。)

现在我简单地有一个硬编码的变量替换(对于厨师来说并不理想,因为任何更改都会在每个厨师的盒子上更新,除非每个都有单独的食谱),但我想知道是否有一种方法可以使用AWS CLI 和 Chef 的惰性节点评估执行以下操作:

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
        }
    }

让我感到困惑的是如何从亚马逊获得 Elasticache 名称。我计划做一些与我的示例非常相似的事情,增加每个新 Elasticache 的数量,并且它们的命名方案将与 Tomcat 相对应(stack1-TC1、stack2-TC2 等)。我怎样才能做到这一点?

【问题讨论】:

    标签: chef-infra amazon-cloudformation amazon-elasticache


    【解决方案1】:

    您可以查看 AWS ruby​​ SDK gem,它可以在 Cookbook 库或提供程序中用于获取运行时详细信息。下面是一个链接和一些代码,您可以利用它们。

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

    代码:-

    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
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2015-10-24
      相关资源
      最近更新 更多