Thursday, July 27, 2023

How to use variable in terraform ?

 

main.tf



provider "aws" {

    region = "us-east-1"

  

}









resource "aws_security_group" "allow_ssh" {

    name = "allow_ssh_oracle_mssql_port"

    description = "allow_ssh_oracle_mssql_port "


    ingress {

    description      = "ssh-port"

    from_port        = 22

    to_port          = 22

    protocol         = "tcp"

    cidr_blocks      = [var.my_ip]

  }



  ingress {

    description      = "Oracle-port "

    from_port        = 1521

    to_port          = 1521

    protocol         = "tcp"

    cidr_blocks      = [var.my_ip]

  }



  ingress {

    description      = "MSSQL-Port"

    from_port        = 1433

    to_port          = 1433

    protocol         = "tcp"

    cidr_blocks      = [var.my_ip]

  }


  egress {

    from_port        = 0

    to_port          = 0

    protocol         = "-1"

    cidr_blocks      = ["0.0.0.0/0"]

    #ipv6_cidr_blocks = [var.my_ip]

  }


  tags = {

    Name = "allow_imp_port_Oracle_MSSQL_ssh"

  }

  




variables.tf


variable "my_ip" {

  default = "34.207.6.67/32"

}






Terraform commands


terraform init

terraform plan

terraform apply





How to Print output from resources using terraform ?

test_output.tf 


provider "aws" {

    region = "us-east-1"

  

}




resource "aws_eip" "eip1" {

    domain = "vpc"

  

}



output "My_Public_ip" {

  

   value = aws_eip.eip1.public_ip

}






Terraform commands


terraform init

terraform plan

terraform apply





Tuesday, July 25, 2023

How to Create github repository using terraform ?

terraform {

  required_providers {

    github = {

      source  = "integrations/github"

      version = "~> 5.0"

    }

  }

}


# Configure the GitHub Provider

provider "github" {


    token = "Put-Github-Token"

}



resource "github_repository" "MyFirstRepo" {

  name = "terraform-module1"

  description = "This Module contain code for Start"

  visibility = "public"

}



Terraform commands


terraform init

terraform plan

terraform apply

How to Create / launch AWS EC2 instance using Terraform ?

MyFirst-EC2.tf



####Define credential to connect with providers


provider "aws" {

    region = "us-east-1"

    access_key = "Put-Your-Access_Key"

    secret_key = "Put-Your-Secret_Key"

  

}



#### Create Resource i.e. EC2 


resource "aws_instance" "MyEc2" {

 ami = "ami-05548f9cecf47b442"  

 instance_type = "t2.micro"


 

}





Terraform commands 


terraform init
terraform plan
terraform apply