簡體   English   中英

在 AWS Cloudformation 上調用 ListStacks 時出現驗證錯誤

[英]ValidationError on calling ListStacks on AWS Cloudformation

我有一段 python 代碼,它在 AWS 的 Cloudformation API 上調用ListStacks操作:

client = boto3.client('cloudformation', region_name='[MYREGION]')

status_filter = ['CREATE_COMPLETE', 'UPDATE_COMPLETE', 'ROLLBACK_COMPLETE']  

response = client.list_stacks(StackStatusFilter=status_filter)

next_token = response.get('NextToken', None)

while next_token:
    response = client.list_stacks(NextToken=next_token)

在最后一行,它拋出一個 ValidationError:

調用 ListStacks 操作時發生錯誤 (ValidationError):[TOKEN] 不是有效的 NextToken

這已經工作了一個月,但現在突然拋出這個錯誤。 代碼沒有任何變化。

奇怪的是,響應小於 1MB,因此根據文檔,令牌應該為空:

下一個令牌
如果輸出的大小超過 1 MB,則為標識下一頁堆棧的字符串。
如果不存在其他頁面,則此值為空。
類型:字符串
長度限制:最小長度為 1。最大長度為 1024

有沒有人知道為什么響應包含(無效的)NextToken 而不是 null?

編輯

我認為響應沒有超過限制的假設是錯誤的。 它只返回適合過濾器的堆棧子集。

所以問題是:為什么 NextToken 無效?

我不知道為什么 NextToken 無效,但一位同事指出我們可以通過使用Cloudformation.Paginator.ListStacks類來規避這個問題。

client = boto3.client('cloudformation', region_name='eu-central-1')
paginator = client.get_paginator('list_stacks')
status_filter = ['CREATE_COMPLETE', 'UPDATE_COMPLETE', 'ROLLBACK_COMPLETE']

response_iterator = paginator.paginate(
  StackStatusFilter=status_filter
)
stacks = []
for response in response_iterator:
  stacks += [stack['StackName'] for stack in response['StackSummaries']]

return stacks

我在這段代碼中看到了同樣的錯誤:

            for region in get_regions(aws):
                instances = cf_client.list_stack_instances(StackSetName=data[i]['StackSetId'],region_name=region['RegionName'])
                stack_instances.append(instances)
                while 'NextToken' in instances:
                    more_instances = cf_client.list_stack_instances(
                        StackSetName=data[i]['StackSetName'],
                        NextToken=instances['NextToken'],
                        MaxResults=99,
                        StackInstanceRegion=region['RegionName']
                    )

在嘗試了分頁器和其他一些想法之后,我在通過刪除 StackInstanceRegion 和 MaxResults 來逐步執行我的代碼時簡化了函數參數,例如

while 'NextToken' in instances:
                    more_instances = cf_client.list_stack_instances(
                        StackSetName=data[i]['StackSetName'],
                        NextToken=instances['NextToken']
                    )

現在它正在使用 client.list_stack_instances()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM