Susana  Stark

Susana Stark

1614570600

Terraform Workspaces

Terraform Workspaces
Source code: https://github.com/HoussemDellai/terraform-course

#terraform

What is GEEK

Buddha Community

Terraform Workspaces

Getting Started With Terraform Modules

Introduction

In this article, we will see a subtle introduction to terraform modules, how to pass data into the module, get something from the module and create a resource (GKE cluster), it’s intended to be as simple as possible just to be aware of what a module is composed of, or how can you do your own modules, sometimes it makes sense to have modules to abstract implementations that you use over several projects, or things that are often repeated along the project. So let’s see what it takes to create and use a module.

The source code for this article can be found here. Note that in this example I’m using GCP since they give you $300 USD for a year to try their services and it looks pretty good so far, after sign-up you will need to go to IAM, then create a service account and after that export the key (this is required for the terraform provider to talk to GCP).

Composition of a Module

A module can be any folder with a main.tf file in it, yes, that is the only required file for a module to be usable, but the recommendation is that you also put a README.md file with a description of the module if it’s intended to be used by people if it’s a sub-module it’s not necessary, also you will need a file called variables.tf and other outputs.tf of course if it’s a big module that cannot be split into sub-modules you can split those files for convenience or readability, variables should have descriptions so the tooling can show you what are they for, you can read more about the basics for a module here.

Before moving on let’s see the folder structure of our project:

Java

1

├── account.json

2

├── LICENSE

3

├── main.tf

4

├── module

5

│   ├── main.tf

6

│   ├── outputs.tf

7

│   └── variables.tf

8

├── README.md

9

└── terraform.tfvars

10

11

1 directory, 8 files

The Project

Let’s start with the main.tf that will call our module, notice that I added a few additional comments but it’s pretty much straight forward, we set the provider, then we define some variables, call our module and print some output (output can also be used to pass data between modules).

Java

1

## Set the provider to be able to talk to GCP

2

provider "google" {

3

  credentials = "${file("account.json")}"

4

  project     = "${var.project_name}"

5

  region      = "${var.region}"

6

}

7

8

## Variable definition

9

variable "project_name" {

10

  default = "testinggcp"

11

  type    = "string"

12

}

13

14

variable "cluster_name" {

15

  default = "demo-terraform-cluster"

16

  type    = "string"

17

}

18

19

variable "region" {

20

  default = "us-east1"

21

  type    = "string"

22

}

23

24

variable "zone" {

25

  default = "us-east1-c"

26

  type    = "string"

27

}

28

29

## Call our module and pass the var zone in, and get cluster_name out

30

module "terraform-gke" {

31

  source = "./module"

32

  zone = "${var.zone}"

33

  cluster_name = "${var.cluster_name}"

34

}

35

36

## Print the value of k8s_master_version

37

output "kubernetes-version" {

38

  value = module.terraform-gke.k8s_master_version

39

}

Then terraform.tfvars has some values to override the defaults that we defined:

Java

1

project_name = "testingcontainerengine"

2

cluster_name = "demo-cluster"

3

region = "us-east1"

4

zone = "us-east1-c"

#tutorial #devops #terraform #gcp cloud #terraform tutorial #kubernetes for beginners #terraform modules

Lindsey  Koepp

Lindsey Koepp

1602953760

Terraform Imports: Resources, Modules, for_each, and Count

If you are developing Terraform you will at some point work with Terraform imports. A simple web search yields plenty of results for simple imports of Terraform resources. However, often missing are some of the more complex or nuanced imports one might encounter in the real world (such as importing modules or resources created from for_each and count).

This guide will quickly cover the generic examples you find easily on the web, focus on some more unique stuff usually hidden in forum posts, and provide a handful of techniques I’ve picked up since Terraform imports became a functionality.

This guide assumes the reader has a good understanding of Terraform, Terraform modules, state file manipulation, and CI/CD. I’ll be using AWS for the examples.

Resource Import

This is perhaps the most prevalent example when searching for Terraform imports. Quite simply you have a resource defined in your Terraform code, some infrastructure out in the environment matching your Terraform resource definition, and you want to import that infrastructure into your Terraform state.

This example is for an aws_iam_user. I’ve already created a user named “bill” via the AWS IAM console and I would like to import this user into my Terraform state. Easy enough!

My Terraform code:

resource "aws_iam_user" "bill" {
  name = "bill"
  tags = { "foo" = "bar" }
}

A simple command:

$ terraform import aws_iam_user.bill bill

#aws #terraform-import #infrastructure-as-code #terraform-modules #terraform

Kole  Haag

Kole Haag

1603213200

Using Terraform to Create an EC2 Instance With Cloudwatch Alarm Metrics

Hey guys! I wanted to do a quick tutorial on how I created an EC2 module for Terraform. If you want to see the repository it is located in check it out here. This module will do a few things:

  1. Create an EC2 Instance
  2. Automatically look up the latest Windows Server 2019 AMI for the EC2 instance.
  3. Create and attach a additional drive.
  4. Create a Cloudwatch Alarm Metric to monitor CPU.

