python - 使用 boto3 删除 CloudFront 分配

标签 python amazon-cloudfront boto3

我正在编写一个拆卸脚本,需要删除网站的云前端发行版。看起来你必须遵循以下内容

  1. 禁用分发
  2. 等待分配被禁用(部署)
  3. 删除分发

但是,看起来服务员在第 2 步失败了,而且除非已禁用分发,否则 delete_distribution 也会失败。

result = client.update_distribution(
    DistributionConfig=disabledConf, 
    Id=dist_id, 
    IfMatch=matchid)
waiter = client.get_waiter('distribution_deployed')
print("Waiting for disabling the distribution")
waiter.wait(Id=dist_id)  # Throws here
client.delete_distribution(Id=dist_id, IfMatch=result['ETag'])

有人知道如何让它与 boto3 一起工作吗?

最佳答案

我刚刚遇到了这个问题。问题在于 Boto3 没有禁用分发的等待功能。来自 docs关于“distribution_deployed”等待函数:

Polls CloudFront.Client.get_distribution() every 60 seconds until a successful state is reached. An error is returned after 25 failed checks.

这意味着它不适用于禁用发行版。为了解决这个问题,我使用 datetime 和 time.sleep 函数来实现我自己的服务员。

import time
from datetime import datetime,timedelta
import sys

#disable distribution
result = client.update_distribution(
    DistributionConfig=disabledConf, 
    Id=dist_id, 
    IfMatch=matchid)

#wait for distribution to disable....
print("Waiting for disabling the distribution...This may take a while....")
timeout_mins=60 
wait_until = datetime.now() + timedelta(minutes=timeout_mins)
notFinished=True
eTag=""
while(notFinished):
    #check for timeout
    if wait_until < datetime.now():
        #timeout
        print("Distribution took too long to disable. Exiting")
        sys.exit(1)

    status=client.get_distribution(Id=dist_id)
    if(status['Distribution']['DistributionConfig']['Enabled']==False and status['Distribution']['Status']=='Deployed'):
        eTag=status['ETag']
        notFinished=False

    print("Not completed yet. Sleeping 60 seconds....")
    time.sleep(60) 

#delete distribution
client.delete_distribution(Id=dist_id, IfMatch=eTag)   

因此除了检查它是否被禁用之外,您还需要查看更改是否已生效。您可以通过确保“状态”已更改为“已部署”(更改期间将处于进行中)来执行此操作

关于python - 使用 boto3 删除 CloudFront 分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43077173/

相关文章:

amazon-web-services - 使用 AWSSDK 激活新的 CloudFront 自动压缩

python - AWS sagemaker 错误 - AttributeError : 'NoneType' object has no attribute 'startswith'

python - 如何使用 boto3 获取 VPC 的流日志 ID

python - 使用 python-igraph 编写带有节点标签的 Pajek 文件

ssl - Cloudfront 通过自己的 SSL 证书提供服务

python - 内存中 CSV 下载文件名

ios - 将 MP4 流式传输到 iOS 无法与 JWPlayer 和 CloudFront 配合使用

python-3.x - 当我尝试将数据帧输出到 csv 文件时,为什么我的输出只有一行?Python3/boto3

python - 针对集合的 CRUD 操作的单一职责原则

python - 使用 Pandas 将数据分组为单独的批处理