Skip to content

Conversation

pratik5826
Copy link

@pratik5826 pratik5826 commented Apr 24, 2025

  • Added validation to check if an EC2 instance with the same name already exists.
  • Script now exits with a message if the name 'Shell-Script-EC2-Demo' is already in use.
  • Helps prevent accidental duplication of EC2 instances and improves automation reliability.

Summary by CodeRabbit

  • New Features

    • Introduced a script to automate the creation of AWS EC2 instances, including automatic installation of AWS CLI if missing and informative progress messages.
  • Bug Fixes

    • Improved error handling for AWS CLI installation failures.
  • Enhancements

    • Added a safeguard to prevent creating EC2 instances with the reserved name "Shell-Script-EC2-Demo".
    • Updated default parameters for EC2 instance creation to use specific values.

Copy link

coderabbitai bot commented Apr 24, 2025

Walkthrough

A new Bash script, create_ec2.sh, was introduced to automate the creation of AWS EC2 instances using the AWS CLI. The script includes functions to check for and install the AWS CLI, create an EC2 instance with specified parameters, and wait for the instance to reach the running state. It also includes error handling and validation, such as preventing the creation of an instance with the reserved name "Shell-Script-EC2-Demo". Subsequent modifications improved error handling, updated default parameter values, and refined logic to prevent duplicate instance creation by name.

Changes

File(s) Change Summary
day04/create_ec2.sh Introduced a script to automate EC2 instance creation with AWS CLI, including functions for CLI checking/installation, instance creation, and waiting for instance readiness. Later updates improved error handling, set default parameters, and added a check to prevent duplicate instance creation by name.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Script
    participant AWS CLI
    participant AWS EC2

    User->>Script: Run create_ec2.sh
    Script->>Script: check_awscli()
    alt AWS CLI not installed
        Script->>Script: install_awscli()
        alt Installation fails
            Script-->>User: Exit with error
        end
    end
    Script->>Script: main() sets parameters
    Script->>Script: create_ec2_instance()
    Script->>Script: Validate instance name
    alt Name is "Shell-Script-EC2-Demo"
        Script-->>User: Print error and exit
    else
        Script->>AWS CLI: aws ec2 run-instances
        AWS CLI->>AWS EC2: Create instance
        AWS EC2-->>AWS CLI: Return instance ID
        Script->>Script: wait_for_instance(instance_id)
        Script->>AWS CLI: aws ec2 describe-instances (polling)
        AWS CLI->>AWS EC2: Check instance state
        AWS EC2-->>AWS CLI: Instance running
        Script-->>User: Print success message
    end
Loading

Poem

In the cloud where rabbits hop and play,
A script now builds EC2s each day.
It checks for tools, installs with care,
Avoids name clashes—no duplicate lair!
With every line and bashful cheer,
New instances in AWS appear.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (4)
create_ec2.sh (4)

4-9: Duplicate of the check_awscli return-based change in day04/create_ec2.sh; returning an error code instead of exiting directly is approved here as well.


79-81: Duplicate of the main installation logic change from day04/create_ec2.sh; handling of install_awscli in main is correct.


66-69: Duplicate of the instance name validation issue identified in day04/create_ec2.sh; please move this check before the run-instances command and use describe-instances to detect existing instances.


87-92: Duplicate of the hardcoded defaults feedback from day04/create_ec2.sh; consider parameterizing these values for better flexibility.

🧹 Nitpick comments (1)
day04/create_ec2.sh (1)

87-92: Hardcoded AWS resource IDs reduce flexibility
Embedding fixed AMI, key pair, subnet, security group IDs, and instance name limits reusability across environments. Consider accepting these as CLI flags or environment variables so users can override them without editing the script.

For example, use getopts:

while getopts "a:t:k:s:g:n:" opt; do
  case $opt in
    a) AMI_ID="$OPTARG" ;;
    t) INSTANCE_TYPE="$OPTARG" ;;
    k) KEY_NAME="$OPTARG" ;;
    s) SUBNET_ID="$OPTARG" ;;
    g) SECURITY_GROUP_IDS="$OPTARG" ;;
    n) INSTANCE_NAME="$OPTARG" ;;
    *) echo "Usage: $0 -a ami -t type -k key -s subnet -g sg1,sg2 -n name" >&2; exit 1 ;;
  esac
done
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 140e9d1 and 7e424e4.

📒 Files selected for processing (2)
  • create_ec2.sh (1 hunks)
  • day04/create_ec2.sh (2 hunks)
🔇 Additional comments (2)
day04/create_ec2.sh (2)

4-9: Return code handling in check_awscli is well separated
Switching from an immediate exit to returning an error code allows main to manage installation logic and keeps side effects localized.


79-81: Explicit AWS CLI installation in main
Good job moving the installation decision into main, which now cleanly handles failures from check_awscli and installs the AWS CLI only when needed.

@pratik5826
Copy link
Author

Add validation to prevent duplicate EC2 instance names:

  • Introduced a check to ensure an EC2 instance named 'Shell-Script-EC2-Demo' is not created if it already exists.
  • Improves automation reliability and prevents duplication.

PS : learning github and git, any mistake please let me know :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant