這是新的 CloudFormation 範本參考指南。請更新您的書籤和連結。如需開始使用 CloudFormation 的說明,請參閱 AWS CloudFormation 使用者指南。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DependsOn 屬性
使用 DependsOn 屬性,您可以指定特定資源遵循另一項資源建立。當您將 DependsOn 屬性新增至資源時,只有在建立 DependsOn 屬性中指定的資源之後才能建立該資源。
重要
相依堆疊也有目標屬性 !Ref、!GetAtt 和 !Sub 形式的隱含相依性。例如,如果資源 A 的屬性使用資源 B 的 !Ref,則適用下列規則:
-
資源 B 會在資源 A 之前建立。
-
資源 A 會在資源 B 之前刪除。
-
資源 B 會在資源 A 之前更新。
您可以使用 DependsOn 屬性搭配任何資源。以下是一些典型用法:
-
決定等待條件何時生效。如需詳細資訊,請參閱《AWS CloudFormation 使用者指南》中的在範本中建立等待條件。
-
針對必須依特定順序建立或刪除的資源宣告相依性。例如,您必須針對 VPC 中的一些資源,明確宣告對閘道連接的相依性。如需詳細資訊,請參閱當 DependsOn 屬性是必要項目時。
-
建立、更新或刪除資源時覆寫預設平行處理原則。 CloudFormation 會盡可能平行建立、更新及刪除資源。它會自動決定範本中的哪些資源可以平行處理,以及哪些資源具有需要先完成其他操作的相依性。您可以使用
DependsOn明確指定相依性,這會覆寫預設平行處理原則,並引導 CloudFormation 依指定順序在這些資源上操作。
注意
在堆疊更新期間,會自動更新相依於已更新資源的資源。CloudFormation 不會對自動更新的資源進行任何變更,但如果堆疊政策與這些資源相關聯,您的帳戶必須具備更新資源的許可。
語法
DependsOn 屬性可以接受單一字串或字串清單。
"DependsOn" : [String, ...]
範例
下列範本包含 AWS::EC2::Instance 資源,其DependsOn屬性指定 myDB,即 AWS::RDS::DBInstance。CloudFormation 建立此堆疊時,會先建立 myDB,然後建立 Ec2Instance。
JSON
{ "Resources" : { "Ec2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId": "{{resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64}}", "InstanceType": "t2.micro" }, "DependsOn" : "myDB" }, "myDB" : { "Type" : "AWS::RDS::DBInstance", "Properties" : { "AllocatedStorage" : "5", "DBInstanceClass" : "db.t2.small", "Engine" : "MySQL", "EngineVersion" : "5.5", "MasterUsername" : "{{resolve:secretsmanager:MySecret:SecretString:username}}", "MasterUserPassword" : "{{resolve:secretsmanager:MySecret:SecretString:password}}" } } } }
YAML
Resources: Ec2Instance: Type: AWS::EC2::Instance Properties: ImageId: '{{resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64}}' InstanceType: t2.micro DependsOn: myDB myDB: Type: AWS::RDS::DBInstance Properties: AllocatedStorage: '5' DBInstanceClass: db.t2.small Engine: MySQL EngineVersion: '5.5' MasterUsername: '{{resolve:secretsmanager:MySecret:SecretString:username}}' MasterUserPassword: '{{resolve:secretsmanager:MySecret:SecretString:password}}'
當 DependsOn 屬性是必要項目時
VPC 閘道連接
VPC 中有些資源需要閘道 (網際網路或 VPN 閘道)。如果您的 CloudFormation 範本定義了 VPC、閘道和閘道連接,則需要閘道的任何資源都會相依於閘道連接。例如,如果同時在相同的範本中宣告了 VPC 和 InternetGateway 資源,具有公有 IP 地址的 Amazon EC2 執行個體會相依於 VPC 閘道連接。
目前,當下列資源有相關聯的公有 IP 地址且位於 VPC 時,這些資源會相依於 VPC 閘道連接。
-
Auto Scaling 群組的能力
-
Amazon EC2 執行個體
-
Elastic Load Balancing 負載平衡器
-
彈性 IP 位址
-
Amazon RDS 資料庫執行個體
-
包含網際網路閘道的 Amazon VPC 路由
當您有 VPN 閘道時,VPN 閘道路由傳播會相依於 VPC 閘道連接。
下列程式碼片段示範閘道連接範例,以及相依於閘道連接的 Amazon EC2 執行個體:
JSON
"GatewayToInternet" : { "Type" : "AWS::EC2::VPCGatewayAttachment", "Properties" : { "VpcId" : { "Ref" : "VPC" }, "InternetGatewayId" : { "Ref" : "InternetGateway" } } }, "EC2Host" : { "Type" : "AWS::EC2::Instance", "DependsOn" : "GatewayToInternet", "Properties" : { "InstanceType" : "t2.micro", "ImageId": "{{resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64}}", "KeyName" : { "Ref" : "KeyName" }, "NetworkInterfaces" : [ { "GroupSet" : [ { "Ref" : "EC2SecurityGroup" } ], "AssociatePublicIpAddress" : "true", "DeviceIndex" : "0", "DeleteOnTermination" : "true", "SubnetId" : { "Ref" : "PublicSubnet" } } ] } }
YAML
GatewayToInternet: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: Ref: VPC InternetGatewayId: Ref: InternetGateway EC2Host: Type: AWS::EC2::Instance DependsOn: GatewayToInternet Properties: InstanceType: t2.micro ImageId: '{{resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64}}' KeyName: Ref: KeyName NetworkInterfaces: - GroupSet: - Ref: EC2SecurityGroup AssociatePublicIpAddress: 'true' DeviceIndex: '0' DeleteOnTermination: 'true' SubnetId: Ref: PublicSubnet
Amazon ECS 服務和 Auto Scaling 群組
當您使用 Auto Scaling 或 Amazon Elastic Compute Cloud (Amazon EC2) 建立 Amazon ECS 叢集的容器執行個體時,Amazon ECS 服務資源必須相依於 Auto Scaling 群組或 Amazon EC2 執行個體,如下列程式碼片段所示。如此一來,您就可以在 CloudFormation 建立 Amazon ECS 服務之前使用容器執行個體,並將其與 Amazon ECS 叢集建立關聯。
JSON
"service": { "Type": "AWS::ECS::Service", "DependsOn": [ "ECSAutoScalingGroup" ], "Properties" : { "Cluster": { "Ref": "ECSCluster" }, "DesiredCount": "1", "LoadBalancers": [ { "ContainerName": "simple-app", "ContainerPort": "80", "LoadBalancerName" : { "Ref" : "EcsElasticLoadBalancer" } } ], "Role" : { "Ref":"ECSServiceRole" }, "TaskDefinition" : { "Ref":"taskdefinition" } } }
YAML
service: Type: AWS::ECS::Service DependsOn: - ECSAutoScalingGroup Properties: Cluster: Ref: ECSCluster DesiredCount: 1 LoadBalancers: - ContainerName: simple-app ContainerPort: 80 LoadBalancerName: Ref: EcsElasticLoadBalancer Role: Ref: ECSServiceRole TaskDefinition: Ref: taskdefinition
IAM 角色政策
進行額外呼叫的資源 AWS 需要服務角色,該角色允許服務 AWS 代表您呼叫 。例如,AWS::CodeDeploy::DeploymentGroup 資源需要服務角色,CodeDeploy 才有將應用程式部署至您執行個體的許可。當您有定義服務角色的單一範本時,角色的政策 (使用 AWS::IAM::Policy 或 AWS::IAM::ManagedPolicy 資源) 及使用角色的資源會新增相依性,讓資源相依於角色的政策。此相依性可確保資源整個生命週期都能使用政策。
例如,假設您有一個範本,其中包含部署群組資源、服務角色和角色的政策。當您建立堆疊時,CloudFormation 無法在建立角色的政策之前建立部署群組。若無此相依性,CloudFormation 可能會在建立角色的政策之前建立部署群組資源。如果發生此情況,由於許可不足,因此部署群組將無法建立。
如果角色有內嵌政策,請勿指定相依性。CloudFormation 會同時建立角色及其政策。