尝试通过python中的AWS CDK创建预算时出现jsii错误

时间:2020-10-19 01:59:32

标签: python aws-cdk

我正在尝试通过AWS CDK创建预算。

这里是代码,这是通过参考官方文档和执行类似操作的TypeScript解决方案拼凑而成的。

email = 'yo@yo.yo'
value = 90.0
subscription = sns_sub.EmailSubscription(email)
topic = sns.Topic(self, id='notification_topic', display_name='budget notifications',
                          topic_name='budget_notifications')
topic.add_subscription(subscription)
budget_data_props = aws_budgets.CfnBudget.BudgetDataProperty(budget_type='COST', time_unit='MONTHLY', 
                                                             budget_name='CDK budget',
                                                             budget_limit=aws_budgets.CfnBudget.SpendProperty(amount=value, unit='USD'))
np = aws_budgets.CfnBudget.NotificationProperty(comparison_operator='GREATER_THAN',
                                                notification_type='FORECASTED', threshold=90.0,
                                                threshold_type='PERCENTAGE')
sub = aws_budgets.CfnBudget.SubscriberProperty(address=topic.topic_arn, subscription_type='SNS')
nws = aws_budgets.CfnBudget.NotificationWithSubscribersProperty(notification=np, subscribers=[sub])

budget_props = aws_budgets.CfnBudgetProps(budget=budget_data_props, notifications_with_subscribers=nws)
budget = aws_budgets.CfnBudget(self, 'cdk budget', budget=budget_props)

我得到的错误是:

错误:

值与联合中的任何类型都不匹配:Wire struct type '@ aws-cdk / aws-budgets.CfnBudgetProps'与预期不符 '@ aws-cdk / aws-budgets.CfnBudget.BudgetDataProperty',预期对象 参考,得到 {“ $ jsii.struct”:{“ fqn”:“ @ aws-cdk / aws-budgets.CfnBudgetProps”,“ data”:{“ budget”:{“ $ jsii.struct”:{“ fqn”:“ @ aws-cdk / aws-budgets.CfnBudget.BudgetDataProperty“,”数据“:{” budgetType“:” COST“,” timeUnit“:” MONTHLY“,” budgetLimit“:{” $ jsii.struct“:{” fqn“ :“ @ aws-cdk / aws-budgets.CfnBudget.SpendProperty”,“数据”:{“金额”:10,“单位”:“ USD”}}},“ budgetName”:“ CDK budget“,” costFilters“:null,” costTypes“:null,” plannedBudgetLimits“:null,” timePeriod“:null}}},” notificationsWithSubscribers“:{” $ jsii.struct“:{” fqn“:” @ aws -cdk / aws-budgets.CfnBudget.NotificationWithSubscribersProperty“,” data“:{” notification“:{” $ jsii.struct“:{” fqn“:” @ aws-cdk / aws-budgets.CfnBudget.NotificationProperty“,” data“:{” comparisonOperator“:” GREATER_THAN“,” notificationType“:” FORECASTED“,” threshold“:90,” thresholdType“:” PERCENTAGE“}}},” subscribers“:[{” $ jsii.struct“: {“ fqn”:“ @ aws-cdk / aws-budgets.CfnBudget.SubscriberProperty”,“ data”:{“ address”:“ $ {Token [TOKEN.56]}”,“ subscriptionType”:“ SNS”}} }]}}}}}

我正在使用带有类型提示的IDE,所以我对于看起来似乎类型不匹配的情况不知所措。

感谢您的阅读和见识。

1 个答案:

答案 0 :(得分:0)

使用python并创建一个Construct时,有两个传递prop的选项。直接使用Props对象或将Props对象的参数用作键/值对。您正在混合这两种方法。由于您正在创建道具对象,因此应该直接传递

budget = aws_budgets.CfnBudget(self, 'cdk budget', budget_props)

您也可以用这种方法代替创建Props对象

budget = aws_budgets.CfnBudget(
    self, 'cdk budget', 
    budget=budget_data_props, 
    notifications_with_subscribers=nws
)