(*Script)Create High Availability Architecture with AWS CLI Automated with script:-

Rohan Singh Shekhawat
8 min readOct 26, 2020

🔰Check out my Video Link:- https://www.linkedin.com/posts/rohan-singh-shekhawat-063619159_automated-task6-script-activity-6726476747828727808-Rx4T

🔰 1. I have Created the Key:-

aws ec2 create-key-pair --key-name rohan4 --query KeyMaterial > finalkey.pem --output text > rohan4.pem

✴️ I will tell my AWS that I want to use the ec2 service and in this service I want to create the key pair and the name of the key pair is “rohan4”

✴️ And Now ** — query KeyMaterial ** will help us to get the key from the output and for write this in the file we will use ** > filename** but the by default output format is json.

✴️ So in this format in the replace of new line it writes the *\n* in the key. so when we use this key we get an error of invalid key format.

✴️ So to fix this problem we change the output format to text and to change the format we use ** — output text**

✴️ Then I have set the variable name key and stored the key name in it so I can use it later

set key="rohan4.pem"

🔰 2. Create a security group:-

aws ec2 create-security-group --group-name rohan --description "aws cli task security group" --output text > security.text

✴️ I will tell my AWS that I want to use the ec2 service and in this service I want to create the security group and the name of the group is rohan and the description is aws cli task security group

✴️ After Running this Command it Created the Security Group and gave me the security group id in output and i have stored it in security.text

✴️ But Here We have 1 problem:-

inbound rules are not created in Security Group.

why?

because by default the upper command create the outbound rule only. it does not create the inbound rule and in this command they didnt provide us the option to add the inbound rule.

Then How to Add Inbound Rule?

I have Used the below command to add the inbound rules to the security group

aws ec2 authorize-security-group-ingress  --group-name rohan --cidr 0.0.0.0/0 --protocol all

✴️ I will tell my AWS that I want to use the ec2 service and in this service I want to add the ingress rules in security group and the security group to which I want to add ingress rules that security group name is rohan and the protocol at which I allow the traffic is all. I want to allow the traffic at all the ports and “ — cidr 0.0.0.0/0” means that I want to allow all the traffic.

✴️ Then wait for 2 seconds and set a variable and store the security group id in it with the help of security.text file

timeout 2
set /p variable1=<security.text

🔰 3. I Have Launched The Instance:-

✴️ I have launched the instance and here i have passed the variables to make it more dynamic

aws ec2 run-instances --image-id ami-03cfb5e1fb4fac428 --instance-type t2.micro --key-name rohan4 --subnet-id subnet-8e2124e6 --security-group-ids %variable1% --query Instances[].InstanceId --output text > instanceid.text

✴️ I will tell my AWS that I want to use the ec2 service and in this service I want to run the instance and the image id which I want to use is “ami-03cfb5e1fb4fac428” the instance type I want to use is t2.micro and add the security group whose id is stored in the %variable1% (which we have created previously) and the key pair which I want to use that keypair name is rohan4.

✴️ The output of this file will store in the instanceid.text.

✴️ Then Set the variable and store the instance id with the help of instanceid.text file in which we have stored the instance id. and then wait for 30 second.

✴️ So our instance can come in running state and we can perform ssh in it.

set /p variable=<instanceid.text
timeout 30

✴️ With this command I will find the Publicdnsname so i can do ssh and then i have stored it in Name.text

✴️ From the Name.text the PublicDnsName has been copy in the variable

aws ec2 describe-instances --query Reservations[-1].Instances[].[PublicDnsName] --output text > name.text

set /p name1=<name.text

🔰4. I Have Created The EBS Volume Of 1GB:-

✴️ I will tell my AWS that I want to use the ec2 service and in this service I want to Create the volume in the ap-south-1a availbility Zone and the size of the volume should be 1 GiB.

✴️ I will store the volumeid in the pop.text and then wait for 10 seconds

