python-3.x - 我如何在 python 中列出 30 天以上的 aws 安全组?
问题描述
import boto3
from datetime import datetime
ec2res = boto3.resource('ec2')
ec2cli = boto3.client('ec2')
now = datetime.now()
def calculator(launch_date, object):
age = now - launch_date
intage = int(age.days)
if intage <= 30:
print(object)
list_security_groups = ec2cli.describe_security_groups()
for security_group in list_security_groups['SecurityGroups']:
launch_date = datetime.strptime(security_group['CreationDate'].strftime("%Y-%m-%d %H:%M:%S.%f"), "%Y-%m-%d %H:%M:%S.%f")
intage = calculator(launch_date,security_group)
或者
list_security_groups = ec2res.security_groups.all()
for security_group in list_security_groups:
launch_date = datetime.strptime(security_group.launchtime.strftime("%Y-%m-%d %H:%M:%S.%f"), "%Y-%m-%d %H:%M:%S.%f")
intage = calculator(launch_date,security_group)
这些格式适用于其他 aws 对象,但我得到了安全组的 KeyError
解决方案
安全组没有“创建日期”的概念,也没有任何日期。
理论上,您可以通过 AWS Config 中的安全组历史记录来获取此信息。
始终查看文档以查看可用的字段。
推荐阅读
- javascript - 在异步调用之外访问数据?
- asp.net-mvc - 转换日期时间格式 ASP.net MVC @Html.DisplayFor
- background-color - Visual Studio 2019 颜色主题编辑器 - 如何使解决方案资源管理器的背景颜色透明?
- c# - 从一个方法获取变量值到另一个?
- python - 仅对第三维求和一个 3d numpy 数组
- python - Python csv文件将字符串转换为浮点数
- yocto - 如何构建配方但不将其包含在操作系统映像中
- c++ - 避免在大型 DFS 中重新访问节点的方法?
- css - 使元素在没有 JS 的情况下占用 100% 的可见空白空间
- c# - 在 .NET Core 中,HttpContext.SignOutAsync() 和 ControllerBase.SignOut() 有什么区别?