Day 35 - AWS CLI (Command Line Interface)
Table of contents
AWS CLI
AWS CLI (Command Line Interface) is a unified tool that allows users to interact with and manage AWS services directly from the command line.
The objective is to use the AWS CLI to move certain files from an EC2 instance to an S3 bucket.(Everything will be done in IAM User-Rohit)
- We are In IAM user(Rohit) search EC2 service.
We create one EC2 instance --> Launch instance
Name --> demo-iam-s3
AMI --> Ubuntu
Instance type --> t2.micro
create key pair --> demo-iam-s3
Click on Launch instance
- Select "demo-iam-s3" and connect it through SSH client.
- aws --> "sudo apt install awscli" will install version 1.22 that is an old version and we need new version 2.
We need to search "install aws cli v2" in Browser --> select Linux
aws
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update
aws --version
--> aws-cli/2.15.11 Python/3.11.6 Linux/6.2.0-1017-aws exe/x86_64.ubuntu.22 prompt/off
aws s3 ls
--> It is a way to check AWS CLi is connected or not with AWS console/account.aws configure
--> If we want to access S3 Buckets through IAM User so for that we need to give Credentials to the AWS CLI.AWS Access Key ID [None]:
How to get AWS Acess Key ID:
Go to IAM User(Rohit) --> Users --> Rohit --> Security credentials --> Access keys --> Create access key --> Select Command Line Interface(CLI) --> Check-mark - I understand the above recommendation and want to proceed to create an access key --> Click on Next --> Description tag value - Rohit-access-key-id --> Create access key.
AWS Access Key ID [None]: AKIAUEA6RAZIE7YDM4W2
AWS Secret Access Key [None]:
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
Default region name [None]: Click on Enter
Default output format [None]: Click on Enter --> aws cli is configured now
aws s3 ls
--> It will list down all S3 Buckets using aws cli of IAM user(Rohit)
Amazon s3 --> Buckets --> aws-static-demo1234 --> Properties --> Static website hosting --> http://aws-static-demo1234.s3-website.ap-south-1.amazonaws.com
Click on --> http://aws-static-demo1234.s3-website.ap-south-1.amazonaws.comEndFragment
If we want to change this content "Welcome To My Static Website Using S3 Buckets".
aws s3 ls aws-static-demo1234
--> It will list all s3 buckets of aws-static-demo1234aws s3 cp s3://aws-static-demo1234/index.html .
--> Want to copy the index.html file into my local server. (Download)ls
vim index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<titl>AWS Zero To Hero</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f5f5f5;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
}
.button {
display: inline-block;
padding: 10px 20px;
font-size: 16px;
text-align: center;
text-decoration: none;
outline: none;
cursor: pointer;
border: 2px solid #3498db;
color: #3498db;
background-color: transparent;
border-radius: 25px;
transition: background-color 0.3s, color 0.3s;
margin: 10px;
}
.button:hover {
background-color: #3498db;
color: #ffffff;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome To AWS: Zero To Hero</h1>
<a href="#" class="button">Button 1</a>
<a href="#" class="button">Button 2</a>
<a href="#" class="button">Button 3</a>
</div>
</body>
Changes done
Esc:wq Enter
If we refresh the page of static website still changes not reflect to my S3 bucket. So need to upload our local server code to S3 Bucket.
aws s3 cp index.html s3://aws-static-demo1234/
--> Upload my edited index.html file to AWS s3 Bucket.(Upload)
- Now If we refresh http://aws-static-demo1234.s3-website.ap-south-1.amazonaws.com this page changes are reflected successfully.
aws s3 help
--> Provides help documentation and information for using AWS CLI commands related to Amazon S3.
Want to download all files("index.html" & "error.html") from s3 Bucket to my local server:
mkdir aws-static-demo
cd aws-static-demo
aws s3 sync s3://aws-static-demo1234/ .
ls
Want to sync/upload from local server to s3 Bucket:
vim hello.txt
Hello Dosto
aws s3 sync . s3://aws-static-demo1234
- click on hello.txt --> click on Open
Subscribe to my newsletter
Read articles from Mayur Barange directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mayur Barange
Mayur Barange
As a DevOps maestro, I orchestrate the seamless synergy between development and operations, crafting a symphony of efficiency in the digital realm. Armed with a toolkit that includes Jenkins, Git, and Docker, I navigate the continuous integration and deployment landscape with finesse. From code inception to its triumphant deployment on production servers, I am the architect of a well-automated and smoothly-scaled software lifecycle. With a passion for containerization and configuration management, I transform challenges into opportunities, ensuring that every line of code finds its perfect performance note. In the world of DevOps, I am the conductor, harmonizing innovation and reliability to create a technological masterpiece. ๐โจ