使用boto3在单个Lambda脚本中更新多个资源,这些资源具有不同的值。

huangapple go评论68阅读模式
英文:

Using boto3 to update multiple resources with different values in single lambda script

问题

我想迭代我的账户中的所有资源,并根据一个包含需要更新的一些值的Excel文件来更新它们。我当前的代码存在的问题是它只会更新返回的最后一个值,而不是其他值。我该如何让它更新所有资源?

import boto3

s3_client = boto3.client('s3')
client = boto3.client('resourcegroupstaggingapi')

def get_resource_tag(resource_name):
    # 这是一个示例,将从这里返回唯一的值。
    return ['uniqueexample', 'value']

def update_resources_dict(resource):
    resource_name = resource["ResourceARN"]
    tag_to_update = get_resource_tag(resource_name)
    resource_list = [resource_name]
    return client.tag_resources(ResourceARNList=resource_list, Tags={tag_to_update[0]: tag_to_update[1]})

def lambda_handler(event, context):
    all_resources = client.get_resources(ResourcesPerPage=100)
    for resource in all_resources["ResourceTagMappingList"]:
        update_resources_dict(resource)

这个函数不会更新我在client.tag_resources上使用的每个资源。我该如何修复它?

英文:

I want to iterate over all the resources in my account and update them based on an excel file which has some values that need to be updated. The problem I'm running into with my current code is it will only update the final value that is returned and not the others. How do i get it to update all resources?

import boto3

s3_client = boto3.client('s3')
client = boto3.client('resourcegroupstaggingapi')

def get_resource_tag(resource_name):
#this is an example, will have unique values returned from here. 
     return ['uniqueexample','value']

def update_resources_dict(resource):
    resource_name = resource["ResourceARN"]
    tag_to_update = get_resource_tag(resource_name)
    resource_list = [resource_name]
    return client.tag_resources(ResourceARNList =resource_list , Tags = {tag_to_update[0]:tag_to_update[1] })

def lambda_handler(event, context):
    all_resources=client.get_resources(ResourcesPerPage = 100)
    for resource in all_resources["ResourceTagMappingList"]:
        update_resources_dict(resource)

This function does not update each of the reosurces I use client.tag_resources on. How would i fix that?

答案1

得分: 0

需要将其附加到某个东西并返回。以下代码解决了这个问题:

import boto3

s3_client = boto3.client('s3')
client = boto3.client('resourcegroupstaggingapi')

def get_resource_tag(resource_name):
    return ['uniqueexample', 'value']

def update_resources_dict(resource):
    resource_name = resource["ResourceARN"]
    tag_to_update = get_resource_tag(resource_name)
    resource_list = [resource_name]
    return client.tag_resources(ResourceARNList=resource_list, Tags={tag_to_update[0]: tag_to_update[1]})

def lambda_handler(event, context):
    all_resources = client.get_resources(ResourcesPerPage=100)
    rd = {}
    for resource in all_resources["ResourceTagMappingList"]:
        print(resource)
        rd[resource['ResourceARN']] = update_resources_dict(resource)
    return rd
英文:

Needed to append it to a something and return that. The following code solved it:

import boto3

s3_client = boto3.client('s3')
client = boto3.client('resourcegroupstaggingapi')

def get_resource_tag(resource_name):
     return ['uniqueexample','value']

def update_resources_dict(resource):
    resource_name = resource["ResourceARN"]
    tag_to_update = get_resource_tag(resource_name)
    resource_list = [resource_name]
    return client.tag_resources(ResourceARNList =resource_list , Tags = {tag_to_update[0]:tag_to_update[1] })

def lambda_handler(event, context):
    all_resources=client.get_resources(ResourcesPerPage = 100)
    rd = {}
    for resource in all_resources["ResourceTagMappingList"]:
        print(resource)
        rd[resource['ResourceARN']] = update_resources_dict(resource)
    return rd
    
       ```

</details>



huangapple
  • 本文由 发表于 2023年7月3日 21:54:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定