在 Chef Recipe 中动态获取 Elasticache 端点?
Dynamically Getting Elasticache Endpoint in Chef Recipe?
我已经为 运行 我的 Web 应用程序创建了一个 CloudFormation 模板,其中包含一个 Elastic Loadbalancer、三个 EC2 实例 运行ning Tomcat 服务器和一个 Elasticache。启动时有一个 Chef 食谱 I 运行,除其他外,它配置每个 Tomcat 的 context.xml 文件以指向 Elasticache。
我想知道在配方中配置 Elasticache 的最佳方法是什么,所以我需要做的就是快速更改 CloudFormation 模板以调出其中的一组 (stack1 -elasticache、stack2-elasticache 等)
现在我有一个简单的硬编码变量替换(对于 Chef 来说并不理想,因为任何更改都会在每个 Chef'd box 上更新,除非每个都有单独的食谱),但我想知道是否有办法使用 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 的数量,它们的命名方案将对应于 Tomcats(stack1-TC1、stack2-TC2 等).我怎样才能做到这一点?
您可以查看 AWS ruby SDK gem,它可用于 Cookbook 库或提供程序以获取 运行 时间详细信息。下面是 link 和一些您可以利用的代码。
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
我已经为 运行 我的 Web 应用程序创建了一个 CloudFormation 模板,其中包含一个 Elastic Loadbalancer、三个 EC2 实例 运行ning Tomcat 服务器和一个 Elasticache。启动时有一个 Chef 食谱 I 运行,除其他外,它配置每个 Tomcat 的 context.xml 文件以指向 Elasticache。
我想知道在配方中配置 Elasticache 的最佳方法是什么,所以我需要做的就是快速更改 CloudFormation 模板以调出其中的一组 (stack1 -elasticache、stack2-elasticache 等)
现在我有一个简单的硬编码变量替换(对于 Chef 来说并不理想,因为任何更改都会在每个 Chef'd box 上更新,除非每个都有单独的食谱),但我想知道是否有办法使用 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 的数量,它们的命名方案将对应于 Tomcats(stack1-TC1、stack2-TC2 等).我怎样才能做到这一点?
您可以查看 AWS ruby SDK gem,它可用于 Cookbook 库或提供程序以获取 运行 时间详细信息。下面是 link 和一些您可以利用的代码。
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