【问题标题】:Is there a way to configure the 'Stack Name' of nested stacks in AWS cloudformation ?有没有办法在 AWS cloudformation 中配置嵌套堆栈的“堆栈名称”?
【发布时间】:2017-10-27 00:33:36
【问题描述】:

我正在尝试使用 AWS CloudFormation 创建嵌套堆栈。 我需要指定嵌套堆栈的“堆栈名称”。 我尝试使用带有键的“标签”属性:“堆栈名称”。但这没有帮助。 有没有办法在创建嵌套堆栈时提供堆栈名称作为输入?

【问题讨论】:

  • 为什么“需要指定嵌套堆栈的'堆栈名称'”?了解这一点可能有助于我们找到解决您问题的最佳方案。

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

如下所述,目前的答案是否定的。

原文:是的,它是作为堆栈名称的资源名称。下面你会得到一个名为 myStackName 的堆栈

YAML

AWSTemplateFormatVersion: "2010-09-09"
Resources: 
  myStackName: 
Type: "AWS::CloudFormation::Stack"
Properties: 
  TemplateURL: "https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2ChooseAMI.template"
  Parameters: 
    InstanceType: "t1.micro"
    KeyName: "mykey"

JSON

{
"AWSTemplateFormatVersion" : "2010-09-09",
   "Resources" : {
  "myStackName" : {
     "Type" : "AWS::CloudFormation::Stack",
     "Properties" : {
        "TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2ChooseAMI.template",
        "Parameters" : {
           "InstanceType" : "t1.micro",
           "KeyName" : "mykey"
        }
     }
  }
}
}

【讨论】:

  • 我试过这种方式。但是子堆栈的名称是像ParentStackName-ResourceName-UniqueID 一样生成的,而不仅仅是资源名称。例如,如果父堆栈名称为 My-Service,资源名称为 my-resource,则嵌套堆栈的名称为 My-Service-my-resource-IHTJ**JKT
  • @Neethu 恐怕这就是 AWS 目前使其工作的方式,而且我知道没有替代方案。很多 CF 资源名称都是以这种方式派生的,包括 ECS 集群和 ALB 目标,它们没有自己的名称参数。希望这会改变。
  • 所以现在的答案是“否”。
【解决方案2】:

在给定的模板中,您可以访问伪参数 stackId 和 stackName。这些可以标记为导出,然后在另一个堆栈中引用。加入后,他们会为您提供子堆栈的名称。

我发现以下视频虽然年代久远,但非常有用:https://youtu.be/6R44BADNJA8

此示例模板也可能有助于理解这些工作原理:https://s3.amazonaws.com/cloudformation-examples/user-guide/cross-stack/SampleNetworkCrossStack.template

{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "AWS CloudFormation Sample Template VPC_with_PublicIPs_And_DNS: Sample template that creates a VPC with DNS and public IPs enabled. Note that you are billed for the AWS resources that you use when you create a stack from this template.", "Resources" : { "VPC" : { "Type" : "AWS::EC2::VPC", "Properties" : { "EnableDnsSupport" : "true", "EnableDnsHostnames" : "true", "CidrBlock" : "10.0.0.0/16" } }, "PublicSubnet" : { "Type" : "AWS::EC2::Subnet", "Properties" : { "VpcId" : { "Ref" : "VPC" }, "CidrBlock" : "10.0.0.0/24" } }, "InternetGateway" : { "Type" : "AWS::EC2::InternetGateway" }, "VPCGatewayAttachment" : { "Type" : "AWS::EC2::VPCGatewayAttachment", "Properties" : { "VpcId" : { "Ref" : "VPC" }, "InternetGatewayId" : { "Ref" : "InternetGateway" } } }, "PublicRouteTable" : { "Type" : "AWS::EC2::RouteTable", "Properties" : { "VpcId" : { "Ref" : "VPC" } } }, "PublicRoute" : { "Type" : "AWS::EC2::Route", "DependsOn" : "VPCGatewayAttachment", "Properties" : { "RouteTableId" : { "Ref" : "PublicRouteTable" }, "DestinationCidrBlock" : "0.0.0.0/0", "GatewayId" : { "Ref" : "InternetGateway" } } }, "PublicSubnetRouteTableAssociation" : { "Type" : "AWS::EC2::SubnetRouteTableAssociation", "Properties" : { "SubnetId" : { "Ref" : "PublicSubnet" }, "RouteTableId" : { "Ref" : "PublicRouteTable" } } }, "PublicSubnetNetworkAclAssociation" : { "Type" : "AWS::EC2::SubnetNetworkAclAssociation", "Properties" : { "SubnetId" : { "Ref" : "PublicSubnet" }, "NetworkAclId" : { "Fn::GetAtt" : ["VPC", "DefaultNetworkAcl"] } } }, "WebServerSecurityGroup" : { "Type" : "AWS::EC2::SecurityGroup", "Properties" : { "GroupDescription" : "Enable HTTP ingress", "VpcId" : { "Ref" : "VPC" }, "SecurityGroupIngress" : [ { "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0" } ] } } }, "Outputs" : { "VPCId" : { "Description" : "VPC ID", "Value" : { "Ref" : "VPC" }, "Export" : { "Name" : {"Fn::Sub": "${AWS::StackName}-VPCID" }} }, "PublicSubnet" : { "Description" : "The subnet ID to use for public web servers", "Value" : { "Ref" : "PublicSubnet" }, "Export" : { "Name" : {"Fn::Sub": "${AWS::StackName}-SubnetID" }} }, "WebServerSecurityGroup" : { "Description" : "The security group ID to use for public web servers", "Value" : { "Fn::GetAtt" : ["WebServerSecurityGroup", "GroupId"] }, "Export" : { "Name" : {"Fn::Sub": "${AWS::StackName}-SecurityGroupID" }} } } }

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 2021-10-26
    • 2018-06-15
    • 2019-10-28
    • 1970-01-01
    • 2018-10-27
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多