Giter VIP home page Giter VIP logo

learn_tf's Introduction

learn_tf

0. Initial Configuration

0.1 Install terraform

0.2 Install aws manager

0.3 Set up aws user with IAM

1. General Setup

1.0 Configure AWS creds

Run the following to configure aws creds:

aws configure
  1. AWS Access Key ID:

  2. AWS Secret Access Key:

  3. Default region name: us-east-2

  4. Default output format: JSON

Run the following to initialize terraform:

terraform init

1.1 Terraform Files and Modules

Terraform files are files with the .tf extension.

Terraform generates .tfstate and .tfstate.backup files when running terraform apply, which may contain secrets, and so should NEVER be uploaded to repositories. They should be added to .gitignore file. Instead, .tfstate files should be stored in an S3 bucket so the team can reference the same state for infrastructure management. This allows locking state so parallel executions don't coincide. This also enables sharing output vlaues with other Terraform configuration or code.

The following snippet is an example of telling terraform where to store the .tfstate file. Note, the bucket must first exist in order for this configuration to be valid.

terraform {
    backend "s3" {
        region  = "us-east-1"
        key     = "terraformstatefile"
        bucket  = "supersecrets3bucket"
    }
}

Terraform also generates .terraform/ directories, which also should NEVER be uploaded to repositories, so they should also be added to .gitignore files.

Terraform has variables. It is best practice to separate the variables into a separate file, variables.tf from the main code body, main.tf.

Terraform references the folder it is in when using terraform apply, with no way of specifying a specific .tf file to run. This means every piece of IaC written should be organized into separate folders, or, in Terraform jargon, modules. For instance, the following will not allow us to run terraform apply for specific pieces of infrastructure, ie .tf files:

folder/
    |- infra1.tf
    |- infra2.tf
    |- variables.tf
    |- terraform.tfstate
    |- .terraform.lock.hcl

The more desirable way to organize .tf files is by modules, like so:

folder/
    |- infra1/
            |- main.tf
            |- variables.tf
            |- terraform.tfstate
            |- .terraform.lock.hcl
    |- infra2/
            |- main.tf
            |- variables.tf
            |- terraform.tfstate
            |- .terraform.lock.hcl

1.2 Useful Terraform Commands

  • terraform init - Prepare your working directory for other commands
  • terraform validate - Check whether the configuration is valid
  • terraform plan - Show changes required by the current configuration
  • terraform apply - Create or update infrastructure
  • terraform destroy - Destroy previously-created infrastructure
  • terraform graph - Generate a Graphviz graph of the steps in an operation. eg:
    • terraform graph > test1_graph.dot
    • dot -Tsvg test1_graph.dot > test1_graph.svg

1.3 Terraform State

terraform.tfstate files are stored locally by defualt, but can be stored remotely in something like S3. It maps real-world resources to Terraform configuration and tracks resource dependency metadata. Prior to any modification operation, Terraform refreshes the state file.

  • terraform state has the following subcommands:
    • list - List resources in the state
    • mv - Move an item in the state
    • pull - Pull current state and output to stdout
    • push - Update remote state from a local state file
    • replace-provider - Replace provider in the state
    • rm - Remove instances from the state
      • Useful if you want to preserve a resource
    • show - Show a resource in the state
      • Useful to get specifics of a resource managed by Terraform

1.4 Terraform Variables

1.4.1 Variable Syntax

1.4.2 Types

Terraform has the following primitive types:

  • number
    • replicas = 3
  • string
    • name = "cluster2"
  • bool
    • backup = true

Terraform has complex types which can be divided into 2 subtypes:

  1. collection: complex types which allow multiple values of one primitive type to be grouped together
  2. structural: complex types which allow multiple values of different primitive types to be grouped together.

Terraform has the following complex types:

  • list (collections)
    variable "mylist" {
        type = list(string)
        default = ["string1", "string2"]
    } 
  • tuple (collections, structural) ```terraform variable "mylist" { type = list(string) default = ("string1", "string2") }
  • map (collections)
  • set (collections, structural)
  • object (structural)
    variable "instructor" {
        type = object({
            name = string
            age = number
        })
    }

1.5 Terraform Modules

1.6 Terraform Built-In functions

Here is a reference of built-ins.

Consider the following block. What would the Name be?

variable "project-name" {
    type = string
    default = "prod"
}

resource "aws_vpc" "my-vpc" {
    cidr_block = "10.0.0.0/16"
    tags = {
        Name = join("-",["terraform", var.project-name])
    }
}

The Name variable would have the value of "terraform-prod".

Terraform functions can be explored in terraform console

1.7 Dynamic Blocks

Dynamic blocks allow us to dynamically construct repeatable nested configuration blocks inside Terraform. They iterate over complex variable types and output a nested block for each element in the complex variable.

resource "aws_security_group" "my-sg" {
    name = 'my-aws-security-group'
    vpc_id = aws_vpc.my-vpc.id
    ingress {
        from_port = 22
        to_port = 22
        protocol = "tcp"
        cidr_blocks = ["1.2.3.4/32"]
    }
    ingress {
        ... # ingress rules
    }
}

vs

resource "aws_security_group" "my-sg" {
    name = 'my-aws-security-group'
    vpc_id = aws_vpc.my-vpc.id
    dynamic "ingress" {
        for_each var.rules
        content {
            from_port = ingress.value["port"]
            to_port = ingress.value["port"]
            protocol = ingress.value["proto"]
            cidr_blocks = ingress.value["cidrs"]
        }
    }
}

learn_tf's People

Contributors

mattwiese123 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.