Thursday, January 09, 2020

AWS CloudFormation vs. Terraform

CloudFormation vs. Terraform | cloudonaut

Scope

https://ryaneschinger.com/blog/aws-cloudformation-vs-terraform/

CloudFormation: using JSON or YAML for templates

JSON
{
  "Parameters": {
    "KeyName": {
      "Description": "The EC2 Key Pair for SSH access",
      "Type": "AWS::EC2::KeyPair::KeyName"
    }
  },
  "Resources": {
    "CFExampleInstance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "SecurityGroups": [ { "Ref": "CFSSHAccess" } ],
        "KeyName": { "Ref": "KeyName" },
        "ImageId": "ami-0f4cfd64",
        "InstanceType": "t1.micro"
      }
    },
    "CFSSHAccess": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "SSH access",
        "SecurityGroupIngress": [ {
          "IpProtocol": "tcp",
          "FromPort": "22",
          "ToPort": "22",
          "CidrIp": "0.0.0.0/0"
        } ]
      }
    }
  }
}
YAML
Parameters:
  KeyName:
    Description: The EC2 Key Pair for SSH access
    Type: AWS::EC2::KeyPair::KeyName
Resources:
  CFExampleInstance:
    Type: AWS::EC2::Instance
    Properties:
      SecurityGroups:
      - Ref: CFSSHAccess
      KeyName:
        Ref: KeyName
      ImageId: ami-0f4cfd64
      InstanceType: t1.micro
  CFSSHAccess:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: SSH access
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: '22'
        ToPort: '22'
        CidrIp: 0.0.0.0/0
Teraform custom syntax in HCL (HashiCorp Configuration Language)

variable "key_name" {}

resource "aws_instance" "TFExampleInstance" {
  ami = "ami-0f4cfd64"
  instance_type = "t1.micro"
  key_name = "${var.key_name}"
  security_groups = [ "${aws_security_group.TFSSHAccess.name}" ]
}

resource "aws_security_group" "TFSSHAccess" {
  name = "TFSSHAccess"
  description = "SSH access"

  ingress {
    protocol = "tcp"
    from_port = 22
    to_port = 22
    cidr_blocks = ["0.0.0.0/0"]
  }
}
@hackernoon


hashicorp/terraform @GitHub

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.