英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论