Friday, 13 April 2018

Deploy a single Web server with security group and user_data using Terraform

To Deploy a single web server that can respond to HTTP request .

Architecture Diagram

In this example I am using bash script that write the text message "Hello World" into index.html


user_data = <<-EOF
            #!/bin/bash
            echo "hello, world" > index.html
            nohup busybox httpd -f -p 8080 &
            EOF

I wrapped the busybox command with nohup and & so that the webserver run permanently in the background.

Note: In this example uses port 8080, rather than HTTP port 80 because root user privileges requires
If you listening on any port less than 1024 requires root user privileges.

You need to create the security group.

resource "aws_security_group" "instance" {
    name = "Terraform-example-1"

    ingress {
        from_port = 8080
        to_port   = 8080
        protocol  = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

Note: Aws does not allow any incoming or outgoing traffic from an ec2 instance.

Also need to tell the ec2 instance actually use it.To do that,you need to pass the ID of the security group into vpc_security_group_ids parameter of the aws_instance resource.

vpc_security_group_ids = ["${aws_security_group.instance.id}"]

Note: Terraform knows it needs to create the security group before the ec2 instance.

Source code of main.tf

variable "aws_access_key" {}
variable "aws_secret_key" {}

provider "aws" {
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    region = "us-east-1"
}

resource "aws_instance" "First" {
ami = "ami-40d28157"
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.instance.id}"]

user_data = <<-EOF
            #!/bin/bash
            echo "hello, world" > index.html
            nohup busybox httpd -f -p 8080 &
            EOF

tags {

   Name = "Terraform-example-1"

  }

}

resource "aws_security_group" "instance" {
    name = "Terraform-example-1"

    ingress {
        from_port = 8080
        to_port   = 8080
        protocol  = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

source code of  terraform.tfvars variable

aws_access_key = "xxxxxxxxxxxxxxxxxxxxx"
aws_secret_key = "xxxxxxxxxxxxxxxxxxxxx"


Run the apply command  > terraform apply -var-file=..\terraform.tfvars

Note:  Terraform to add a security group and replace the original ec2 instance with a new one that has the new user date( the -/+ means "replace")

Example output:

-/+ aws_instance.First (new resource required)
      id:                                   "i-0b6d39ea71d5fb1ed" => <computed> (forces new resource)
      ami:                                  "ami-40d28157" => "ami-40d28157"

Now EC2 instance launching




Use web browser or curl command to make an HTTP request

> curl http://<ec2-ip-address>:8080
Hello, World




Now web server running in AWS!

Note:

Even you get the dependency graph by running the terraform graph command:

> terraform graph

Output:

digraph {
        compound = "true"
        newrank = "true"
        subgraph "root" {
                "[root] aws_instance.First" [label = "aws_instance.First", shape = "box"]
                "[root] aws_security_group.instance" [label = "aws_security_group.instance", shape = "box"]
                "[root] provider.aws" [label = "provider.aws", shape = "diamond"]
                "[root] aws_instance.First" -> "[root] aws_security_group.instance"
                "[root] aws_security_group.instance" -> "[root] provider.aws"
                "[root] meta.count-boundary (count boundary fixup)" -> "[root] aws_instance.First"
                "[root] provider.aws (close)" -> "[root] aws_instance.First"
                "[root] provider.aws" -> "[root] var.aws_access_key"
                "[root] provider.aws" -> "[root] var.aws_secret_key"
                "[root] root" -> "[root] meta.count-boundary (count boundary fixup)"
                "[root] root" -> "[root] provider.aws (close)"
        }
}

The output is in a graph description language called DOT.

Note: you can turn into an image using Graphviz online tool and paste above output into text area.It generate the graphic picture.






Thursday, 12 April 2018

Python variable names and keywords

A variable is a name that refers to a value.

>> message = 'Hello'
>>> n = 18
>>> pi = 3.1415

A first assigns a string to a new variable  named message the second assigns the integer 17 to n
the third assigns the value of pi.

you can use the print statement to display value.

>>> print(n)
18
>>> print(message)
Hello

Total 3 variables called string, integer and float.

Note:  I showed some example of illegal character.please check it out below.

you cannot define the number in value

>>> 1 = a
SyntaxError: can't assign to literal

class is one of python keyword you cannot use

>>> 1 = a
SyntaxError: can't assign to literal

more@ is an illegal character you cannot use

>>> 1 = a
SyntaxError: can't assign to literal

Additional example

1.swap variable value

>>>a, b = 1, 2 >>>a, b = b, a

>>>print('a')
2
>>>print('b)
1

2.Concatenation -> combine strings and integer

>>> print ‘red’ + str(3)

red3




Basic python value and type

Value & Type

A value is one of the basic things a program works with like letter and number.

The value belongs to a different type: integer and string.

you can identify a string, integer, and float using below command.

>>> type('hello')
<class 'str'>

>>> type(3)
<class 'int'>

>>> type(3.0000)
<class 'float'>

Note: if you quotation mark its output will sting value even float value.

>>> type('3.2')
<class 'str'>

Large integer use comma between number we cannot expect value because it is a legal integer

>>> print(1,000,000)
1 0 0






Wednesday, 11 April 2018

How to share terraform code to other team members?

If you want to share your terraform code other team members also you need to track the history of all infrastructure changes.Please go-head with bitbucket or github

> git init
>git add main.tf
>git commit - m 'Inital commit"

you should also create a file called .gitignore that tells git to ignore certain types of files.

Add below value

.terraform          --> Ignore the folder
*.tfstate              -->  terraform uses to store state
*.tfstate.backup

First, you should commit the .gitinore file too

> git add .gitigore
>git commit -m "add add file"

Note: you will create the shared git repository also provide the permission of user.

Add endpoint orgin

>git remote add origin git@github.com:<your username>/<your repo name>.git

Push code to repo

>git push origin master


Note:  your team-members have made, the you can pull command.

> git pull orgin master.


Add tags to the AWS instance using terraform

If you notice that my previous exercise the ec2 doesn't have a name.so you can add tags to the aws instance using below syntax

tags {
   Name = "Terraform-example-1"
  }

I updated above syntax to my previous exercise. Let see ..

Run the plan command again


> terraform plan
aws_instance.First: Refreshing state... (ID: i-0701ccfb5da90)

Note: Terraform keep track of all resources it already created for this set of configuration file.Notice terraform says "Refreshing state" which means It going to append the changes to instance.

please refresh you ec2 console. you will see something similar to below image.



Thursday, 5 April 2018

Deploy a single server using terraform

We are going to use the AWS provider and deploy your infrastructure into the us-east-1 region.

Note: AWS region is a separate geographic area,such as us-east-1(North virginia),eu-west-1 (Ireland), and ap-souteast-2 (sydney).Within each region, there are mutiple isolated data centers know as availability zones, such as us-east-1a,us-east-1b and so on.

Create the new file called main.tf under the basic directory also copy below content into file.


variable "aws_access_key" {}
variable "aws_secret_key" {}

provider "aws" {
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    region = "us-east-1"
}

resource "aws_instance" "First" {
ami = "ami-40d28157"
instance_type = "t2.micro"
}

where Provider is name of a provider (eg.aws),resource to create in that provider, NAME is an identifier

General syntax for terraform resource

resource "PROVIDER_TYPE" "NAME" {
{config}
}

AMI --> you can find find free and paid AMI in the AWS marketplace

Amazon EC2 AMI Locator : https://cloud-images.ubuntu.com/locator/ec2/

INSTANCE_TYPE --> Type of ec2 instance to run

Also create the variable file called terraform.tfvars.

you need to set the aws credentials for the IAM user and passing variable via external file because you can share your code with other team members also you can ignore certain type of file like that.

aws_access_key = 'xxxxxxxxxxxxxxxx"
aws_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

Note: Don't maintaing the terraform file and variable file in same directory



plan command --> lets you see what terraform will do before actually making any changes
apply command --> create the instance
destroy command. --> delete environment


In a terminal, go into folder where you create main.tf  and run the plan command also pasted output of result

F:\Terraform\basic>terraform plan -var-file=..\terraform.tfvars
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_instance.First
      id:                           <computed>
      ami:                          "ami-40d28157"
      associate_public_ip_address:  <computed>
      availability_zone:            <computed>
      ebs_block_device.#:           <computed>
      ephemeral_block_device.#:     <computed>
      get_password_data:            "false"
      instance_state:               <computed>
      instance_type:                "t2.micro"
      ipv6_address_count:           <computed>
      ipv6_addresses.#:             <computed>
      key_name:                     <computed>
      network_interface.#:          <computed>
      network_interface_id:         <computed>
      password_data:                <computed>
      placement_group:              <computed>
      primary_network_interface_id: <computed>
      private_dns:                  <computed>
      private_ip:                   <computed>
      public_dns:                   <computed>
      public_ip:                    <computed>
      root_block_device.#:          <computed>
      security_groups.#:            <computed>
      source_dest_check:            "true"
      subnet_id:                    <computed>
      tenancy:                      <computed>
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note:  ( + ) ---> going to be created 
           ( - )  ---> going to be deleted
           ( ~)  ---> going to be modified 

To actually create the instance, run the terraform apply command.


F:\Terraform\basic>terraform apply -var-file=..\terraform.tfvars

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_instance.First
      id:                           <computed>
      ami:                          "ami-40d28157"
      associate_public_ip_address:  <computed>
      availability_zone:            <computed>
      ebs_block_device.#:           <computed>
      ephemeral_block_device.#:     <computed>
      get_password_data:            "false"
      instance_state:               <computed>
      instance_type:                "t2.micro"
      ipv6_address_count:           <computed>
      ipv6_addresses.#:             <computed>
      key_name:                     <computed>
      network_interface.#:          <computed>
      network_interface_id:         <computed>
      password_data:                <computed>
      placement_group:              <computed>
      primary_network_interface_id: <computed>
      private_dns:                  <computed>
      private_ip:                   <computed>
      public_dns:                   <computed>
      public_ip:                    <computed>
      root_block_device.#:          <computed>
      security_groups.#:            <computed>
      source_dest_check:            "true"
      subnet_id:                    <computed>
      tenancy:                      <computed>
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_instance.First: Creating...
  ami:                          "" => "ami-40d28157"
  associate_public_ip_address:  "" => "<computed>"
  availability_zone:            "" => "<computed>"
  ebs_block_device.#:           "" => "<computed>"
  ephemeral_block_device.#:     "" => "<computed>"
  get_password_data:            "" => "false"
  instance_state:               "" => "<computed>"
  instance_type:                "" => "t2.micro"
  ipv6_address_count:           "" => "<computed>"
  ipv6_addresses.#:             "" => "<computed>"
  key_name:                     "" => "<computed>"
  network_interface.#:          "" => "<computed>"
  network_interface_id:         "" => "<computed>"
  password_data:                "" => "<computed>"
  placement_group:              "" => "<computed>"
  primary_network_interface_id: "" => "<computed>"
  private_dns:                  "" => "<computed>"
  private_ip:                   "" => "<computed>"
  public_dns:                   "" => "<computed>"
  public_ip:                    "" => "<computed>"
  root_block_device.#:          "" => "<computed>"
  security_groups.#:            "" => "<computed>"
  source_dest_check:            "" => "true"
  subnet_id:                    "" => "<computed>"
  tenancy:                      "" => "<computed>"
  volume_tags.%:                "" => "<computed>"
  vpc_security_group_ids.#:     "" => "<computed>"
aws_instance.First: Still creating... (10s elapsed)
aws_instance.First: Still creating... (20s elapsed)
aws_instance.First: Still creating... (30s elapsed)
aws_instance.First: Creation complete after 36s (ID: i-0aaf724744d15489b)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.


To verify this head over to the ec2 console and you should see something similar below image.



Congrats you've just deployed a server.

once you completed your task and delete environment using destroy command.

F:\Terraform\basic>terraform destroy  -var-file=..\terraform.tfvars
aws_instance.First: Refreshing state... (ID: i-0aaf724744d15489b)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - aws_instance.First


Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy?
Yes
aws_instance.First: Destroying... (ID: i-0aaf724744d15489b)
aws_instance.First: Still destroying... (ID: i-0aaf724744d15489b, 10s elapsed)
aws_instance.First: Still destroying... (ID: i-0aaf724744d15489b, 20s elapsed)
aws_instance.First: Still destroying... (ID: i-0aaf724744d15489b, 30s elapsed)
aws_instance.First: Still destroying... (ID: i-0aaf724744d15489b, 40s elapsed)
aws_instance.First: Still destroying... (ID: i-0aaf724744d15489b, 50s elapsed)
aws_instance.First: Destruction complete after 1m0s

Destroy complete! Resources: 1 destroyed.
--------------------------------------------------------------------------------------------------------------------------


Terraform Basic Detail


--> Terraform code is written in the hashicrop configuration Language (HCL) in the files with the extension ".tf"
--> It also declarative language.

Terraform support variety of platforms
  •  AWS
  • Azure
  • Google cloud
  • Digital Ocean

Find the best editor with syntax highlighting support

  • Atom
  • Visual Studio code
  • sublime test
  • intellij


Creating Notifications for Project Managers

If you are a project manager or otherwise the official lead assigned to a project in JIRA, you want to keep track of the work that is being done in your project. This is not an easy task, even with notifications and filters and JQL and reports in JIRA. Email notifications are nice because you can receive an email when changes are made to issues in JIRA, depending on how your notification schemes are configured.
  • Go to project → select the project settings users --> notifications
  • If you like to  "created notifications" otherwise you can add it same notification schemes.


  • Go to the notification scheme
  • Find the "issue created" line
  • Add "Role: created notifications" to that line

Configuring time tracking

> JIRA's time tracking feature enables users to record the time they spend working on issues.
It looks like below screen shot for project issue creation.


Enabling time tracking
Choose     > Issues.
Select Issue Features > Time Tracking to open the Time Tracking page.
Click the 'Activate' button to turn time tracking ON.

Time tracking Permissions
Choose     >  Go to project administration then click the permissions 
 Check whether the row labeled 'Work On Issues' contains the appropriate users, groups or project roles who need to specify time estimates or log work. If it does not, click the 'Add' link in the 'Operations' column:



Note:-
 The 'Time Tracking' field is not added to the screen of your project. If you have Jira Administrator priviledges, just add the 'Time Tracking' field to the screen being configured for the project.


Rename a space in Confluence


Generate the XML backup and create a test instance
  1. On the production server, login as an administrator.
  2. Go to Space -> Space Tools -> Content Tools -> Export -> XML -> Next -> Full Export -> Export Space and export the target space as XMLincluding attachments.


Extract the export backup file
  1. Open the space backup file with a zip file editor and find the file entities.xml.
  2. Edit entities.xml in a text editor.
  3. Do a 'Search & Replace' with the old space name and replace with New Space Name.

    Example:
    <property name="destinationSpaceKey"><![CDATA[DDT]]></property>
    <property name="lowerDestinationSpaceKey"><![CDATA[ddt]]></property>
Note:  Repalce with upper and lower case in entities.xml file using  sed commend "sed  's/old/new/g' entities.xml"
Edit the exportDescriptor.properties file
  1. Also edit the exportDescriptor.properties file in a text editor
  2. Change the spaceKey value in the exportDescriptor.properties file to the new spaceKey.

    Compress the attachments,entities.xml  and exportDescriptor.properties with .xml.zip format.

     zip -r Confluence-space-export-095347-136.xml.zip attachments entities.xml exportDescriptor.properties

    Note :

    To transfer zip file to test instance under "/hboprod/itdept/hbowiki/data/restore" directory.
    Restore to test instance for verification
    1. Loginto the test instance as a Confluence administrator.
    2. Go to ->General Configuration -> Backup & Restore. Under 'Upload a zipped backup to Confluence', browse to select the modified space backup. Check the 'Build index' option and select 'Upload & Restore'.

      If  everything working as expected then restore to production
      Restore On Production
      1. Backup your production instance now.
      2. Loginto the production instance as a Confluence administrator.
      3. Go to ->General Configuration -> Backup & Restore.

      When you access a Space, the sidebar is blank and Space Tools is missing - (Do this in non PROD and then export the space from non-prod and import to prod)

      Resolution

      1. Verify if there is a Duplicate Entry in "AO_187CCC_SIDEBAR_LINK"
        select * from "AO_187CCC_SIDEBAR_LINK" where "CATEGORY" = 'ADVANCED' AND "SPACE_KEY"='{SPACEKEY}';
        select * from "AO_187CCC_SIDEBAR_LINK" where "CATEGORY" = 'MAIN' AND "SPACE_KEY"='{SPACEKEY}';
      2. Change the  SPACE_KEY from the old to new key. Commit. Restart confluence to reflect the change.

Configure EKS Persistent Static Storage With Elastic File System Using Terraform

  checkout :https://youtu.be/QuUIgQWBBZM