Tuesday, June 1, 2021

Terraform - Modules

 Terraform - 6-02-2021

class notes

Topic - Terraform Module

- Make complex idea into simpler one...

Plan:- When you want to create infrascture (cloud), create manually, or writing code (with terraform)

create module, organize your code


we try to create same infracture at Dev/test/prod env. with same app.

test -> verify -> deploy


main code variables
- mail.tf
- var.tf -> this is where we mostly make changes.

create one centralize folder and dump all  your planing, code everything..

parameterize ...



$ cd /workspace; mkdir modules; cd modules; mkdir ec2; cd ec2

everything you need for ec2, define here.

$ vi main.tf

google:
teffarorm.io -> registry -> proivideers -> aws -> documents -> search for aws_instance -> aws inrtance

and copy the section resource part

provider "aws" {
region = "ap-south-1"
profile = "default"

resource "aws_instance" "web" {
  ami = "ami-ssss"
  instance_type = var.mytype

  tags = {
    Name = "Hello World !!"
  }
}


get the id info for ami-id



$ vi var.tf
variable "mytype" {}    # define nothing - no value is defined

> terraform init



you can put this folder on gitlab, hub or locally, nfs and make a use of it.


import the module
$ vi dev.tf
module {

# source = "github://user@passwd ...
source = "../modules/ec2"
}

> terrafrom apply

vi dev.tf

module "mydevm" {
 source
}


> tf apply



> cd ../prod

> vi prod.tf
module " mydevm" {
 source = "../modules/ec2"

.....
}


> terraform init
> tf apply



mkdir ../myvpc
cd ../myvpc



check terrafrom web site for vpc

there are lots of variable names such as cidr, create route .. created
you can use it..

go to section vpc module ..




$ cat main.tf

provider "aws" {
region = "ap-south-1"
profile = "default"
}
module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  cidr = "10.10.1.0/16"
  }
}

> tf apply


practice: look for all the modules at the registry. if may not find the module you are looking for.

- Find the module you want to work on.
- Read the module documents
- you can createyour module or add content on existing modules with more features...
- create and share in public.
- recognization ...

publish on registry or github

write your plan, find the module to work on and add feature

google for
"terraform modules source "
you see the sources
- path - local ,github, ssh ...

git::username@domain.com:storage.git"



-----------------review
module "mydevm" {


source = "../modules/ec2"
mytype = ${"module.myvpc.cidr}" # verify, this is just an example..
}

you can use output function to see the default value or look gotoutput function.




No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...