Skip to main content

TERRAFORM MINI PROJECT VPC

 provider "aws" {

  region     = "us-east-1"

  access_key = "AKIARBKVUGQ"

  secret_key = "KfU0tFpao0b+B63xN99jTMdfVkt1aurid"

}


resource "aws_vpc" "cloud-vpc" {

  cidr_block       = "10.0.0.0/16"

  instance_tenancy = "default"


  tags = {

    Name = "cloud-vpc"

  }

}


#


resource "aws_subnet" "public-subnet" {

  vpc_id     = aws_vpc.cloud-vpc.id

  cidr_block = "10.0.1.0/24"


  tags = {

    Name = "public-subnet"

  }

}





resource "aws_subnet" "private-subnet" {

  vpc_id     = aws_vpc.cloud-vpc.id

  cidr_block = "10.0.2.0/24"


  tags = {

    Name = "private-subnet"

  }

}



resource "aws_security_group" "moon_security" {

  name        = "moon_security"

  description = "Allow TLS inbound traffic"

  vpc_id      = aws_vpc.cloud-vpc.id

  ingress {

      description      = "TLS from VPC"

      from_port        = 22

      to_port          = 22

      protocol         = "tcp"

      cidr_blocks      = ["0.0.0.0/0"]

    }


  egress {

      from_port        = 0

      to_port          = 0

      protocol         = "-1"

      cidr_blocks      = ["0.0.0.0/0"]

    }


  tags = {

    Name = "moon_security"

  }

}





resource "aws_internet_gateway" "cloud-igw" {

  vpc_id = aws_vpc.cloud-vpc.id


  tags = {

    Name = "cloud-igw"

  }

}





resource "aws_route_table" "public-rt" {

  vpc_id = aws_vpc.cloud-vpc.id


  route {

      cidr_block = "0.0.0.0/0"

      gateway_id = aws_internet_gateway.cloud-igw.id

    }




  tags = {

    Name = "public-rt"

  }

}







resource "aws_route_table_association" "route-ass" {

  subnet_id      = aws_subnet.public-subnet.id

  route_table_id = aws_route_table.public-rt.id

}






resource "aws_key_pair" "cloud-key" {

  key_name   = "cloud"

  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu8tss1HsG448fxpXK/m+MXaZLfeyxDrh5q/9kuJqA1d2QEehw99Jdfq3ZNs1NTVrlPH8DBdtk1U2+oG1tejWzviWGZ8ksmXZmv6RIoJYy/UBtz72fA8w9YMYpXBIYRMUtymtzGEAf95GZ2IOfTq2gPo6cGYXzd0isj4Ld9QJrtqS0aTp8XU2mMrhzKdQKBMDoCvpzUX1rH1K+00HKDn2S6iiuWv+8zJLnr1+H0mUFmJCT+udkKchpHIo/OUJwB5XviNsAdHq2kme/dEvrqRhhCgnHWq1afqbfTYnKuwwGmPQXlh97NQCgxOCB4wUTCennb6DlZ6ZZkZPXVI+qxfgZ root@ip-172-31-46-6.ap-south-1.compute.internal"

}



resource "aws_instance" "cloud-instance" {

  ami           = "ami-02e136e904f3da870"

  instance_type = "t2.micro"

  subnet_id     = aws_subnet.public-subnet.id

  vpc_security_group_ids = [aws_security_group.moon_security.id]

  key_name      = "cloud"

  tags = {

    Name = "HelloWorld"

  }

}





resource "aws_instance" "db-instance" {

  ami           = "ami-02e136e904f3da870"

  instance_type = "t2.micro"

  subnet_id     = aws_subnet.private-subnet.id

  vpc_security_group_ids = [aws_security_group.moon_security.id]

  key_name      = "cloud"

  tags = {

    Name = "database"

  }

}







resource "aws_eip" "public-ip" {

  instance = aws_instance.cloud-instance.id

  vpc      = true

}



resource "aws_eip" "cloud-natip" {

  vpc      = true

}






resource "aws_nat_gateway" "cloud-nat" {

  allocation_id = aws_eip.cloud-natip.id

  subnet_id     = aws_subnet.public-subnet.id



}



resource "aws_route_table" "private-rt" {

  vpc_id = aws_vpc.cloud-vpc.id


  route {

      cidr_block = "0.0.0.0/0"

      gateway_id = "aws_nat_gateway.cloud-nat"

    }




  tags = {

    Name = "cloud-nat"

  }

}




resource "aws_route_table_association" "nat-ass" {

  subnet_id      = aws_subnet.private-subnet.id

  route_table_id = aws_route_table.private-rt.id

}


Comments

Popular posts from this blog

ONLY & MULTIPLE PARAMTER IN PACKER

if we have to define 3 builders from different services azure, gcp,aws here we can  provisioner will run on all builders  if we want to run on specific builder then we can use only parameter  "only": ["prod-team","test-team"]  {        "builders": [            {            "name": "test-team",            "type": "amazon-ebs",            "access_key": "AUGQ",            "secret_key": "Kaurid",            "region": "us-east-1",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "test-team-{{timestamp}}"            },   ...

Packer Environment Variable

Note:  1011  export AWS_ACCESS=AKGQ                         do this on console not on editor  1012  export AWS_SECRET=KfVkt1aurid  1013  echo $AWS_ACCESS  1014  AKVUGQ   vim moon.json {     "variables": {       "aws_access_key": "{{env `AWS_ACCESS`}}",       "aws_secret_key": "{{env `AWS_SECRET`}}"     },        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "access_key": "{{user `aws_access_key`}}",            "secret_key": "{{user `aws_secret_key`}}",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870", ...

copying snap from one REGION to another

 while copying snap from one to another we need to change region provider "aws" {   region     = "us-west-1"   access_key = "AGQ"   secret_key = "Kurid" } resource "aws_ebs_snapshot_copy" "example_copy" {   source_snapshot_id = "snap-08bc5c27dad8e82b3"   source_region      = "us-east-1"   tags = {     Name = "HelloWorld_copy_snap"   } }