阅读背景:

标签上的aws-sdk javascript过滤器:key = value

来源:互联网 

Folks, I am having difficult time understanding the docs https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html

伙计们,我很难理解文档https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html

I need to grab all the running instances with the following tags assigned to them:

我需要使用分配给它们的以下标记来获取所有正在运行的实例:

project=foo environment=production

project = foo environment = production

The following does not seem to work.

以下似乎不起作用。

var params = {
    DryRun: false,
    Filters: [
        {
            Name: 'instance-state-name',
            Values: [
                'running'
            ],
        },
        {
            Name: 'tag:key=value',
            Values: [
                'foo',
                'production'
            ],
        },
    ]
};

ec2.describeInstances(params, function (err, data) {
...

2 个解决方案

#1


23  

If your tag's key is 'foo' and its value is 'production', you should change your code to the following. The Name is in the 'tag:key' format, and the Values are the values are the ones you are looking for that correspond to that key.

如果您的标记键是“foo”且其值为“production”,则应将代码更改为以下内容。名称采用'tag:key'格式,值是您要查找的与该键对应的值。

{
    Name: 'tag:foo',
    Values: [
            'production'
    ],
},

#2


0  

This is slightly related, and may help anyone who found there way here like I did.

这有点关联,可能会帮助那些像我一样在这里找到方式的人。

I was trying check if an Instance with a tag Name=myInstance, was running in my aws account.

我正在尝试检查具有标签Name = myInstance的实例是否在我的aws帐户中运行。

It's pretty difficult to check existence of resources in aws, but clairestreb's answer led me to the work around for this case.

检查aws中资源的存在是非常困难的,但是clairestreb的回答让我在这个案例中找到了解决方法。

I've begun developing a library of other such work arounds, and with it you can now do this:

我已经开始开发其他类似工作的库了,现在你可以这样做:

Find Running Instance with tag Name=myInstance

var ex = require('aws-existence')
var ec2 = ex.ec2({region: 'us-west-2'})

ec2.doesInstanceExist('myInstance', 'running')
  .then(doSomething)

function doSomething (exists) {
  if (exists) {
    // stop it?
  } else {
    // start it?
  }
}

So that's doesInstanceExist(identifier, instanceState) where the identifier is a name tag, or an InstanceId. And state is optionally any one of the instance-state-name values. If you omit the state, it will tell you if an instance in any of the states exists.

所以这是标识符是名称标签或InstanceId的doesInstanceExist(identifier,instanceState)。并且state可选地是instance-state-name值中的任何一个。如果省略状态,它将告诉您是否存在任何状态的实例。

You can find the library here:

你可以在这里找到图书馆:

nackjicholson/aws-existence


分享到: