Skip to main content

Posts

Showing posts from April, 2022

Jenkins Terraform Integration | How do you integrate Terraform with Jenkins | Automate Infrastructure setup using Terraform and Jenkins Pipeline

  Pre-requistes: Jenkins  is up and running Terraform  is installed in Jenkins Terraform files already created in your SCM Make sure you have necessary IAM role created with right policy and attached to Jenkins EC2 instance. see below for the steps to create IAM role. I have provided  my public repo  as an example which you can use. Create IAM role to provision EC2 instance in AWS  Select AWS service, EC2, Click on Next Permissions Type EC2 and choose AmazonEC2FullAccess as policy Click on Next tags, Next Review give some role name and click on Create role. Assign IAM role to EC2 instance Go back to Jenkins EC2 instance, click on EC2 instance, Security, Modify IAM role Type your IAM role name  my-ec2-terraform-role  and Save to attach that role to EC2 instance. Create a new Jenkins Pipeline Give a name to the pipeline you are creating. Add parameters to the pipeline Click checkbox - This project is parameterized, choose Choice Parameter Enter name...

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}}"            },   ...

TIME-STAMP

NOTE: whenver we start provisioner we have to define ami_name always other it will give error  example we have already defined to install git . later i need to install tree also once i define my command and  try to buid it it give give error ami_name exist  so the solution is timestamp that we need to define in  ami_name: "dev-team-{{timestamp}}" {        "builders": [            {            "type": "amazon-ebs",            "access_key": "AVUGQ",            "secret_key": "KfVkt1aurid",            "region": "us-east-1",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moonu-amiii-...

ANSIBLE LOCAL-PROVISIONER

NOTE: INSTALL ANSIBLE IN LOCAL MACHINE   now to n.virginia region  go to ami launch image  go to ec2 dashboard  launch instance  all traffic anywhere use public ip of that with 8080 and see website' {        "builders": [            {            "type": "amazon-ebs",            "access_key": "AKQ",            "secret_key": "K1aurid",            "region": "us-east-1",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moonu-amiii"            }        ],        "provisioners": [        ...

ANSIBLE REMOTE-PROVISIONER

 NOTE:   974  mkdir sourcepath   975  cd sourcepath/   976  wget https://www.free-css.com/assets/files/free-css-templates/download/page276/transportz.zip   977  ll   978  unzip transportz.zip   979  ll   980  rm -rf transportz.zip   981  ll   982  cp -rvf transportz/* .   983  rm -rf transportz/ packer build aws.json now to n.virginia region  go to ami launch image  go to ec2 dashboard  launch instance  all traffic anywhere use public ip of that with 8080 and see website' vim   moo.json {        "builders": [            {            "type": "amazon-ebs",            "access_key": "AGQ",            "secret_key": "K1aurid",            "region": "us-east-1",         ...

SHELL PROVISIONER

we need to create one dir  mkdir cloud wget url (sample website) ---------------------------------- unzip moon.zip rm -rf moon.zip cp -rvf moon/* . rm -rf moon  VIM JJ.JSON {        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "access_key": "AKQ",            "secret_key": "Kfaurid",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moon-amiiii"            }        ],        "provisioners": [                {     ...

File Provisioner

 Note: file provisioner is used to copy file we have created one file vim clod now how to checck its been copied  go to ami and launch image and create instance  vi pro.json {        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "access_key": "AUGQ",            "secret_key": "Kurid",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moon-amiii"            }        ],        "provisioners": [                {   ...

Pass Variables Via Cli

 Note: 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",            "ssh_username": "ec2-user",            "ami_name": "moon-amiii"            }        ] }

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", ...

terraform Role Concept

 Note: go to n.varginia region create roles ec2 admin access write name packer create ======================================= now go to Mumbai region where instance is running  actions security modify ami select packer  save  we don't need to define access or secret key vim moon.json {        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moon-amiii"            }        ] }

Terraform packer calling variables

  note:  To run command  packer build -var-file= var.json abc.json another way to run without variable packer build moon.json vim moon.json {     "variables": {       "aws_access_key": "",       "aws_secret_key": ""     },       "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",            "ssh_username": "ec2-user",            "ami_name": "moon-amiii"            }   ...

Create AMI using packer

 basic parameter vim moon.json  {        "builders": [            {            "type": "amazon-ebs",            "access_key": "AGQ",            "secret_key": "Kft1aurid",            "region": "us-east-1",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moon-amiii"         }        ] } convert json file into hcl  packer hcl2 upgrade moon.json validate code  packer validate moon.json   packer validate moon.json.pkr.hcl