JENKINS : Work Project : Using AWS

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

ec2-instance.tf

resource "aws_instance" "sreejith_instance" {
  ami           = data.aws_ami.amz_linux2.id
  #instance_type = "t2.micro"
  instance_type = var.instance_type
  count         = var.instance_count
  # user_data = file("${path.module}/app1-install.sh")
  key_name = var.instance_keypair
  vpc_security_group_ids = [aws_security_group.vpc-ssh.id, aws_security_group.vpc-web.id]
  # count = 3
  tags = {
    Name  = element(var.instance_tags, count.index)
  }
}



 ami-datasource.tf

# While launching the EC2 - You ger the image id taking that image id search in
# AMI -- on the left - search in public images -- copy past the id you will get the Image id

# data.aws_ami.amz_linux2.id  

data "aws_ami" "amz_linux2" {
 # executable_users = ["self"]  // we are not using this
  most_recent      = true  // If more than one result is returned, use the most recent image.
 # name_regex       = "^myami-\\d{3}" // We arent using any regex expressions.
  owners           = ["amazon"]
  filter {
    name   = "name"
    # values = ["amzn2-ami-kernel-5.10-hvm-2.0.20220719.0-x86_64-gp2"] // Changes made below
    values = ["amzn2-ami-kernel-5.10-hvm-*-gp2"] # this mean get the latest AMI with the *
  }
  filter {
    name   = "root-device-type"
    values = ["ebs"]
  }
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
# We add another filter here to get the x86 64 bit VM. Architecture
  filter {
    name   = "architecture"
    values = ["x86_64"]
  }
}

 provider.tf

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "4.26.0"
    }
  }
}

provider "aws" {
   region = var.aws_region
   profile = "default"
}

securitygroup.tf

# Create Security Group - SSH Traffic
resource "aws_security_group" "vpc-ssh" {
  name        = "vpc-ssh"
  description = "Dev VPC SSH"
  ingress {
    description = "Allow Port 22"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    description = "Allow all ip and ports outbound"    
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "vpc-ssh"
  }
}

# Create Security Group - Web Traffic
resource "aws_security_group" "vpc-web" {
  name        = "vpc-web"
  description = "Dev VPC Web"
  ingress {
    description = "Allow Port 80"
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    description = "Allow Port 443"
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }  
  egress {
    description = "Allow all ip and ports outbound"    
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "vpc-web"
  }
}

 variables.tf

 

#AWS Region
variable "aws_region" {
    type = string
    default = "ap-south-1"
    description = "Desribing the region"
}

#AWS EC2 Instance Type
variable "instance_type" {
    type = string
    default = "t2.micro"
    description = "EC2 Instance Type"
}

# AWS Instance Key Pair
variable "instance_keypair" {
    type = string
    default = "new-key-pair"
    description = "EC2 Instance Key pair"
}

# AWS EC2 Instance Type - Map
variable "instance_tags" {
  type = list(string)
  default = ["Jenkins", "Ansible", "Host1"]
}

variable "instance_count" {
  default = "3"
}


 

 There was no need to give the "Auth" in Putty any more , Just direct logging is enough this time

ec2-user

 

 Installing Jenkins in Jenkins server 

https://www.jenkins.io/doc/tutorials/tutorial-for-installing-jenkins-on-AWS/

 

Downloading and installing Jenkins

 

Jenkins File :  cat /etc/default/jenkins/

 Amazon

/var/lib/jenkins

 


Jenkins URL :  http://15.207.99.237:8080/

 

 

 

 

 



Comments

Popular posts from this blog

Jenkins file | declarative pipeline scripting

Jenkins : Webhooks

JENKINS -- My Work -Project - DNS