本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用虛擬參數取得 AWS 值
虛擬參數是內建變數,可讓您存取重要的 AWS 環境資訊,例如帳戶 IDs、區域名稱,以及可在部署或環境之間變更的堆疊詳細資訊。
您可以使用虛擬參數,而不是硬式編碼值,讓您的範本更方便攜帶,並更輕鬆地在不同 AWS 帳戶 和 區域重複使用。
語法
您可以使用Ref
內部 函數或Fn::Sub
內部 函數來參考虛擬參數。
Ref
Ref
內部函數使用下列一般語法。如需詳細資訊,請參閱 Ref。
JSON
{ "Ref" : "AWS::
PseudoParameter
" }
YAML
!Ref AWS::
PseudoParameter
Fn::Sub
Fn::Sub
內部函數使用不同的格式,其中包含 虛擬參數周圍的${}
語法。如需詳細資訊,請參閱 Fn::Sub。
JSON
{ "Fn::Sub" : "${AWS::
PseudoParameter
}" }
YAML
!Sub '${AWS::
PseudoParameter
}'
可用的虛擬參數
AWS::AccountId
傳回要在其中建立堆疊之帳戶的 AWS 帳戶 ID,例如 123456789012
。
在定義涉及帳戶特定 ARNs 的 IAM 角色、政策和其他資源政策時,通常會使用此虛擬參數。
AWS::NotificationARNs
傳回接收堆疊事件通知之 Amazon SNS 主題的 Amazon Resource Name ARNs) 清單。您可以在建立 AWS CLI 或更新堆疊時,透過 中的 --notification-arns
選項或透過 主控台指定這些 ARNs。
與其他傳回單一值的虛擬參數不同, AWS::NotificationARNs
會傳回 ARNs清單。若要存取清單中的特定 ARN,請使用 Fn::Select
內部函數。如需詳細資訊,請參閱Fn::Select。
AWS::NoValue
若在 Fn::If
內部函數中將此參數指定為傳回值,則系統會移除對應的資源屬性。如需詳細資訊,請參閱Fn::If。
此虛擬參數特別適用於建立條件式資源屬性,這些屬性應僅在特定條件下包含。
AWS::Partition
傳回資源所在的分割區。對於標準 AWS 區域,分割區為 aws
。如果資源處於其他分割區,則會傳回 aws-
partitionname
分割區。例如,中國 (北京和寧夏) 區域中資源的分割區是 ,aws-cn
G AWS GovCloud (美國西部) 區域中資源的分割區是 aws-us-gov
。
分割區構成資源 ARN 的一部分。使用 AWS::Partition
可確保您的範本在不同 AWS 分割區之間正確運作。
AWS::Region
傳回建立包容性資源區域的代表字串,例如:us-west-2
。
這是最常用的虛擬參數之一,因為它允許範本在不修改 AWS 區域 的情況下適應不同的參數。
AWS::StackId
傳回堆疊的 ID (ARN),例如 arn:aws:cloudformation:us-west-2:123456789012:stack/teststack/51af3dc0-da77-11e4-872e-1234567db123
。
AWS::StackName
傳回堆疊的名稱,例如 teststack
。
堆疊名稱通常用於建立可輕易識別為屬於特定堆疊的唯一資源名稱。
AWS::URLSuffix
在部署 AWS 區域 堆疊的 中傳回 AWS 網域的尾碼。尾碼通常為 amazonaws.com
,但對於中國 (北京) 區域,尾碼為 amazonaws.com.cn
。
此參數在建構 AWS 服務端點URLs 時特別有用。
範例
基本使用
下列範例會建立兩個資源:Amazon SNS 主題和傳送通知至該主題的 CloudWatch 警示。它們使用 AWS::StackName
、 AWS::Region
和 AWS::AccountId
,將堆疊名稱、目前 AWS 區域和帳戶 ID 動態插入資源名稱、描述和 ARNs。
JSON
{ "Resources": { "MyNotificationTopic": { "Type": "AWS::SNS::Topic", "Properties": { "DisplayName": { "Fn::Sub": "Notifications for ${AWS::StackName}" } } }, "CPUAlarm": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": { "Fn::Sub": "Alarm for high CPU in ${AWS::Region}" }, "AlarmName": { "Fn::Sub": "${AWS::StackName}-HighCPUAlarm" }, "MetricName": "CPUUtilization", "Namespace": "AWS/EC2", "Statistic": "Average", "Period": 300, "EvaluationPeriods": 1, "Threshold": 80, "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [{ "Fn::Sub": "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${MyNotificationTopic}" }] } } } }
YAML
Resources: MyNotificationTopic: Type: AWS::SNS::Topic Properties: DisplayName: !Sub Notifications for ${AWS::StackName} CPUAlarm: Type: AWS::CloudWatch::Alarm Properties: AlarmDescription: !Sub Alarm for high CPU in ${AWS::Region} AlarmName: !Sub ${AWS::StackName}-HighCPUAlarm MetricName: CPUUtilization Namespace: AWS/EC2 Statistic: Average Period: 300 EvaluationPeriods: 1 Threshold: 80 ComparisonOperator: GreaterThanThreshold AlarmActions: - !Sub arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${MyNotificationTopic}
使用 AWS::NotificationARNs
下列範例會設定 Auto Scaling 群組,以傳送執行個體啟動事件和啟動錯誤的通知。組態使用AWS::NotificationARNs
虛擬參數,提供堆疊建立期間指定的 Amazon SNS 主題 ARNs 清單。Fn::Select
函數會從該清單中選擇第一個 ARN。
JSON
"myASG": { "Type": "AWS::AutoScaling::AutoScalingGroup", "Properties": { "LaunchTemplate": { "LaunchTemplateId": { "Ref": "myLaunchTemplate" }, "Version": { "Fn::GetAtt": [ "myLaunchTemplate", "LatestVersionNumber" ] } }, "MaxSize": "1", "MinSize": "1", "VPCZoneIdentifier": [ "subnetIdAz1", "subnetIdAz2", "subnetIdAz3" ], "NotificationConfigurations" : [{ "TopicARN" : { "Fn::Select" : [ "0", { "Ref" : "AWS::NotificationARNs" } ] }, "NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH", "autoscaling:EC2_INSTANCE_LAUNCH_ERROR" ] }] } }
YAML
myASG: Type: AWS::AutoScaling::AutoScalingGroup Properties: LaunchTemplate: LaunchTemplateId: !Ref myLaunchTemplate Version: !GetAtt myLaunchTemplate.LatestVersionNumber MinSize: '1' MaxSize: '1' VPCZoneIdentifier: - subnetIdAz1 - subnetIdAz2 - subnetIdAz3 NotificationConfigurations: - TopicARN: Fn::Select: - '0' - Ref: AWS::NotificationARNs NotificationTypes: - autoscaling:EC2_INSTANCE_LAUNCH - autoscaling:EC2_INSTANCE_LAUNCH_ERROR
使用 的條件式屬性 AWS::NoValue
下列範例會建立僅在提供快照 ID 時使用快照的 Amazon RDS 資料庫執行個體。若 UseDBSnapshot
條件評估為 true,CloudFormation 會對 DBSnapshotIdentifier
屬性使用 DBSnapshotName
參數值。若條件評估為 false,CloudFormation 即會移除 DBSnapshotIdentifier
屬性。
JSON
"MyDB" : { "Type" : "AWS::RDS::DBInstance", "Properties" : { "AllocatedStorage" : "5", "DBInstanceClass" : "db.t2.small", "Engine" : "MySQL", "EngineVersion" : "5.5", "MasterUsername" : { "Ref" : "DBUser" }, "MasterUserPassword" : { "Ref" : "DBPassword" }, "DBParameterGroupName" : { "Ref" : "MyRDSParamGroup" }, "DBSnapshotIdentifier" : { "Fn::If" : [ "UseDBSnapshot", {"Ref" : "DBSnapshotName"}, {"Ref" : "AWS::NoValue"} ] } } }
YAML
MyDB: Type: AWS::RDS::DBInstance Properties: AllocatedStorage: '5' DBInstanceClass: db.t2.small Engine: MySQL EngineVersion: '5.5' MasterUsername: Ref: DBUser MasterUserPassword: Ref: DBPassword DBParameterGroupName: Ref: MyRDSParamGroup DBSnapshotIdentifier: Fn::If: - UseDBSnapshot - Ref: DBSnapshotName - Ref: AWS::NoValue