
Terraform vs Cloudformation is a hot topic nowadays. Are you feeling confident with your infrastructure? You can easily create and manage everything manually. Now you might think, “Why not add more functions and make it more robust?” So, you create a robust database and a dynamodb table. Then, you decide to create an S3 bucket to store the log data and metadata of the app.
Then you add some roles to go along with the Lambda function. Now, you want the AWS Athena to perform all analytics. Then you will have users access with login passwords and you give them the necessary permission to access. The process will get more complicated if you are running multiple servers. That’s where Terraform and Cloudformation come in.
In this article, we will talk about Terraform vs Cloudformation, what is the difference, and whether you can use terraform with cloudformation or not. So, let’s get started!
How Terraform works?
Terraform is open-source software that defines the infrastructure. It allows you to automate and manage your infrastructure, your platform, and the services that run on that platform or infrastructure. It uses declarative language. That means you don’t have to define every step of how this management and automation is done.
You just have to define what result you want and terraform will figure out how to execute them. Also, you can use an imperative style where you can specify how to execute each step. In other words, Terraform is used for provisioning infrastructure like creating VPC, spinning up the servers, creating the security with users and permissions, installing docker, etc for app deployment.
How Cloudformation works?
Cloudformation is used in AWS. Cloudformation defines a template file to define how the files should be created. So, you don’t need to create everything by pushing multiple buttons. All will be automated through the template file.
It is an infrastructure as a code that creates and defines a file to deploy a diagram to model an infrastructure. Then it will give you a status update and show an error and rollback automatically. You have to upload the template file and upload it in AWS. It is also great to move all this stuff to a different region. So, it doesn’t matter whether your infrastructure is complicated or not.
Does Terraform use Cloudformation?
You can add Terraform as a registry resource to Cloudformation. To do so, you will need the following things.
Available Terraform Server
The first thing you need to maintain is that the terraform server should always be available and ready for deployment. You can use cloudformation templates. This will create a new EC2 instance to install terraform.
IAM Roles
IAM means Identity and Access Management. You need to register a resource type in each region where you need to create two IAM roles.
- Logging role: Terraform will use it to create log entries.
- Execution role: Terraform server will use to deploy resources
You can place the terraform instances in a shared account and they can configure their resource types to use.
Installing Terraform Resource
You need to register the terraform resource first. To do so, you need to copy the public DNS name value and set up the script value from the output tab. Then you should register the private key into the system manage parameter. If you want to use an existing server, you should identify the existing server and configure it.
This is how you can create cloud formation for terraforming to control your public cloud resources and on-premises programmatically.
Differences between Terraform vs Cloudformation
Terraform has become one of the standards for IaC. Cloudformation has gained a lot of popularity for managing and running infrastructure. Let’s compare Terraform vs Cloudformation based on the following things
- Compatibility with Cloud: Terraform supports multiple cloud providers like Azure, Google, AWS, etc but cloudformation is supported only on AWS.
- Code Syntax: Cloudformation uses popular YAML and JSON and Terraform uses newly introduced HCL syntax.
- State Management: Terraform’s state file stores all information and works as a map for all terraform resources.
- Rollbacks: You must deploy the fixes and roll back to the previous configuration manually. But Cloudformation automatically backs up the infrastructure before deleting and automatically rolls back to the last working state.
- Modularity: Modules allow quick provisioning and repeatability. Terraform modules perform one task and can be reused. Cloudformation has no modules. Its stack is similar to modules but less flexible.
- Expertise Support: For cloudformation, you can get extra support from AWS experts for infrastructure. But, terraforming will require expertise. Otherwise, your infrastructure will collapse.
These are the key differences between cloudformation and terraform. But these differences don’t show the winner between them. Everyone has their own use cases.
How to Generate CloudFormation to Terraform?
There are some command line interface tools to generate terraform configurations for existing cloudformation templates. It means that you can generate configuration files with terraform state to manage the cloudformation templates. Actually, it doesn’t directly generate the terraform configuration for the individual cloudformation resource provision.
It generates a cloudformation stack resource file that manages the cloudformation stacks with terraform instead of AWS console or CLI. You can install the terraform binary by itself or use a tool to manage it. Also, there are other tools like JSON to manage the output. Then you have to do some coding stuff and then you are good to go.
Final Verdict
AWS wants to provide a great experience to users and app deployers with its cloud formation service. Terraform also offers a great experience in its own area. It is useful for multi-cloud architecture. You can also avoid the cons of Terraform by using it only to manage the stacks in the IaC tools.
On the other hand, cloudformation is still imperfect in some places. But it is used in a professional and reliable place. But the good news is, you can use terraform with cloudformation. So, you don’t have to pick the real winner. Rather you can use both of them as per your need.