aws ec2 create-volume --availability-zone ap-south-1a --size 1 --query VolumeId --output text >pop.text

timeout 10

🔰5. I Have Attached The EBS Volume Of 1GB To The Instance:-

✴️ It is reading the volumeid from the pop.text file which has been created by the script in above command.

aws ec2 attach-volume --device /dev/sdb --instance-id %variable% --volume-id file://pop.text

✴️Here we have passed the variables for make it more dynamic

🔰6.INSTALLING THE SOFTWARE IN INSTANCE :-

ssh -i %key% ec2-user@%name1% sudo yum install httpd -y

✴️ Here the %name1% is the variable which stores the PublicDnsName of the instance and the %key% is the variable which stores the key name and here I am running the command to install the httpd software for the webserver

🔰7.CREATE PARTITION IN THE HARDDISK:

ssh -i %key% ec2-user@%name1% sudo fdisk /dev/xvdb

✴️ This command will run the sudo fdisk /dev/xvdb in the instance so we can make the partition

✴️ Now we have to select the size of the partition and then we have to save it thats it . this was the manual work only

✴️ Then automatically next command will run and that command will format the partition created

🔰8.FORMAT THE CREATED PARTITION AND MOUNT:

ssh -i %key% ec2-user@%name1% sudo mkfs.ext4 /dev/xvdb1

✴️ Now The Command For Mount This Partition To the /var/www/html is

ssh -i %key% ec2-user@%name1% sudo mount /dev/xvdb1 /var/www/html

🔰9.The Command to Create the Bucket:-

aws s3api create-bucket --bucket rohan1231231 --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1

✴️ This command will create the bucker with name rohan1231231 in ap-south-1 region

✴️ Now we will set our image name(which we want to upload in bucket) in a variable so we can use it later

set image=vimal_sir.jpeg

🔰10. Now We Will Upload The Image In The Bucket And We Will Give The Public Access To the Object:-

aws s3api put-object --acl public-read-write --bucket rohan1231231 --key %image% --body %image%

✴️ Again we will set another variable:-

✴️ In s3 variable we will store the url of the object so we can use this later

✴️ In the domain variable we will store the domain name of the bucket so we can use it in cloud front

set s3="https://rohan1231231.s3.ap-south-1.amazonaws.com/%image%"
set domain=rohan1231231.s3.ap-south-1.amazonaws.com

🔰11. Create The Cloud Front:-

✴️ Using the same bucket domain name which we have stored in the domain variable and then we have find the Cloudfront url and stored it in the Cloudfront.text file.

aws cloudfront create-distribution --origin-domain-name %domain% --query Distribution.DomainName --output text > cloudfront.text

✴️ And then timeout for 60seconds because creating the cloud front take sometime

✴️ And then we will store the Cloudfront Url in the Cloudfront variable with the help of Cloudfront.text.

timeout 60
set /p cloudfront=<cloudfront.text

✴️ Then We Have Stored the html code in the url variable but this html code is dynamic because here i have used the variables

set url="<body><center><img src=%cloudfront%/%image% alt="dont loaded"></center></body>"

🔰12. CONFIGURING WEBSERVER:-

✴️ I have Stored the code in url.html And Then I have sended this file from cmd to instance /home/ec2-user/ location with the help of scp command

echo %url% > url.html
scp -i %key% -r url.html ec2-user@%name1%:~

✴️ Then I have Removed The Inverted Commas from the file and write the updated html code in index.html

ssh -i %key% ec2-user@%name1% sudo sed 's/\"//g' url.html > index.html

✴️ Then I have copied that updated index.html file to /var/www/html/ location

ssh -i %key% ec2-user@%name1% sudo cp index.html /var/www/html/

🔰13. I have Started The Service Of httpd:-

ssh -i %key% ec2-user@%name1% sudo service httpd start

Thank You So Much To Read This Article:-

Github Url of the Script :- https://github.com/rsshekhawat0/task_6

--

--