是否可以在 Terraform 中执行 CloudFormation 文件? [英] Is it possible to execute a CloudFormation file in Terraform?

查看:31
本文介绍了是否可以在 Terraform 中执行 CloudFormation 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个团队已经将 cloudformation 模板编写为 .yml 文件,该文件提供了一堆资源.

One team has already written a cloudformation template as a .yml file that provisions a stack of resources.

是否可以通过在 Terraform 中执行该文件来利用它?还是必须重写?

Is it possible to leverage this file by executing it from within Terraform? Or does it have to be rewritten?

我是 terraform 新手,刚刚开始.

I'm new to terraform and just getting started.

如果我使用 AWS CLI,我会执行这样的命令,

If I were using the AWS CLI I would execute a command like this,

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

我想在我的 terraform 配置中包含与此命令等效的命令.

I'd like to include the equivalent of this command in my terraform configuration.

如果可能的话,你可以给我举个例子,我会很感激的.

If it is possible and you can point me to an example, I would really appreciate that.

谢谢!

推荐答案

aws_cloudformation_stack 资源 充当从 Terraform 到 CloudFormation 的桥梁,可以用作从 CloudFormation 迁移到 Terraform 的辅助工具(正如您在此处所做的那样)或使用Terraform 目前无法处理的 CloudFormation 的一些功能,例如将新实例的部署滚动到 ASG.

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

parameters 参数允许将数据从 Terraform 传递到 Cloudformation 堆栈.也可以使用 outputs 属性来利用 Terraform 中其他地方的 CloudFormation 堆栈的 results,实现双向集成:

The parameters argument allows passing data from Terraform into the Cloudformation stack. 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:

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

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

<小时>

如果您有一个预先存在的 CloudFormation 堆栈由 Terraform 管理,您仍然可以通过 aws_cloudformation_stack 数据源:


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:

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"],
  ]
}

<小时>

这些功能让您可以在单个系统中以不同的组合有效地混合 CloudFormation 和 Terraform,无论是作为迁移时的临时措施还是在需要混合解决方案的情况下永久使用.


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.

这篇关于是否可以在 Terraform 中执行 CloudFormation 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