![](/img/trans.png)
[英]Template error: every Fn::GetAtt object requires two non-empty parameters, the resource name and the resource attribute
[英]AWS cloudformation error: Template validation error: Template error: resource NotificationsTopic does not support attribute type Arn in Fn::GetAtt
我正在嘗試使用 yaml 模板創建 AWS cloudformation 堆棧。 目標是為某些通知創建一個 sns 主題。 我想輸出主題 arn,以便能夠通過僅指定主題 arn 來為該主題訂閱多個函數。
但是,當我嘗試從 aws 控制台創建堆棧時出現錯誤:
“模板驗證錯誤:模板錯誤:資源 NotificationsTopic 不支持 Fn::GetAtt 中的屬性類型 Arn”
我對 s3 存儲桶、dynamodb 表做了完全相同的操作,並且一切正常,但由於某種原因,使用 SNS 主題我無法獲得 ARN。
我想避免在訂閱的所有函數中對主題 arn 進行硬編碼。 因為如果有一天 ARN 主題發生變化,我需要更改所有函數,而不是我想在所有函數中導入主題 arn 並使用它。 這樣,如果將來出於任何原因我有一個新的 arn 主題,我將不得不修改任何內容。
這是模板:
Parameters:
stage:
Type: String
Default: dev
AllowedValues:
- dev
- int
- uat
- prod
Resources:
NotificationsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: !Sub 'notifications-${stage}'
Subscription:
- SNS Subscription
TopicName: !Sub 'notifications-${stage}'
Outputs:
NotificationsTopicArn:
Description: The notifications topic Arn.
Value: !GetAtt NotificationsTopic.Arn
Export:
Name: !Sub '${AWS::StackName}-NotificationsTopicArn'
NotificationsTopicName:
Description: Notifications topic name.
Value: !Sub 'notifications-${stage}'
Export:
Name: !Sub '${AWS::StackName}-NotificationsTopicName'
並非所有資源都相同。 始終檢查特定資源的文檔。 它有“返回值”部分,您可以輕松驗證 SNS 主題是否將 ARN 作為Ref
值,因此您不必使用GetAtt
函數
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sns-topic.html
編輯:感謝您指出並非每個資源都提供其 ARN 的評論。 一個值得注意的例子是 Autoscaling 組。 當然,我的回答中的關鍵是“檢查每個資源的文檔”,這是一個示例,並非每個資源都具有每個屬性。 話雖如此,ASG 輸出缺少 ARN 是一件非常奇怪的事情。 它也不能輕松構建,因為 ARN 還包含 GroupId,它是一個隨機散列。 至少對於 ECS 容量提供商https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/548和https://github.com的用例,可能有一些努力來解決這個問題/aws/containers-roadmap/issues/631#issuecomment-648377011但我認為這是一個足夠重要的問題,應該在這里提及。
對於不直接返回 ARN 的資源,我找到了一種解決方法,其中包括自己構建 ARN。
例如,要獲取我的代碼管道的 ARN:
!Join [ ':', [ "arn:aws:codepipeline", !Ref AWS::Region, !Ref AWS::AccountId, !Ref StackDeletePipeline ] ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.