Is it possible to execute a CloudFormation file in Terraform?

后端 未结 2 437
慢半拍i
慢半拍i 2021-02-07 12:58

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

Is it possible to leverage this file by executi

相关标签:
2条回答
  • 2021-02-07 13:18

    I can confirm that template_body with a file reference to a cloudformation template works. I assume template_url will also work great. 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")}"
    }
    
    0 讨论(0)
  • 2021-02-07 13:32

    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.

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

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

    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.

    0 讨论(0)
提交回复
热议问题