Tuesday, April 2, 2019

AWS:- Create a snapshot of a volume

AWS:- Create a snapshot of a volume

1. Login to Amazon Management Console
2. EC2 => Snapshots in the navigation pane
3. Choose Create Snapshot.
4. Select the volume you want to create a snapshot for.
5. Add tags to your snapshot if you like.
6. Choose Create Snapshot.


Using command line to create a snapshot

=> First go to the volume and get the volume IP of the colume you want to take snapshot.
Volume ID = vol-1234567890abcdef0

Command:
aws ec2 create-snapshot --volume-id vol-1234567890abcdef0 --description "This is my root volume snapshot"

Source: Amazon

Monday, April 1, 2019

AWS:- Ansible Installation and Deployment on AWS servers


Ansible Installation and Deployment on AWS servers

1. Launch 3 EC2 instances on Oregan region as follows.
EC2 => Launch Instance => 3 machines { Redhat or Amazon ssd }
=> Security group [ http anywhere ]
=> Create new key pair and download to your wondows download folder.
=> Review and launch.

a. Get the private assress of your VM
Hostname Private IP Public IP
ansible 172.31.31.175 54.70.239.139
webvm1 172.31.17.217 35.163.159.143
webvm2 172.31.17.71 52.12.168.96

2. Now, we have 3 machines are ready. Rename the servers as ansible, web1 and web2
   a. User MobaX to connect to your LinuxVM.
        Open three MobaX tab to connect to three servers
➤ /drives/e/Downloads
ansible->    ssh -i "mykeyfile.pem" ec2-user@ec2-54-70-239-139.us-west-2.compute.amazonaws.com
webvm1->  ssh -i "mykeyfile.pem" ec2-user@ec2-35-163-159-143.us-west-2.compute.amazonaws.com
webvm2->   ssh -i "mykeyfile.pem" ec2-user@ec2-52-12-168-96.us-west-2.compute.amazonaws.com

   b. Setup all machines to connect directly as root or set up as password less authentication for root user.
- First login as normal user
- su to root  $ sudo su -
# mv /root/.ssh/authorized_keys  /opt
# vi /etc/ssh/sshd_config
  38 PermitRootLogin yes
            65 PasswordAuthentication yes
- Restart sshd demon.
# systemctl restart sshd/service httpd restart
- Chnage root password
# passwd root
- Verify direct root login with new password
➤ ssh root@ec2-54-70-239-139.us-west-2.compute.amazonaws.com
➤ ssh root@ec2-35-163-159-143.us-west-2.compute.amazonaws.com
➤ ssh root@ec2-52-12-168-96.us-west-2.compute.amazonaws.com
# ssh -q webvm1/webvm2


3. Since we don't have dns server set up. Add server entry to hostfile on your ansible server.
# vi /etc/hosts
172.31.31.175 ansible
172.31.17.217 webvm1
172.31.17.71 webvm2
- Try to ping webvm1 and webvm2 from ansible server. It will fail
- Go to security Group of ansible server. Incoming -> add rule for icmp ipv4 -> anywhere
- Now, it should ping.

[root@ip-172-31-31-175 ~]# ping webvm1
64 bytes from webvm1 (172.31.17.217): icmp_seq=2 ttl=255 time=0.494 ms

[root@ip-172-31-31-175 ~]# ping webvm2
64 bytes from webvm2 (172.31.17.71): icmp_seq=1 ttl=255 time=0.795 ms

4. On ansible server, install ansible
- First setup/enable epel repo on RHEL7 by changing from 0 to 1
- epel.repo file is already there. If not follow the instruction below.
# cd /tmp; wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum install ./epel-release-latest-*.noarch.rpm
# vi /etc/yum.repos.d/epel.repo
[epel]
enabled=1
# yum update
# yum clean all; yum repolist
# yum  install ansible
- Now, ansible installation is completed.

5. Now, log out as ec3-user and log back in as a root user using MobaX.

6. Create/setup an inventory file [ This file is a core file of ansible]
- At the buttom of the page, ass this entry.
- You can categorize/ group server based on type, function
# vi /etc/ansible/hosts
[mywebvm]
webvm1 or IP
webvm2 or IP

7. Now, lets run some ansible commands
[root@ip-172-31-31-175 ~]# ansible mywebvm -m ping -u root -k
SSH password:
webvm2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
webvm1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

The command below lists all hardware related info of mywebvm servers.
[root@ip-172-31-31-175 ~]# ansible mywebvm -m setup -u root -k

