-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Update create_ec2.sh #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,26 +1,25 @@ | ||||||
| #!/bin/bash | ||||||
|
|
||||||
| set -euo pipefail | ||||||
|
|
||||||
| check_awscli() { | ||||||
| if ! command -v aws &> /dev/null; then | ||||||
| echo "AWS CLI is not installed. Please install it first." >&2 | ||||||
| exit 1 | ||||||
| fi | ||||||
| if ! command -v aws &> /dev/null; then | ||||||
| echo "AWS CLI is not installed. Please install it first." >&2 | ||||||
| return 1 | ||||||
|
|
||||||
| fi | ||||||
| } | ||||||
|
|
||||||
| install_awscli() { | ||||||
| echo "Installing AWS CLI v2 on Linux..." | ||||||
| echo "Installing AWS CLI v2 on Linux..." | ||||||
|
|
||||||
| # Download and install AWS CLI v2 | ||||||
| # 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 | ||||||
| unzip -q awscliv2.zip | ||||||
| sudo ./aws/install | ||||||
|
|
||||||
| # Verify installation | ||||||
| #verify installation | ||||||
| aws --version | ||||||
|
|
||||||
| # Clean up | ||||||
| # clean up | ||||||
| rm -rf awscliv2.zip ./aws | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -30,62 +29,63 @@ wait_for_instance() { | |||||
|
|
||||||
| while true; do | ||||||
| state=$(aws ec2 describe-instances --instance-ids "$instance_id" --query 'Reservations[0].Instances[0].State.Name' --output text) | ||||||
| if [[ "$state" == "running" ]]; then | ||||||
| echo "Instance $instance_id is now running." | ||||||
| break | ||||||
| fi | ||||||
| sleep 10 | ||||||
| if [[ "$state" == "running" ]]; then | ||||||
| echo "Instance $instance_id is now running." | ||||||
| break | ||||||
| fi | ||||||
| sleep 10 | ||||||
| done | ||||||
|
Comment on lines
30
to
37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Add timeout and terminal-state detection to the polling loop. The 🔧 Proposed fix wait_for_instance() {
local instance_id="$1"
+ local max_attempts=30
+ local attempt=0
echo "Waiting for instance $instance_id to be in running state..."
- while true; do
+ while [[ $attempt -lt $max_attempts ]]; do
state=$(aws ec2 describe-instances --instance-ids "$instance_id" --query 'Reservations[0].Instances[0].State.Name' --output text)
if [[ "$state" == "running" ]]; then
echo "Instance $instance_id is now running."
break
fi
+ if [[ "$state" == "terminated" || "$state" == "shutting-down" || "$state" == "stopped" ]]; then
+ echo "Instance $instance_id entered terminal state: $state" >&2
+ exit 1
+ fi
sleep 10
+ ((attempt++))
done
+ if [[ $attempt -eq $max_attempts ]]; then
+ echo "Timeout waiting for instance $instance_id to reach running state" >&2
+ exit 1
+ fi
}🤖 Prompt for AI Agents |
||||||
| } | ||||||
|
|
||||||
| create_ec2_instance() { | ||||||
| local ami_id="$1" | ||||||
| local instance_type="$2" | ||||||
| local key_name="$3" | ||||||
| local subnet_id="$4" | ||||||
| local security_group_ids="$5" | ||||||
| local instance_name="$6" | ||||||
|
|
||||||
| # Run AWS CLI command to create EC2 instance | ||||||
| instance_id=$(aws ec2 run-instances \ | ||||||
| --image-id "$ami_id" \ | ||||||
| --instance-type "$instance_type" \ | ||||||
| --key-name "$key_name" \ | ||||||
| --subnet-id "$subnet_id" \ | ||||||
| --security-group-ids "$security_group_ids" \ | ||||||
| --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$instance_name}]" \ | ||||||
| --query 'Instances[0].InstanceId' \ | ||||||
| --output text | ||||||
| ) | ||||||
|
|
||||||
| if [[ -z "$instance_id" ]]; then | ||||||
| echo "Failed to create EC2 instance." >&2 | ||||||
| exit 1 | ||||||
| fi | ||||||
|
|
||||||
| echo "Instance $instance_id created successfully." | ||||||
|
|
||||||
| # Wait for the instance to be in running state | ||||||
| wait_for_instance "$instance_id" | ||||||
| create_ec2_instance(){ | ||||||
| local ami_id="$1" | ||||||
| local instance_type="$2" | ||||||
| local key_name="$3" | ||||||
| local subnet_id="$4" | ||||||
| local security_group_ids="$5" | ||||||
| local instance_name="$6" | ||||||
|
|
||||||
| #run AWS CLI command t create EC2 instance | ||||||
| instance_id=$(aws ec2 run-instances \ | ||||||
| --image-id "$ami_id" \ | ||||||
| --instance-type "$instance_type" \ | ||||||
| --key-name "$key_name" \ | ||||||
| --subnet-id "$subnet_id" \ | ||||||
| --security-group-ids "$security_group_ids" \ | ||||||
| --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$instance_name}]" \ | ||||||
| --query 'Instances[0].InstanceId' \ | ||||||
| --output text | ||||||
| ) | ||||||
| if [[ -z "$instance_id" ]]; then | ||||||
| echo "failed to create instance" | ||||||
| exit 1 | ||||||
| fi | ||||||
|
|
||||||
| echo "instance $instance_id created" | ||||||
|
|
||||||
| #wait for instance to be in running " | ||||||
| wait_for_instance "$instance_id" | ||||||
| } | ||||||
|
|
||||||
| main() { | ||||||
| check_awscli || install_awscli | ||||||
|
|
||||||
| echo "Creating EC2 instance..." | ||||||
|
|
||||||
| # Specify the parameters for creating the EC2 instance | ||||||
| AMI_ID="" | ||||||
| INSTANCE_TYPE="t2.micro" | ||||||
| KEY_NAME="" | ||||||
| SUBNET_ID="" | ||||||
| SECURITY_GROUP_IDS="" # Add your security group IDs separated by space | ||||||
| INSTANCE_NAME="Shell-Script-EC2-Demo" | ||||||
| if ! check_awscli ; then | ||||||
| install_awscli || exit 1 | ||||||
| fi | ||||||
| echo "creating EC2 instance ...." | ||||||
| # Specify the parameters for creating the EC2 instance | ||||||
| AMI_ID="ami-0b6d9d3d33ba97d99" | ||||||
| INSTANCE_TYPE="t2.micro" | ||||||
| KEY_NAME="AWSCLI_demo" | ||||||
| SUBNET_ID="subnet-01845bee6f9f54abb " | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win Remove trailing space in
🐛 Proposed fix- SUBNET_ID="subnet-01845bee6f9f54abb "
+ SUBNET_ID="subnet-01845bee6f9f54abb"📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| SECURITY_GROUP_IDS="sg-0ddba97c267e46e49" # Add your security group IDs separated by space | ||||||
| INSTANCE_NAME="Shell-Script-EC2-Demo" | ||||||
|
|
||||||
| #call the function to create ec2 | ||||||
| create_ec2_instance "$AMI_ID" "$INSTANCE_TYPE" "$KEY_NAME" "$SUBNET_ID" "$SECURITY_GROUP_IDS" "$INSTANCE_NAME" | ||||||
| echo " EC2 instance creation completed" | ||||||
| } | ||||||
|
|
||||||
| # Call the function to create the EC2 instance | ||||||
| create_ec2_instance "$AMI_ID" "$INSTANCE_TYPE" "$KEY_NAME" "$SUBNET_ID" "$SECURITY_GROUP_IDS" "$INSTANCE_NAME" | ||||||
| main"$@" | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win Fix script entry:
🐛 Proposed fix-main"$@"
+main "$@"📝 Committable suggestion
Suggested change
🧰 Tools🪛 Shellcheck (0.11.0)[error] 89-89: Argument mixes string and array. Use * or separate argument. (SC2145) 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||||||
|
|
||||||
| echo "EC2 instance creation completed." | ||||||
| } | ||||||
|
|
||||||
| main "$@" | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Add
--failflag tocurlto handle HTTP errors.Without
--fail,curl -sreturns exit code 0 on HTTP 404/500 and saves the error page asawscliv2.zip. The subsequentunzipthen fails with a confusing message instead of indicating the download failed.🔧 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents