如何获得RDS标签?

时间:2018-12-04 03:51:07

标签: node.js amazon-web-services amazon-rds

我正在尝试为rds实例获取标签。 我使用node js,它可以异步运行,因此无法正确获取结果。 请回复报价

添加: 一个rds实例可以具有多个标签。 我只想从rds实例的标签中导入某些标签。 但是,实例和代码不匹配,因为它可以异步工作。

var AWS = require('aws-sdk');
var request = require("request");
AWS.config.update({region:'ap-northeast-1'});
var credentials = new AWS.SharedIniFileCredentials({profile: 'test'});
AWS.config.credentials = credentials;
var rds = new AWS.RDS();

rds.describeDBInstances(function(err, data) {
    for(var i=0; i<data.DBInstances.length; i++) {
        var instance = data.DBInstances[i];
        var clusterName = instance.DBClusterIdentifier;
        var instanceName = instance.DBInstanceIdentifier;
        var resourceName = 'arn:aws:rds:ap-northeast-1:231497262134:db:' + instanceName;
        var params = {
            ResourceName: resourceName,
        };

        rds.listTagsForResource(params, function(err, data) {
            var serviceTag = ''
            data.TagList.forEach((tag) => {
                if (tag.Key.toLowerCase() === 'service' && tag.Value.length > 0) {
                    serviceTag = tag.Value;
                }        
            })
            console.log(instanceName, serviceTag)
        })
    }
})

1 个答案:

答案 0 :(得分:2)

我已经用async和await语法修复了它。 感谢您的评论。

var AWS = require('aws-sdk');
var request = require("request");
AWS.config.update({region:'ap-northeast-1'});
var credentials = new AWS.SharedIniFileCredentials({profile: 'test'});
AWS.config.credentials = credentials;
var rds = new AWS.RDS();


rds.describeDBInstances(async (err, data) => {
    for(var i=0; i<data.DBInstances.length; i++) {
        var instance = data.DBInstances[i];
        var clusterName = instance.DBClusterIdentifier;
        var instanceName = instance.DBInstanceIdentifier;
        var resourceName = 'arn:aws:rds:ap-northeast-1:231497262134:db:' + instanceName;


        let params = {
            ResourceName: resourceName,
        };

        let tags = await rds.listTagsForResource(params).promise();
        var serviceTag = ''
        tags.TagList.forEach((tag) => {
            if (tag.Key.toLowerCase() === 'service' && tag.Value.length > 0) {
                serviceTag = tag.Value;
            }        
        })


        console.log(clusterName + ',' + instanceName + ',' + serviceTag)
    }
})