8. Lets configure web server using ansible.

a. Install httpd service on all the servers [ all means all the servers, not on specific group]
   - Pay attention to group/all.
[root@ip-172-31-31-175 ~]# ansible all -u root -m yum -a "name=httpd state=latest" -k
\n\nComplete!\n"

  - Now start the service on all the machines now. [ I specified group name now]
[root@ip-172-31-31-175 ~]# ansible mywebvm -u root -m service -a "name=httpd state=restarted" -k
SSH password:
webvm2 | SUCCESS => {
    "changed": true,
    "name": "httpd",
    "state": "started"
}
webvm1 | SUCCESS => {
    "changed": true,
    "name": "httpd",
    "state": "started"
}

- Now get the public IP address of webvm1 and webvm2 and paste on your browser.
In our case, here is the info
webvm1 172.31.17.217 35.163.159.143
webvm2 172.31.17.71 52.12.168.96

- You should see default page.

- Lets go ahead and create/update our default page.
  - on your ansible server create an index page.
[root@ip-172-31-31-175 ~]# cat /root/index.html
<html>
<head>
  <title>Welcome to My AWS Page </title>
</head>
<body bgcolor=black>
        <font color=yellow><h1> <marquee> Samrat on the web </marquee></h1></font>
</body>
</html>
- Lets push this file to default doc root page.
[root@ip-172-31-31-175 ~]# ansible mywebvm -m copy -a 'src=/root/index.html dest=/var/www/html' -k
webvm1 | SUCCESS => {
webvm2 | SUCCESS => {

- Go to the browser and paste the public IP again and verify the update.
- Again change the index file and push the update. You will see the change.

To get help,
# ansible-doc -l


This way, you can deploy lots of config update to hundreds of servers.


Learm docket/kubernets, git/jenkins, ansible/puppet/chef,Vagrant/cloud

8. Shutdown your AWS VM using command line
Make sure you set up your PC/linux to use command line (aws-cmd)
INSTANCE-ID=i-016073db971619294

Stop EC2 Instnace
aws ec2 stop-instances --instance-id <INSTANCE-ID> --output json
aws ec2 wait instance-stopped --instance-ids <INSTNACE-ID>

Start EC2 Instnace
aws ec2 start-instances --instance-id <INSTNACE-ID> --output jso

Terminate EC2 Instances
aws ec2 terminate-instances --instance-ids <INSTNACE-ID> --output jso


=================xxxxx================

Wednesday, March 27, 2019

AWS:- How to Create and Login to AWS OS Instance


How to Create and Login to Aws OS Instance

1. Click on Services   =>> Click on EC2 under compute
2. Click on Launch Instance
3. Click on Free Tier Only on the left hand side which only displays the OS instances
4. Select the Amazon Machine Image (AMI) you like.
5. Under Instance Type, select the t2.micro free tier eligible one. [ Redhat ]
6. Click configure Instance details
7. Under Configure Instance Details, leave as it is a click on Add storage from bottom right corner.
8. Do not add any storage, click on Next: Add Tags
9. On Add Tags page, look at the middle of the page, you will see Click to add a Name tag
10. On this key value pair section just type value as lxvm1 and click next
11. ssh is already enabled click on Review and Launch.
 12. Click on Launch, you will be prompting to select/create key pair.
13. Select create anew key pair and type the name of the key and save the key to your desktop
14. Click on launch and wait for another 4 minutes, server will be ready.


Login to AWS OS instancce.

1. Once you create an instance, click on the check box before the instance name.
2. Click on connect
3. You will get a link to connect,
ssh -i "mynewvm.pem" ec2-user@ec2-35-167-153-89.us-west-2.compute.amazonaws.com

4. Go to google and searcg for MobaXterm_Portable. Download on your desktop and open the application.
5. Copy the downloaded key to your drive. I kept it under e:\downloads\awskeys

 ➤ cd e:/
 ➤ cd dowloads
 ➤ cd awskeys/
 ➤ ls
mynewvm.pem

6. Now, login to your Amazon OS instance. You wil be login without prompting for password.
 ➤ ssh -i "mynewvm.pem" ec2-user@ec2-35-167-153-89.us-west-2.compute.amazonaws.com

ec2-user@ip-172-31-25-98:~> sudo su -
ip-172-31-25-98:~ #


You will have 750 hrs free for new account, Once instance is created and lab part is completed, terminate it. Do not use delete, there will be charge on storage even instance is down.

To Terminate,
=> Go to Services  -==> EC2 Instances  ==> Select the instance you want to terminate.
==>  Go to Action  ==> Instance state  =>> Terminate.

It will warn you about termination, click on Yes Terminate.

You are good to go.

Friday, March 22, 2019

AWS: - Create S3 Bucket and upload file from PC


Create S3 Bucket and upload file from PC
1.    Login to AWS management console and click on Amazon S3.

2.    Click on Create Bucket
3.    Here, the name should be unique FNS-compliant name which must be unique across all existing bucket names in Amazon S3.

Fields are
Bucket Name: YYYYMMDDBucketname
Region: US East
Click next

4.    Under configure option tag your Bucket.


Here, you can click on create or Click next. I click next for more options.

5.    Click next and review the setting. If it looks good, click on create bucket

6.    Now, click on Bucket Name (do not click on check box)

7.    Click on upload to upload file to the bucket

8.    Click on Add files

9.    Select the file you want to upload.


10.                       File is successfully uploaded.
11.                       Now, Click on the file you just uploaded.
12.                       Click on Properties and review different options like versioning, server access logging, static website hosting, encryption and more. And click on Permission tab and go all the way bottom and click on Everyone. Everyone pops up and click on Read Object and click on save.

13.                       I got permission deny error.
14.                       Now go back to yor bucket and click on check box and click on Edit public access and remove all check boxes. Once its done go back to the promperies of the image and grant access to everyone.

15.                       Now, error is gone.  Go back to the bucket, and click on Image. On overview page you will see object URL, just copy it and paste on web browser, you should be able to see it.

16.                       If you want to delete the image, click on check box of the image, go to action and click on delete.


Cheers !!! You successfully created a S3 bucket and uploaded a file.



AWS:- Login to AWS


Login to AWS
1.    Go to aws.amazon.com
2.    Enter your account information
3.    You are logged in to AWS Management Console
4.    Click on Services on top right corner.

5.    Once you click on services, you will see many available services.

6.    By default, you can see the services based on category. If you like to change to list alphabetically, you can click on A-Z on right side corner.

And you will see like this,
7.    Click on different services to know and understand more.
There are lots of whitepapers, free trainings available @aws. Simply enroll and enjoy !!!


AWS:- Creating account on AWS


Creating account on AWS

1.    Go to https://aws.amazon.com/

2.    Click on Sign in to the console

3.    Click on Create a new AWS account

4.    Fill Out the basic info on the page presented. The basic account includes 12 months of free tier access (750 hrs).

5.    Click on continue. Select personal for your personal use.

6.    Fill out the information and click on Create account and continue. Payment information page displays. Fill credit/David card info and click on Secure Submit.

7.    Your account is created successfully.

Saturday, March 2, 2019

RHEL7 - Create a simple web page

The script will create simple web page.
[root@server2 ~]# cat createweb.sh
#!/bin/bash
#
#
# Specify the parameters
domain_name=$1
domain_root=/var/www/html/$domain_name
myip=192.168.10.122
port=80

if [ $# -eq 0 ]
then
        echo "Please enter your domain name followed by script as command line parameter"
        exit
fi

echo "<virtualhost $myip:$port>" > /etc/httpd/conf.d/$domain_name.conf
echo "servername $domain_name" >> /etc/httpd/conf.d/$domain_name.conf
echo "documentroot /var/www/html/$domain_name" >> /etc/httpd/conf.d/$domain_name.conf
echo "directoryindex index.html" >> /etc/httpd/conf.d/$domain_name.conf
echo "</virtualhost>" >> /etc/httpd/conf.d/$domain_name.conf
echo

#mkdir /var/www/html/$domain_name
if [ ! -d "$domain_root" ]
then
        mkdir ${domain_root}
fi

#echo "Welcome to $domain_name" >/var/www/html/$domain_name/index.html
echo "Welcome to $domain_name" > ${domain_root}/index.html

echo "Now, checking httpd systax"
httpd -t
if [ $? -eq 0 ]
then
        systemctl restart httpd
        echo "Your website $domain_name  is configured successfully"
        echo $myip $domain_name >> /etc/hosts
else
        echo "Please check the config file for error"
fi

# firewall-cmd --list-all | grep http
# if [ $? -ne 0 ]
# then
#
#       firewall-cmd --permanent --add-service=http
#       firewall-cmd --reload
#fi

# nothing needed for else statement

[root@server2 ~]#