Skip to main content

Posts

Showing posts from February, 2018

AWS Command line Tasks : Create EC2 instance and EC2 instance with 500GB root volume.

To create an EC2 instance from command line.

aws ec2 run-instances  --image-id ami-efe09bf8 --count 1 --instance-type m4.4xlarge --key-name mysshkey --security-group-ids sg-126adasd2--subnet-id subnet-148fd971 --block-device-mapping /dev/sda1=:500:false --region us-east-1

--image-id : The AMI ID which we are going to create the instance.
--count : The number of instances that we need to create.
--instance-type : The type of EC2 instance that we are going to use it.
--key-name : The pem that we are going to use to connect to this server.
--secrutiy group : The security group this instance is getting used.
--subnet-id : The subnet in which we want the ec2 instance.
--block device mapping - device:space in gb : to terminate volume [true or false.]

To create an EC2 instance with 500GB root volume.

aws ec2 run-instances --image-id ami-efe09bf8 --block-device-mappings  '[{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":500,"DeleteOnTermination&qu…

AWS command line tasks - Create 500 GB EBS Volume, to attach and detach the volume

To create a volume of 500 GB
aws ec2 --region us-east-1 create-volume --size 500 --availability-zone us-east-1d --volume-type gp2

size : in GB
availability zone : This needs to be in the same availability zone as that of the instance that we are going to attach the EBS volume.

To attach an EBS volume to the ec2 instance.

aws  ec2 attach-volume --volume-id vol-9dfjhsdkfj93 --instance-id i-1234567899oasd  --device /dev/xvdb --region us-east-1

After attaching format the drive and mount it like what we do for a normal hard disk drive.

To detach an EBS volume from the ec2 instance.

aws ec2 detach-volume --volume-id vol-9dfjhsdkfj93 --region us-east-1


AWS Command line Tasks - Create SSH key pair, security group, Allow connection to a port

We need a working aws cli configured to use the following commands.

To create an SSH pem file.

aws ec2 create-key-pair --region us-east-1 --key-name mysshkey --output text > mysshkey.pem 

region :  The region in which the ssh pem file is getting created.
Here we are saving the pem file in our linux machine as the name mysshkey.pem 


Modify the permission of the pem file.

chmod 400 mysshkey.pem 

To create a security group.

aws ec2 create-security-group --region us-east-1 --group-name work_project --description "Project_for_work" --vpc-id vpc-12345 

group name - It is the name that we are going to give for the security group
description - The description that we are going to give for the security group

To allow connection to a port

aws ec2 --region us-east-1 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --port 22 --cidr 10.100.0.0/16
aws ec2 --region us-east-1 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --port 22 --cidr 10.150.0.0…

Bash basics for personal reference.

To add double quotes in a line which has words seperated by comma.

sed -e 's/"//g' -e 's/[^,]*/"&"/g' -i filename

To add double quotes at end of line in a file.

sed -e 's/$/"/' -i filename

To add double quotes at begining of a line in a file.

sed -e 's/^/"/' -i filename

To replace using sed when there are too many "/" forward slash is there in the line or file to be replaced.

sed -i -e 's@/home/readonly/jas/@/home/readonly/jas/testfolder/@g' zeus_script_zdp_bos.sh

In bash scripting on remote server ssh login, we could experience problems with awk print $ and `. In such situations, use cut and there will be no such issues.

To check a log file between two dates.

awk '$0 >= "Sep 26 05:01:11" && $0 <= "Sep 26 08:37:05"' logfile

To pass two objects using multiple for loop from single file.

for i in `cat names | awk '{print $1}'`; do for j in `cat names | grep $i …