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

jsii error when attempting to create a budget via AWS CDK in python

我正在尝试通过 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)

我得到的错误是:

错误:

Value did not match any type in union: Wire struct type '@aws-cdk/aws-budgets.CfnBudgetProps' does not match expected '@aws-cdk/aws-budgets.CfnBudget.BudgetDataProperty', Expected object reference, got {"$jsii.struct":{"fqn":"@aws-cdk/aws-budgets.CfnBudgetProps","data":{"budget":{"$jsii.struct":{"fqn":"@aws-cdk/aws-budgets.CfnBudget.BudgetDataProperty","data":{"budgetType":"COST","timeUnit":"MONTHLY","budgetLimit":{"$jsii.struct":{"fqn":"@aws-cdk/aws-budgets.CfnBudget.SpendProperty","data":{"amount":10,"unit":"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,所以我不知道这是如何出现类型不匹配的。

感谢您的阅读和见解。

当使用 python 并创建构造时,您有两种传递道具的选择。使用 Props 对象或直接使用 Props 对象的参数作为 key/value 对。您正在混合使用这两种方法。由于您正在创建 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
)