简体   繁体   English

具有来自当前CFT的变量的AWS Cloudformation模板

[英]AWS Cloudformation Template with variable from current CFT

I'm having a hard time figuring out how to use a value from a current stack when applying a new change-set template. 我很难确定在应用新的变更集模板时如何使用当前堆栈中的值。 As you can imagine, many of the current configuration parameters of a stack are going to be needed to re-apply a new set. 可以想象,要重新应用新集合,将需要使用堆栈的许多当前配置参数。 However, I can't really find the definitive documentation on how to do this. 但是,我真的找不到关于如何执行此操作的权威文档。

So far, I'm looking at something like this. 到目前为止,我正在寻找类似的东西。

  "DomainName": {
    "Value": {"Ref": "AWS::S3::Bucket::DomainName"}
  }

Where I'm hoping the change set generator will use this Ref value to get the current domain name of the configured S3 bucket. 我希望变更集生成器将使用该Ref值来获取已配置S3存储桶的当前域名。

Maybe this is not how change-sets work at all? 也许这根本不是变更集的工作方式? I would imagine the configuration is merged with the previous configuration at run-time, so you should have access to some of the previous instance variables? 我可以想象该配置在运行时与先前的配置合并,因此您应该可以访问某些先前的实例变量?

Thanks. 谢谢。

What you define in CloudFormation template are so called Resources . 您在CloudFormation模板中定义的内容称为Resources Example of resources are: 资源示例为:

  • AWS::S3::Bucket AWS :: S3 :: Bucket
  • AWS::EC2::Instance AWS :: EC2 ::实例
  • AWS::EC2::VPC AWS :: EC2 :: VPC

Each resource has Properties that you define when declaring them in a CloudFormation template. 每个资源都有在CloudFormation模板中声明它们时定义的Properties Let's take AWS::S3::Bucket resource as an example. 让我们以AWS::S3::Bucket资源为例。

{
  "Resources" : {

    "MyBucket" : {
      "Type" : "AWS::S3::Bucket",
      "Properties" : {
        "BucketName" : "my-bucket-name"
      }
    }

  }
}

BucketName is a property of "MyBucket" resource. BucketName是“ MyBucket”资源的属性。

Resources typically also have Return Values . 资源通常还具有Return Values A return value is something that you can use in your CloudFormation templates to build cross-reference logic and relationships between resources. 您可以在CloudFormation模板中使用返回值来建立交叉引用逻辑和资源之间的关系。 Each resource usually has a "default" return value, and this value is returned when you reference a certain resource using Ref intrinsic function within the template. 每个资源通常都有一个“默认”返回值,当您使用模板中的Ref内在函数引用某个资源时,将返回此值。 In case of AWS::S3::Bucket resource, this "default" return value is simply a BucketName . 对于AWS::S3::Bucket资源,此“默认”返回值只是BucketName This means that if you would like to use my-bucket-name somewhere else in your template, you would use Ref intrinsic function on MyBucket resource. 这意味着,如果您想在模板的其他位置使用my-bucket-name ,则可以在MyBucket资源上使用Ref内部函数。 As an example, we can create AWS::S3::BucketPolicy resource, BucketName (in fact Bucket ) is one of properties of AWS::S3::BucketPolicy . 例如,我们可以创建AWS::S3::BucketPolicy资源, BucketName (实际上是Bucket )是AWS::S3::BucketPolicy的属性之一。 Let's do it then: 然后让我们这样做:

{
  "Resources" : {

    "MyBucket" : {
      "Type" : "AWS::S3::Bucket",
      "Properties" : {
        "BucketName" : "my-bucket-name"
      }
    },

    "MyBucketPolicy" : {
      "Type" : "AWS::S3::BucketPolicy",
      "Properties" : {
        "Bucket" : { "Ref" : "MyBucket" },
        "PolicyDocument" : JSON_STRUCTURE_SKIPPED_FOR_CLARITY
      }
    }

  }
}

Note that I didn't write "Bucket" : "my-bucket-name", in MyBucketPolicy resource above, I actually want to reference MyBucket there. 请注意"Bucket" : "my-bucket-name",在上面的MyBucketPolicy资源中,我没有写"Bucket" : "my-bucket-name",实际上我想在那里引用MyBucket CloudFormation engine will replace { "Ref" : "MyBucket" } value with "my-bucket-name" when I deploy the template. 部署模板时, { "Ref" : "MyBucket" }引擎会将{ "Ref" : "MyBucket" }值替换为"my-bucket-name"

Some resources provide extra Return Values (attributes), these values are accessible by using Fn::GetAtt intrinsic function. 某些资源提供了额外的Return Values (属性),可以使用Fn::GetAtt内部函数访问这些值。 AWS::S3::Bucket extra return values / attributes are: AWS :: S3 :: Bucket额外的返回值/属性为:

  • DomainName 域名
  • DualStackDomainName DualStackDomainName
  • WebsiteURL 网址

Syntax of Fn::GetAtt intrinsic function is shown below: Fn::GetAtt内在函数的语法如下所示:

{ "Fn::GetAtt" : [ "logicalNameOfResource", "attributeName" ] }

In order to get DomainName from "AWS::S3::Bucket resource, you would do it like this: 为了从"AWS::S3::Bucket资源"AWS::S3::Bucket获取DomainName ,您需要这样做:

{ "Fn::GetAtt" : [ "MyBucket", "DomainName" ] }

You could use DomainName for example in CloudFormation stack outputs section. 例如,您可以在CloudFormation堆栈输出部分中使用DomainName Complete example follows: 完整的示例如下:

{
  "Resources" : {

    "MyBucket" : {
      "Type" : "AWS::S3::Bucket",
      "Properties" : {
        "BucketName" : "my-bucket-name"
      }
    },

    "MyBucketPolicy" : {
      "Type" : "AWS::S3::BucketPolicy",
      "Properties" : {
        "Bucket" : { "Ref" : "MyBucket" },
        "PolicyDocument" : JSON_STRUCTURE_SKIPPED_FOR_CLARITY
      }
    }

  },

  "Outputs" : {

    "MyBucketDomainName" : {
      "Description" : "Domain name of my bucket",
      "Value" : { "Fn::GetAtt" : [ "MyBucket", "DomainName" ] }
    }
  }
}

You will find information about all Resources , their Properties and Return Values (these accessible via Ref as well as Fn::GetAtt functions) in AWS Resource Types Reference . 您将在AWS Resource Types Reference中找到有关所有Resources ,其PropertiesReturn Values (可通过Ref以及Fn::GetAtt函数访问)的信息。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM