amazon-web-services - 是否可以在 Terraform 中执行 CloudFormation 文件?

标签 amazon-web-services aws-cloudformation terraform

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

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

我是 terraform 的新手,刚刚开始。

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

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

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

如果可能的话,您可以给我举一个例子,我将非常感激。

谢谢!

最佳答案

The aws_cloudformation_stack resource作为从 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 堆栈的结果,以实现双向集成:

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

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

如果您有一个预先存在的 CloudFormation 堆栈,由 Terraform 管理,您仍然可以使用 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,无论是作为迁移时的临时措施,还是在需要混合解决方案的情况下永久使用。

关于amazon-web-services - 是否可以在 Terraform 中执行 CloudFormation 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43266506/

相关文章:

java - AWS Lambda 中的 S3 客户端初始化缓慢

amazon-web-services - AWS Glue 的工作原理是什么?

amazon-web-services - 如何覆盖 AWS CDK 资源逻辑 ID 而不导致强制转换异常

azure - 从 Azure Key Vault 访问多个 key 作为数据源

amazon-web-services - 如何在部署应用程序 Elastic beanstalk 上修改 NGINX 配置

amazon-web-services - 如何从 Amazon Kinesis 流中获取最新记录?

amazon-web-services - 将环境变量从 AWS Codepipeline 传递到 CodeBuild

amazon-web-services - 将静态私有(private) IP 地址设置为向 AWS Elastic Load Balancer 注册的 AWS EC2 实例

azure-devops - 在 Azure DevOps Pipelines (YAML) 中通过手动批准跳过阶段

azure - 使用 for_each 引用资源实例