Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions day05Yatin/create_s3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

set -euo pipefail

<< TASK
NAME: YATIN
TASK: CREATE S3 BUCKET USING SHELL SCRIPT
TASK
Comment on lines +5 to +8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Misuse of here-document for metadata comments
A heredoc (<< TASK … TASK) without a command will break the script (SC2188). It’s better to convert this block into shell comments or prepend a no-op.

Suggested diff:

- << TASK
- NAME: YATIN
- TASK: CREATE S3 BUCKET USING SHELL SCRIPT
- TASK
+ # NAME: YATIN
+ # TASK: CREATE S3 BUCKET USING SHELL SCRIPT
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<< TASK
NAME: YATIN
TASK: CREATE S3 BUCKET USING SHELL SCRIPT
TASK
# NAME: YATIN
# TASK: CREATE S3 BUCKET USING SHELL SCRIPT
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 5-5: This redirection doesn't have a command. Move to its command (or use 'true' as no-op).

(SC2188)


check_awscli(){
if ! command -v aws &> /dev/null; then
echo "AWS CLI not installed, Installing it"
install_awscli
else
echo "AWS CLI INSTALLED!"
fi
}

install_awscli(){
# Download and install AWS CLI v2
curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt-get install -y unzip &> /dev/null
unzip -q awscliv2.zip
sudo ./aws/install

# Verify installation
aws --version

# Clean up
rm -rf awscliv2.zip ./aws
}
Comment on lines +19 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Support cross-distro installation of unzip
Right now, you assume an APT-based distro. If the user is on RHEL/CentOS or another flavor, apt-get will fail. Detect the package manager and handle accordingly.

Proposed diff:

 install_awscli(){
@@
-     sudo apt-get install -y unzip &> /dev/null
+     if command -v apt-get &> /dev/null; then
+         sudo apt-get install -y unzip &> /dev/null
+     elif command -v yum &> /dev/null; then
+         sudo yum install -y unzip &> /dev/null
+     else
+         echo "Error: Unsupported package manager. Please install 'unzip' manually."
+         exit 1
+     fi
@@
     # Verify installation
     aws --version
@@
     # Clean up
     rm -rf awscliv2.zip ./aws
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
install_awscli(){
# Download and install AWS CLI v2
curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt-get install -y unzip &> /dev/null
unzip -q awscliv2.zip
sudo ./aws/install
# Verify installation
aws --version
# Clean up
rm -rf awscliv2.zip ./aws
}
install_awscli(){
# Download and install AWS CLI v2
curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
if command -v apt-get &> /dev/null; then
sudo apt-get install -y unzip &> /dev/null
elif command -v yum &> /dev/null; then
sudo yum install -y unzip &> /dev/null
else
echo "Error: Unsupported package manager. Please install 'unzip' manually."
exit 1
fi
unzip -q awscliv2.zip
sudo ./aws/install
# Verify installation
aws --version
# Clean up
rm -rf awscliv2.zip ./aws
}


create_s3_bucket(){
BUCKET_NAME=$1
REGION=$2
aws s3 mb s3://$BUCKET_NAME --region $REGION
echo -e "------------------\nS3 Bucket $BUCKET_NAME created Successfully!!\n-------------------------"
}

main(){
BUCKET_NAME="yatins-bucket"
REGION="us-east-1"

check_awscli
if aws s3api head-bucket --bucket "$BUCKET_NAME" &> /dev/null; then
echo -e "-----------------------------\nBucket $BUCKET_NAME already exists. Skipping creation.\n----------------------------"
else
echo "Creating Bucket in region $REGION"
create_s3_bucket $BUCKET_NAME $REGION
fi
}

main