The folder structure looks like this:

Image for post

First things first… I created the main.tf file which contains all of my configuration except for the variables and outputs. The main.tf has a few parts to it.

AWS Instance Code

The first section is the instance resource code

#AWS Instance

resource "aws_instance" "example" {
     ami = data.aws_ami.windows.id
     instance_type = "t2.micro"
     availability_zone = var.availability_zone
}

You will notice a few things here.

  1. The instance type is set in the module to t2.micro
  2. availability_zone is set using a variable
  3. ami is set using data

We will get the the availability zone piece in just a bit, first we are going to tackle the data used for the ami argument.

Data for AMI Using a Filter

The next bit of code for the filter looks like this

#AMI Filter for Windows Server 2019 Base

data "aws_ami" "windows" {
     most_recent = true
     filter {
       name   = "name"
       values = ["Windows_Server-2019-English-Full-Base-*"]
  }
     filter {
       name   = "virtualization-type"
       values = ["hvm"]
  }
     owners = ["801119661308"] ## Canonical
}

The argument most_recent is set to true. This means that it will grab the most recent AMI that fits the criteria that we specify in our filter.

Next you will notice that in the name we set the value to *Windows_Server-2019-English-Full-Base- **with the star at the end. This lets Terraform know we don’t care about what text comes after that point and it was done because the standard format puts the date there. If we set the date the ami was created and set the most_recent argument to true it would not do us any good.

After that we set the virtualization-type to hvm. I am not going to go into a lot of detail here. Just know this is a good idea and do some additional research on hvm vs pv.

Last we set **owners **to 801119661308.

Now I am sure you are asking… how the heck do I actually get this information? Well you are going to have to run a quick command with the AWS cli.

First, login to AWS and get the ami you want to grab the information for. Here is an example:

Image for post

If you click on launch instance you can do a search.

#aws-ec2 #hashicorp-terraform #aws-cloudwatch #terraform-modules #terraform

Raleigh  Hayes

Raleigh Hayes

1626919020

Terraform Tutorial | Terraform For Beginners

Hello! Today we are looking at infrastructure as code with a terraform tutorial. Tools such as terraform are invaluable when it comes to dealing with infrastructure. Definitely worth taking a look :)

Useful Links:
GitHub: https://github.com/redhwannacef/youtube-tutorials/tree/master/terraform

Timestamps:
0:00 - Intro
0:24 - What is it?
0:54 - Key concepts

#terraform #terraform tutorial

Mireille  Von

Mireille Von

1625320020

Terraform Primer | Terraform Architecture | #2

This video covers the Terraform Architecture and Infrastructure as code

📌 Related Links

🔗 Terraform Getting Started: https://youtu.be/YXCuqueNQHk
🔗 Terraform setup: https://www.terraform.io/downloads.html
🔗 Github code: https://github.com/TechPrimers/terraform-primer
🔗 Terraform Providers: https://registry.terraform.io/browse/providers

📌 Related Playlist

🔗 Terraform Playlist - https://youtube.com/playlist?list=PLTyWtrsGknYcFBpNMcaRL9riUZSfkEON_
🔗Spring Boot Primer - https://www.youtube.com/playlist?list=PLTyWtrsGknYegrUmDZB6rcqMotOFZKvbn
🔗Spring Cloud Primer - https://www.youtube.com/playlist?list=PLTyWtrsGknYeOJHtd3Ll93GRf28hrjlHV
🔗Spring Microservices Primer - https://www.youtube.com/playlist?list=PLTyWtrsGknYdZlO7LAZFEElWkEk59Y2ak
🔗Spring JPA Primer - https://www.youtube.com/playlist?list=PLTyWtrsGknYdt079e1pyvpgLrJ48RQ1LK
🔗Java 8 Streams - https://www.youtube.com/playlist?list=PLTyWtrsGknYdqY_7lwcbJ1z4bvc5yEEZl
🔗Spring Security Primer - https://www.youtube.com/playlist?list=PLTyWtrsGknYe0Sba9o-JRtnRlkl4gXMQl

💥 Join TechPrimers Slack Community: https://bit.ly/JoinTechPrimers
💥 Telegram: https://t.me/TechPrimers
💥 TechPrimer HindSight (Blog): https://medium.com/TechPrimers
💥 Website: http://techprimers.com
💥 Slack Community: https://techprimers.slack.com
💥 Twitter: https://twitter.com/TechPrimers
💥 Facebook: http://fb.me/TechPrimers
💥 GitHub: https://github.com/TechPrimers or https://techprimers.github.io/

🎬Video Editing: FCP


🔥 Disclaimer/Policy:
The content/views/opinions posted here are solely mine and the code samples created by me are open sourced.
You are free to use the code samples in Github after forking and you can modify it for your own use.
All the videos posted here are copyrighted. You cannot re-distribute videos on this channel in other channels or platforms.
#Terraform #TerraformPrimer #TechPrimers

#terraform primer #terraform