使用Python3?Boto3包刪除AWS?CloudFormation的棧(Stacks)
小結
本文記錄了使用Python3的Boto3包刪除AWS CloudFormation的棧(Stacks)
問題及解決
有關Json文件的輸入和輸出
json.loads
函數是將一個字符串(String)輸入轉換為字典類型(dictionary)輸出json.dumps
函數是將一個字典類型(dictionary)輸入轉換為字符串(String)輸出
當出現JSON object must be str, bytes or bytearray, not dict
或者'dict' object has no attribute 'read'
的錯誤時,需要檢查以上輸入類型是否正確。
對于datetime.datetime not JSON serializable
的問題,也就是日期類型無法進行Json序列化,可以使用以下指令解決問題,應該是default=str
這個參數起了作用,將日期類型處理為了字符串:
json_formatted_str = json.dumps(task_definition, indent=2, sort_keys=True, default=str)
使用Python3及正則表達式查找字符串包含某個子字符串
使用以下辦法:
exp = re.compile(stack_name_to_Search) stack_name = cfn_stack['StackName'] if re.search(exp, stack_name): ....
以上是查找 stack_name
這個字符串是否包含stack_name_to_Search
這個子字符串。
使用Python3 Boto3刪除AWS CloudFormation的棧(Stacks)
def delete_skms_stack(stack_name): cf_client = boto3.client('cloudformation') #list all the stacks excepts 'DELETED_STACKS' response = cf_client.list_stacks( StackStatusFilter=[ 'CREATE_IN_PROGRESS','CREATE_FAILED','CREATE_COMPLETE', 'ROLLBACK_IN_PROGRESS','ROLLBACK_FAILED','ROLLBACK_COMPLETE', 'DELETE_IN_PROGRESS','DELETE_FAILED', 'UPDATE_IN_PROGRESS','UPDATE_COMPLETE_CLEANUP_IN_PROGRESS','UPDATE_COMPLETE','UPDATE_FAILED','UPDATE_ROLLBACK_IN_PROGRESS','UPDATE_ROLLBACK_FAILED','UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS','UPDATE_ROLLBACK_COMPLETE','REVIEW_IN_PROGRESS', 'IMPORT_IN_PROGRESS','IMPORT_COMPLETE','IMPORT_ROLLBACK_IN_PROGRESS','IMPORT_ROLLBACK_FAILED','IMPORT_ROLLBACK_COMPLETE' ] ) #Stack name pattern exp = re.compile(stack_name) print('--------------------') print(response['StackSummaries']) for cfn_stack in response['StackSummaries']: stack_name = cfn_stack['StackName'] #match = re.search(regex_pattern,stack_name) if re.search(exp, stack_name): #Custome conditions can be implemented here try: response = cf_client.delete_stack(StackName=stack_name) stack_delete_status = cf_client.describe_stacks(StackName=stack_name) logger.info("Delete stack: " + json.dumps(response)) while stack_delete_status['Stacks'][0]['StackStatus'] == 'DELETE_IN_PROGRESS': time.sleep(10) stack_delete_status = cf_client.describe_stacks(StackName=stack_name) logger.info("Delete stack status: " + stack_delete_status['Stacks'][0]['StackStatus']) if stack_delete_status['Stacks'][0]['StackStatus'] == 'DELETE_FAILED': logger.warning('Delete failed. Retry delete') resources = cf_client.delete_stack(StackName=stack_name) return resources elif stack_delete_status['Stacks'][0]['StackStatus'] == 'DELETE_IN_PROGRESS': continue else: logger.info("Delete stack complete") except Exception as e: logger.error(e)
以上代碼執(zhí)行效果如下:
INFO:__main__:Delete stack status: DELETE_IN_PROGRESS
INFO:__main__:Delete stack status: DELETE_IN_PROGRESS
INFO:__main__:Delete stack status: DELETE_IN_PROGRESS
INFO:__main__:Delete stack status: DELETE_IN_PROGRESS
ERROR:__main__:An error occurred (ValidationError) when calling the DescribeStacks operation: Stack with id ECS-Console-V2-Service-sammperso-johnvpc-svc-John-VPC-Cluster-8c3e39c8 does not exist
其中最后一個錯誤是因為CloudFormation的棧(Stacks)已經被刪除,找不到了,所以是正常返回。
參考
Digital Ocean: Python Pretty Print JSON
Stackoverflow: JSON object must be str, bytes or bytearray, not dict
Stackoverflow: ‘dict’ object has no attribute ‘read’
Stackoverflow: How can I overcome “datetime.datetime not JSON serializable”?
Stackoverflow: How to delete multiple Cloudformation stacks at once?
Stackoverflow: Python regex check if string contains any of words
到此這篇關于如何使用Python3 Boto3刪除AWS CloudFormation的棧(Stacks)的文章就介紹到這了,更多相關Python3 Boto3刪除AWS CloudFormation的棧內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Tensorflow 如何從checkpoint文件中加載變量名和變量值
這篇文章主要介紹了Tensorflow 如何從checkpoint文件中加載變量名和變量值的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05