Introduction to Terraform

18 Jun 2021, 9:36 a.m.
06:41 minutes

    Introduction

     Started 2014
     Terraform is the infrastructure as code offering from HashiCorp. 
     Single declarative language instead of configuration through various dashboards.
     HCL - HashiCorp Configuration Language (HCL) 
     HitashiCorp makes Vagrant, Packer, Vault, Consul, Nomad
     Two versions, Cloud (Open Source) and Enterprise (Paid For)
     Enterprise has better auditing and more templates. 
     Can run, Amazon AWS, Azure, Google Cloud, Oracle, Alibaba Cloud
     IT is commonly misunderstood that you can run a file against multi clouds verbatim i.e. without any modifications.
    

    Advantages

    • Easily repeatable
    • Easily readable
    • Operational certainty with "terraform plan"
    • Standardised environment builds
    • Quickly provisioned development environments
    • Disaster recovery
    

    Complimentary Technologies

    • Ansible
    • Chef
    • Puppet
    • SaltStack
    • Packer = You create a stack (like a container) that you deploy to a server. Docker changes the server (you run it on the server)
    • Docker = Containers
    • Kubernetes = Open Source Docker Container Manager
    • Nomad = HashiCorp version of Kubernetes (provisions containers)
    

    HCL Files

    • Split into two (Provider and Resource)
    • The provider block configures the named provider such as AWS, Azure, 
    • The resource block defines a piece of infrastructure. A resource might be a physical component such as an EC2 instance, or it can be a logical resource such as a Heroku application.
    

    Provider

    Go to terraform.io to see the list of providers.

    Current List

    ACMEAkamaiAlibaba CloudArchiveArukasAuth0Avi VantageAviatrixAWSAzureAzure Active DirectoryAzure StackA10 NetworksBaiduCloudBitbucketBrightboxCenturyLinkCloudCheck PointChefCherryServersCirconusCisco ASACisco ACICloudflareCloud-initCloudScale.chCloudStackCobblerCohesityConstellixConsulDatadogDigitalOceanDNSDNSimpleDNSMadeEasyDockerDome9DynEnterpriseCloudExoscaleExternalF5 BIG-IPFastlyFlexibleEngineFortiOSGenymotionGitHubGitLabGoogle Cloud PlatformGrafanaGridscaleHedvigHelmHerokuHetzner CloudHTTPHuaweiCloudHuaweiCloudStackIcinga2IgnitionIncapsulaInfluxDBInfobloxJDCloudKingsoftCloudKubernetesLaceworkLaunchDarklyLibratoLinodeLocalLogentriesLogicMonitorMailgunMetalCloudMongoDB AtlasMySQLNaver CloudNetlifyNew RelicNomadNS1NullNutanix1&1OktaOkta Advanced Server AccessOpenNebulaOpenStackOpenTelekomCloudOpsGenieOracle Cloud InfrastructureOracle Cloud PlatformOracle Public CloudOVHPacketPagerDutyPalo Alto NetworksPostgreSQLPowerDNSProfitBricksPureportRabbitMQRancherRancher2RandomRightScaleRubrikRundeckRunScopeScalewaySelectelSignalFxSkytapSoftLayerSpotinstStackPathStatusCakeSumo LogicTelefonicaOpenCloudTemplateTencentCloudTerraformTerraform CloudTimeTLSTritonTurbotUCloudUltraDNSVaultVenafiVMware CloudVMware NSX-TVMware vCloud DirectorVMware vRA7VMware vSphereVultrWavefrontYandex
    

    Declarative vs Procedural

    Terraform is a declarative type language.

    Declarative

    Define how the end-state should look

    There shall be a load balancer in front of a pool of 3 servers

    Procedural

    Perform set of actions to create an end state

    Installing Terraform

    https://www.terraform.io/downloads.html

    We can use Homebrew or Chocolatey on Windows to install Terraform. Alternatively, we can perform a manual installation with a binary package.

    Chocolatey

    Run the following after installing Chocolatey

    choco install terraform
    terraform --help
    

    Example Output

    Installing Terraform with Chocolately

    Manual

    For manual installation, you need to download the binary and add the location to your system path.

    Building Infrastructure

    • The set of files used to deploy your infrastructure is called a 'configuration'
    

    The prerequisite to run the below is an AWS Account and the AWS CLI installed.

    mkdir learn-terraform-aws-instance
    cd learn-terraform-aws-instance
    

    nano example.tf

            provider "aws" {
              profile = "default"
              region  = "us-east-1"
            }
    
            resource "aws_instance" "example" {
              ami           = "ami-2757f631"
              instance_type = "t2.micro"
            }
    
    terraform init
    terraform apply
    

    At this point, you will be prompted as below

    Confirm Your Terraform Plan
    Confirm Your Terraform Plan

    At this point you can cancel out and it will have no effect, alternatively you can proceed. Terraform will create your infrastructure.

    You can run the following to see your infrastructure.

    terraform show 
    

    When you have finished you may run the following to tear down the infrastructure.

    terraform destroy
    

    Coding

    Visual Studio Code, IntelliJ, Eclipse, all have support for terraform.
    • Lists are the same as arrays in Php or Dictionary in PythonSets are like Tuples in Python.
    

    We can set variables

    variable "location" {
      description = "Name of the Azure region to create resources in"
      default     = "westus2"
      type        = string
    }
    

    We can use variables

    resource "azurerm_resource_group" "rg" {
      name     = "tf-variables-rg"
      location = var.location
    }
    
    Setting Variables
    Setting Variables

    Cross References

    Setting Cross References
    Setting Cross References

    Changing Configuration

    As you change Terraform configurations, Terraform builds an execution plan that only modifies what is necessary to reach your desired state.

    Resources

    Repo from Cybrary Trainer https://github.com/jleonelion/fundamentals-of-terraform

    Terraform Releases https://releases.hashicorp.com/terraform/

    Captcha: What's the standard TCP port of the following service?

    captcha

    0 comments