What is the difference between terraform vs cloudformation?

279    Asked by Diyatomar in AWS , Asked on May 15, 2024

 I have been asked for a particular task that is related to automating the infrastructure deployment for a company’s new project. The project involves setting up a highly scalable web-based application on AWS. Should I choose Terraform or AWS Cloudformation for this particular task and why? 

Answered by elonjigar

In the context of AWS, for this particular scenario you can choose terraform over AWS cloudformation. While the cloudformation is AWS–specific and tightly integrated with AWS services, terraform on the other hand can offer multi cloud support, allowing you to infrastructure provisioning across various cloud providers like AWS Azure, and google cloud platform. Additionally, terraform’s declarative Configuration language, HCL(hashiCorp Configuration language), provides a more concise and readable syntax as compared to Cloudformation’s JSON or even YAML. This flexibility and readability can make terraform a preferred choice to manage infrastructure as code in a multi-cloud environment.

In the terms of coding, here is an example of how you can use the terraform for the purpose of deploying the described infrastructure:-

Provider “aws” {
  Region = “us-west-2”
}
Resource “aws_vpc” “example” {
  Cidr_block = “10.0.0.0/16”
}
Resource “aws_subnet” “example” {
  Vpc_id = aws_vpc.example.id
  Cidr_block = “10.0.1.0/24”
}
Resource “aws_instance” “example” {
  Ami = “ami-0c55b159cbfafe1f0”
  Instance_type = “t2.micro”
  Subnet_id = aws_subnet.example.id
}

Here is the example given of how you can use the Python programming language with the Boto3 library to achieve a similar infrastructure as the above provisioning on AWS:-

Import boto3
Ec2 = boto3.resource(‘ec2’, region_name=’us-west-2’)
Vpc = ec2.create_vpc(CidrBlock=’10.0.0.0/16’)
Subnet = vpc.create_subnet(CidrBlock=’10.0.1.0/24’)
Ami_id = ‘ami-0c55b159cbfafe1f0’
Instance = ec2.create_instances(
    ImageId=ami_id,
    InstanceType=’t2.micro’,
    MaxCount=1,
    MinCount=1,
    SubnetId=subnet.id
)[0]

Here is the same example given in java programming language of how you can use java with the AWS SDK for achieving the similar infrastructure provisioning on AWS:-

Import com.amazonaws.services.ec2.AmazonEC2;
Import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
Import com.amazonaws.services.ec2.model.*;
Public class CreateEC2Instance {
    Public static void main(String[] args) {
        AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard()
                .withRegion(“us-west-2”)
                .build();
        String vpcId = createVPC(ec2);
        String subnetId = createSubnet(ec2, vpcId);
        String amiId = “ami-0c55b159cbfafe1f0”;
        RunInstancesRequest runRequest = new RunInstancesRequest()
                .withImageId(amiId)
                .withInstanceType(InstanceType.T2Micro)
                .withMinCount(1)
                .withMaxCount(1)
                .withSubnetId(subnetId);
        Ec2.runInstances(runRequest);
    }
    Public static String createVPC(AmazonEC2 ec2) {
        CreateVpcRequest request = new CreateVpcRequest()
                .withCidrBlock(“10.0.0.0/16”);
        CreateVpcResult result = ec2.createVpc(request);
        Return result.getVpc().getVpcId();
    }
    Public static String createSubnet(AmazonEC2 ec2, String vpcId) {
        CreateSubnetRequest request = new CreateSubnetRequest()
                .withCidrBlock(“10.0.1.0/24”)
                .withVpcId(vpcId);
        CreateSubnetResult result = ec2.createSubnet(request);
        Return result.getSubnet().getSubnetId();
    }
}


Your Answer

Interviews

Parent Categories