简体   繁体   English

是否可以在 Terraform 中执行 CloudFormation 文件?

[英]Is it possible to execute a CloudFormation file in Terraform?

One team has already written a cloudformation template as a .yml file that provisions a stack of resources.一个团队已经将 cloudformation 模板编写为.yml文件,用于配置资源堆栈。

Is it possible to leverage this file by executing it from within Terraform?是否可以通过在 Terraform 中执行来利用该文件? Or does it have to be rewritten?还是必须重写?

I'm new to terraform and just getting started.我是 terraform 的新手,刚刚开始。

If I were using the AWS CLI I would execute a command like this,如果我使用 AWS CLI,我会执行这样的命令,

aws cloudformation create-stack --stack-name my-new-stack --template-body file://mystack.yml --parameters ParameterKey=AmiId aws cloudformation create-stack --stack-name my-new-stack --template-body file://mystack.yml --parameters ParameterKey=AmiId

I'd like to include the equivalent of this command in my terraform configuration.我想在我的 terraform 配置中包含与此命令等效的命令。

If it is possible and you can point me to an example, I would really appreciate that.如果可能的话,你可以给我举个例子,我真的很感激。

Thanks!谢谢!

The aws_cloudformation_stack resource serves as a bridge from Terraform into CloudFormation, which can be used either as an aid for migration from CloudFormation to Terraform (as you're apparently doing here) or to make use of some of CloudFormation's features that Terraform doesn't currently handle, such as rolling deployments of new instances into an ASG . aws_cloudformation_stack资源充当从 Terraform 到aws_cloudformation_stack的桥梁,可用作从aws_cloudformation_stack迁移到 Terraform 的辅助工具(正如您在此处所做的那样)或利用一些 CloudFormation 目前没有的 CloudFormation 功能处理,例如将新实例滚动部署到ASG 中

resource "aws_cloudformation_stack" "example" {
  name = "example"
  parameters = {
    VpcId = var.vpc_id
  }
  template_body = file("${path.module}/example.yml")
}

The parameters argument allows passing data from Terraform into the Cloudformation stack. parameters参数允许将数据从 Terraform 传递到 Cloudformation 堆栈。 It's also possible to use the outputs attribute to make use of the results of the CloudFormation stack elsewhere in Terraform, for a two-way integration:也可以使用outputs属性来利用 Terraform 中其他地方的 CloudFormation 堆栈的结果,进行双向集成:

resource "aws_route_53_record" "example" {
  name = "service.example.com"
  type = "CNAME"
  ttl  = 300

  records = [
    aws_cloudformation_stack.example.outputs["ElbHostname"],
  ]
}

If you have a pre-existing CloudFormation stack that isn't managed by Terraform, you can still make use of its outputs using the aws_cloudformation_stack data source :如果您有一个不由Terraform 管理的预先存在的 CloudFormation 堆栈,您仍然可以使用aws_cloudformation_stack数据源利用其输出:

data "aws_cloudformation_stack" "example" {
  name = "example"
}

resource "aws_route_53_record" "example" {
  name = "service.example.com"
  type = "CNAME"
  ttl  = 300

  records = [
    data.aws_cloudformation_stack.example.outputs["ElbHostname"],
  ]
}

These features together allow you to effectively mix CloudFormation and Terraform in a single system in different combinations, whether it's as a temporary measure while migrating or permanently in situations where a hybrid solution is desired.这些功能一起使您能够以不同的组合在单个系统中有效地混合 CloudFormation 和 Terraform,无论是作为迁移时的临时措施,还是在需要混合解决方案的情况下永久使用。

I can confirm that template_body with a file reference to a cloudformation template works.我可以通过对 cloudformation 模板的文件引用来确认 template_body 有效。 I assume template_url will also work great.我认为 template_url 也能很好地工作。 Example例子

resource "aws_cloudformation_stack" "my-new-stack" {
  name = "my-new-stack"
  parameters {
    Name="my-new-stack"
    Port="22"
    VpcId = "${var.vpc_id}"
  }
  template_body = "${file("${path.module}/mystack.yml")}"
}

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

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