AWS CLOUD INFRASTRUCTURE USING AWS CLI
Nishant Singh
Software Engineer@HCL Tech | Red Hat Certified System Administrator | AWS Certified Solution Architect-Associate | AWS Certified Developer Associate | AWS Cloud Practitioner Certified
What is AWS CLI?
AWS CLI is a tool that pulls all the AWS services together in one central console, giving you easy control of multiple AWS services with a single tool. The acronym stands for Amazon Web Services Command Line Interface because, as its name suggests, users operate it from the command line. With it, you can control services manually or automate them with powerful scripts.
Task Description:
? Webserver configured on EC2 Instance
?Document Root(/var/www/html) made persistent by mounting on EBS Block Device.
?Static objects used in code such as pictures stored in S3
?Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.
?Finally place the Cloud Front URL on the webapp code for security and low latency.
All the above steps must be performed using AWS CLI
Solution:
First of all we have to install the aws command in our windows.
For download awscli use this link (only for windows) but you can download this from internet for other operating systems.
Link: https://awscli.amazonaws.com/AWSCLIV2.msi
After installing this you have to setup the path of it then type this command in cmd.
aws --version
This command give the output like this
To perform the above task assigned, we need to configure the IAM - User profile through aws configure command.
Step1: Launch the aws instance
Now we have to launch the instance which uses the key-pair (for remote login) and security group which we created previously. You have to type this command.
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --security-group-ids sg-09ac883b6559900ab --key-name mytask2key
--subnet-id subnet-06b4d980faf717e6f
First of all you have to type aws help command this command shows all the service names. You have to know that instance running sub-service comes under ec2 service so type aws ec2 help here you find many options. Now you find the option run-instances in it. Now type aws ec2 run-instances help. Here you can see the multiple options so Finally you find this --image-id, --instance-type, --count, --security-group-ids, --subnet-id and --key-name options after giving the information about these options your command completed.
Step2: Create an EBS volume
Now you have to create the volume from AWS CLI. Now type the command.
aws ec2 create-volume --availability-zone ap-south-1a --size 1
--volume-type gp2
First of all you have to type aws help command this command shows all the service names. You have to know that volume creation comes under ec2 service so type aws ec2 help here you find many options. Now you find the option create-volume in it. Now type aws ec2 create-volume help. Finally you find this --availability-zone, --size and --volume-type option giving the information about these options your command completed.
Step3: Attach the volume to the instance which we created above
Now you have to attach the volume from AWS CLI. Now type the command
aws ec2 attach-volume --instance-id i-0e34cb8c23a3adce1
--volume-id vol-016288c9e626d5974 --device /dev/sdh
First of all you have to type aws help command this command shows all the service names. You have to know that volume attach comes under ec2 service so type aws ec2 help here you find many options. Now you find the option attach-volume in it. Now type aws ec2 attach-volume help. Finally you find this --instance-id, --volume-id and --device option giving the information about these options your command completed.
Step4: Creating the partition
Now we have to create the partition of the EBS block storage for using this storage.
ssh -i mytask2key.pem -l ec2-user 15.206.125.225 sudo fdisk /dev/xvdh
Format the partition:
Now we have to format the partition for saving the data.
ssh -i mytask2key.pem -l ec2-user 15.206.125.225 sudo mkfs.ext4 /dev/xvdh
Now install the httpd software and start the httpd service.
ssh -i mytask2key.pem -l ec2-user 15.206.125.225 sudo yum install httpd -y
Start the httpd service so we can mount the /var/www/html with the EBS block storage which behave like a pen drive.
ssh -i mytask2key.pem -l ec2-user 15.206.125.225 sudo systemctl start httpd ssh -i mytask2key.pem -l ec2-user 15.206.125.225 sudo systemctl status httpd
Now we have to mount /var/www/html with the EBS block
ssh -i mytask2key.pem -l ec2-user 15.206.125.225 sudo mount /dev/xvdh /var/www/html
Step5: Create the S3 bucket and store the static objects.
aws s3api create-bucket --bucket mybuckettask290 --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1
First of all you have to type aws help command this command shows all the service names. You have to know that s3 bucket creation comes under s3api service so type aws s3api help here you find many options. Now you find the option create-bucket in it. Now type aws s3api create-bucket help. Finally you find this --bucket, --region and --create-bucket-configuration option giving the information about these options your command completed.
Now we have to put the static object inside the s3 bucket.
aws s3api put-object --bucket mybuckettask290 --key image.jpg --body E:\Movie\Wallpapers\image.jpg
First of all you have to type aws help command this command shows all the service names. You have to know that object storing in s3 bucket comes under s3api service so type aws s3api help here you find many options. Now you find the option put-object in it. Now type aws s3api put-object help. Finally you find this --bucket, --key and --body option giving the information about these options your command completed.
Now we have to change the policies of bucket as well as object (For publicly access).
aws s3api put-bucket-acl --acl public-read --bucket mybuckettask290
First of all you have to type aws help command this command shows all the service names. You have to know that bucket policy comes under s3api service so type aws s3api help here you find many options. Now you find the option put-bucket-acl in it. Now type aws s3api put-bucket-acl help. Finally you find this --acl, --bucket option giving the information about these options your command completed.
Now we have to create the policy for object
aws s3api put-object-acl --bucket mybuckettask290 --key image.jpg --grant-read uri=https://acs.amazonaws.com/groups/global/AllUsers
First of all you have to type aws help command this command shows all the service names. You have to know that object policies comes under s3api service so type aws s3api help here you find many options. Now you find the option put-object-acl in it. Now type aws s3api put-object-acl help. Finally you find this --bucket, --key and --grant-read option giving the information about these options your command completed.
Now I create a code for webserver in AWS.
Now we check that everything is going good or not.
Step6: Create the CloudFront
aws cloudfront create-distribution --origin-domain-name mybuckettask290.s3.amazonaws.com --default-root-object image.jpg
First of all you have to type aws help command this command shows all the service names. You have to know that cloudfront distribution comes under cloudfront service so type aws cloudfornt help here you find many options. Now you find the option create-distribution in it. Now type aws cloudfornt create-distribution help. Finally you find this --origin-domain-name, --default-root-object option giving the information about these options your command completed.
After 10-15 minutes, you can see that distribution is deployed.
Now change the url in the webserver code and give the domain name of the cloudfront.
Final Output will be same but the difference between the content delivery network.
So I have successfully completed another task of AWS CSA & Developer Training .
THANK YOU ALL FOR VISITING MY ARTICLE!!!