Skip to content

GovTechStackSG/stackx2019-terraform-basics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lab Instructions

  • A HTML version with code snippets that you can copy is available at https://govtechstacksg.github.io/stackx2019-terraform-basics/

  • Login to your workstation

    Cloud9 URL: https://user#.stackx.govtechstack.sg/ide.html
    User ID: user#
    Password: password#
    Git URL: https://github.com/GovTechStackSG/stackx2019-terraform-basics

    To create a local copy of this git repository, run the following command in your Cloud9 environment.

    git clone https://github.com/GovTechStackSG/stackx2019-terraform-basics
  • Create a basic Terraform configuration

    mkdir terraform101
    cd terraform101
    • Initialise your first terraform file - main.tf
    • Create provider block for AWS
    provider "aws" {
      #access_key = "PLEASE_NO_SECRETS_IN_YOUR_TF"
      #secret_key = "WHAT_DID_I_JUST_SAY"
      region     = "ap-southeast-1"
    }
  • Initialize and apply the configuration (create infrastructure)

    • In the same Terraform configuration, main.tf , create an AWS instance with the ID web and the following:

      AMI ID: ami-0fa08c0a6b3f80751
      Instance Type: t2.micro
      Tags: Name

      TIP: You need to start with a resource called aws_instance.

      resource "aws_instance" "web" {
        ami           = "ami-0fa08c0a6b3f80751"
        instance_type = "t2.micro"
      
        tags = {
          Name = "user#-HelloStackX"
        }
      }
    • You must run terraform init after adding or removing providers.

      terraform init        
    • Run terraform plan to see what will happen.

      terraform plan
    • Run terraform apply to create resources on AWS.

      terraform apply
    • Run terraform show to get details of the resources you created on AWS.

      terraform show
    • Define useful output values that will be highlighted to the user when Terraform applies: IP addresses, usernames, generated keys.

      output "public_ip" {
        value = "${aws_instance.web.public_ip}"
      }
    • Create a new output variable named "public_dns" which outputs the instance's public_dns attribute.

      output "public_dns" {
        value = "${aws_instance.web.public_dns}"
      }
    • Run terraform refresh to pick up the new output values.

      terraform refresh
    • Use the terraform output command to query for:

      • All outputs
        terraform output
      • A single output
        terraform output public_dns
        ping $(terraform output public_dns)
    • Define the parameterization of Terraform configurations, using variables. Create two variables in the Terraform configuration:

      • region (default: ap-southeast-1)

         variable "region" {
          type    = "string"
          default = "ap-southeast-1"
        }
      • label (no default value)

        variable "label" {
          type = "string"
        }
    • Variables' values can be interpolated into a string using the “${var.}” syntax

      resource "aws_instance" "web" {
        ami           = "ami-0b5a47f8865280111"
        instance_type = "t2.micro"
      
        tags = {
          Name = "${var.label}-HelloStackX"
          Creator = "Cameron Huysmans"
          TTL = "8"
        }
      }
    • Replace the hard-coded values in main.tf file with references to the newly-defined variables using the interpolation syntax.

        variable "region" {
          default = "ap-southeast-1"
        }
        variable "label" {
          default = "terraform101"
        }
        
        provider "aws" {
          region     = "${var.region}"
        }
      
    • Run terraform plan now that the variables are parameterized. Observe what happens.

  • Change and re-apply the configuration

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published