|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | 3 | read -p "Enter Docker image to pull (e.g., nginx:latest): " DOCKER_IMAGE |
4 | | -read -p "Enter the name of your EC2 key pair (do not include .pem): " KEY_NAME |
5 | 4 |
|
6 | 5 | AMI_ID="ami-0e58b56aa4d64231b" #AMI could possibly need to be updated in the future |
7 | 6 | INSTANCE_TYPE="t2.micro" |
8 | 7 | SECURITY_GROUP_NAME="ec2-docker-sg" |
9 | 8 | INSTANCE_PROFILE_NAME="EC2InstanceECRAccessProfile" |
10 | 9 | REGION="us-east-1" |
11 | 10 | VOLUME_SIZE=20 |
12 | | -KEY_FILE="${KEY_NAME}.pem" |
13 | 11 |
|
14 | 12 | AFTER_SECOND_SLASH=$(echo "$DOCKER_IMAGE" | cut -d'/' -f3-) |
15 | 13 | REPO_AND_TAG="${AFTER_SECOND_SLASH##*/}" |
|
174 | 172 | echo "Waiting for IAM instance profile propagation..." |
175 | 173 | sleep 5 |
176 | 174 |
|
177 | | -echo "Creating key pair..." |
178 | | - |
179 | | -KEY_OUTPUT=$(aws ec2 create-key-pair \ |
180 | | - --key-name "$KEY_NAME" \ |
181 | | - --query 'KeyMaterial' \ |
182 | | - --output text \ |
183 | | - --region "$REGION" 2>&1) |
184 | | - |
185 | | -if [[ $? -ne 0 ]]; then |
186 | | - if [[ "$KEY_OUTPUT" == *"InvalidKeyPair.Duplicate" ]]; then |
187 | | - echo "Key pair '$KEYNAME' already created." |
188 | | - exit 1 |
189 | | - fi |
190 | | -else |
191 | | - echo "$KEY_OUTPUT" > "$KEY_FILE" |
192 | | - chmod 400 "$KEY_FILE" |
193 | | - echo "Key pair created and saved to $KEY_FILE" |
194 | | -fi |
195 | | - |
196 | | -echo "Waiting for key propagation..." |
197 | | -sleep 5 |
198 | | - |
199 | 175 | USER_SCRIPT=$(cat <<EOF |
200 | 176 | #!/bin/bash |
201 | 177 | set -e |
@@ -251,5 +227,4 @@ aws ec2 wait instance-running --instance-ids "$INSTANCE_ID" |
251 | 227 | PUBLIC_IP=$(aws ec2 describe-instances --instance-ids "$INSTANCE_ID" \ |
252 | 228 | --query "Reservations[0].Instances[0].PublicIpAddress" --output text) |
253 | 229 |
|
254 | | -echo "If you wish to login a pem key has been put in your working directory. The instance public IP is: $PUBLIC_IP" |
255 | 230 | echo "It can take up to 15 minutes for an image to appear in your private repository." |
0 commit comments