Is it possible to execute a CloudFormation file in Terraform?

One team has already written a cloud information template as an .yml file that contains a resource stack.

Can I use this file by executing it from Terraform? Or should it be rewritten?

I am new to terraform and just getting started.

If I used AWS CLI, I would execute such a command,

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

I would like to include the equivalent of this command in the terraform configuration.

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

Thanks!

+5
source share
2 answers

The aws_cloudformation_stack resource serves as a bridge from Terraform to CloudFormation, which can be used either as an aid to move from CloudFormation to Terraform (as you obviously are here), or use some CloudFormation features that Terraform does not currently handle, for example, deployment Deploy new instances in ASG.

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

The parameters argument allows you to transfer data from Terraform to the Cloudformation stack. It is also possible to use the outputs attribute to use the results of the CloudFormation stack elsewhere in Terraform for 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 an existing CloudFormation stack that is not controlled by Terraform, you can still use its outputs using the aws_cloudformation_stack source data :

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

Together, these features allow you to efficiently mix CloudFormation and Terraform in the same system in different combinations, whether it’s a temporary measure during migration or on an ongoing basis in situations where a hybrid solution is required.

+9
source

I can confirm that template_body has a file link for the cloudformation template. I assume template_url also works fine. 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")}" } 
+1
source

Source: https://habr.com/ru/post/1266446/


All Articles