【问题标题】:AWS SSM Python / Boto3 Create Hybrid Activation ExpirationDate Type ErrorAWS SSM Python / Boto3 创建混合激活 ExpirationDate 类型错误
【发布时间】:2020-07-27 21:03:11
【问题描述】:

我正在尝试为多个部门创建 AWS SSM 混合激活。我的 IDE 告诉我 datetime 不可调用,我收到的错误消息是:

Traceback (most recent call last):
  File "C:\Users\username\bin\Create-Activation.py", line 23, in <module>
    ExpirationDate = datetime(y, m, d),
TypeError: 'module' object is not callable

变量 y、m 和 d 被传递给 ExpirationDate 对象。我使用 Amazon 和 Boto3 文档作为指南。

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ssm.html#SSM.Client.create_activation

https://docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/SSM/Client.html#create_activation-instance_method

谁能帮我理解 ExpirationDate 对象失败的原因?

import boto3
import datetime

client = boto3.client('ssm')
current_date = datetime.date.today()
creation_date = current_date.strftime('%Y%m%d')
end_date = current_date + datetime.timedelta(days=30)
y = end_date.year
m = end_date.month
d = end_date.day
divisions = ['div1', 'div2', 'div3']

#def lambda_handler(event, context):

for x in divisions:
    client.create_activation(
        Description = (x + '-' + 'creation_date'),
        #DefaultInstanceName = 'string',
        IamRole = 'MySSMServiceRole',
        RegistrationLimit = 200,
        ExpirationDate = datetime(y, m, d),
        Tags=[
            {
                'Key': 'Division',
                'Value': x
            },
        ]
    )
    print('\n ' + x + '-creation_date was create.')

【问题讨论】:

  • 感谢您的更新。这就是我使用from datetime import datetimeTraceback (most recent call last): File "C:\Users\jmoorhead\bin\Create-Activation.py", line 7, in &lt;module&gt; current_date = datetime.date.today() AttributeError: 'method_descriptor' object has no attribute 'today'后得到的结果
  • 使用 current_date = datetime.today() 。正如我提到的,使用 dir() 来检查对象的属性。
  • 谢谢萨兰吉特。我最终使用了from datetime import datetime, timedelta 并修改了我的几个变量。 print(dir(datetime)) 在这样做时非常有用。
  • 乐于帮助@jmoorhead :)

标签: python-3.x amazon-web-services boto3 amazon-systems-manager


【解决方案1】:

使用from datetime import datetime 代替import datetime。 日期时间模块本身不可调用,但你可以调用它的属性datetime

【讨论】:

  • 你可以使用 dir() 函数列出 datetime 模块的所有属性。 print(dir(datetime))
猜你喜欢
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 2022-01-23
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
相关资源
最近更新 更多