Как я могу указать aws групп безопасности старше 30 дней в python? - PullRequest
0 голосов
/ 27 мая 2020
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 для групп безопасности

1 Ответ

1 голос
/ 27 мая 2020

Нет понятия «Дата создания» или любая дата для групп безопасности.

Теоретически вы можете go через историю группы безопасности в AWS Config чтобы получить эту информацию.

Всегда просматривайте документацию, чтобы узнать, какие поля доступны.

...