From 73519bc13494cf478d0f7918bf051e2b1ede964f Mon Sep 17 00:00:00 2001 From: JustFiesta Date: Wed, 15 May 2024 15:17:05 +0200 Subject: [PATCH] combine 3 scripts into one waterfall --- prepare_aws_enviroment.sh | 2 ++ run_container_on_EC2.sh | 5 +++-- send_image_to_aws.sh | 14 ++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/prepare_aws_enviroment.sh b/prepare_aws_enviroment.sh index 4faab3e58..63caf6791 100644 --- a/prepare_aws_enviroment.sh +++ b/prepare_aws_enviroment.sh @@ -112,3 +112,5 @@ aws ec2 associate-address --instance-id "$INSTANCE_ID" --public-ip "$PUBLIC_IP" echo "Public IP address has been allocated and associated with EC2 instance: $PUBLIC_IP" echo "EC2 instance, public IP address, and Security Group have been successfully created." + +./send_image_to_aws.sh diff --git a/run_container_on_EC2.sh b/run_container_on_EC2.sh index ebab42cd5..8139c958d 100644 --- a/run_container_on_EC2.sh +++ b/run_container_on_EC2.sh @@ -3,8 +3,9 @@ # This script sets authenthicates and pulls docker image. Then runs it on host # Global data -REGION="eu-west-1" -IMAGE_NAME="" +source send_image_to_aws.sh + +IMAGE_NAME="$ECR_NAME:latest" # Get data from user read -p "Enter the ECR image name: " IMAGE_NAME diff --git a/send_image_to_aws.sh b/send_image_to_aws.sh index f8370c4e7..9fbda7cc2 100644 --- a/send_image_to_aws.sh +++ b/send_image_to_aws.sh @@ -5,9 +5,9 @@ # Note: docker is reqiured # Global data -AWS_REGION="eu-west-1" +source prepare_aws_enviroment.sh + AWS_ACCOUNT_ID="" -ECR_REPO_NAME="$ECR_NAME" DOCKERFILE_DIR="" # Get data from user @@ -25,7 +25,7 @@ fi # Log in to ECR echo "Logging in to Amazon ECR..." -DOCKER_LOGIN_CMD=$(aws ecr get-login-password --region "$AWS_REGION") +DOCKER_LOGIN_CMD=$(aws ecr get-login-password --region "$REGION") if [ $? -eq 0 ]; then echo "Got credentials from AWS CLI." @@ -34,7 +34,7 @@ else exit 1 fi -if echo "$DOCKER_LOGIN_CMD" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID".dkr.ecr."$AWS_REGION".amazonaws.com; then +if echo "$DOCKER_LOGIN_CMD" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID".dkr.ecr."$REGION".amazonaws.com; then echo "Logged in to ECR successfully." else echo "Error: Failed to log in to ECR." @@ -43,7 +43,7 @@ fi # Tag the image echo "Tagging Docker image..." -if docker tag spring-petclinic:latest "$AWS_ACCOUNT_ID".dkr.ecr."$AWS_REGION".amazonaws.com/"$ECR_REPO_NAME":latest; then +if docker tag spring-petclinic:latest "$AWS_ACCOUNT_ID".dkr.ecr."$REGION".amazonaws.com/"$ECR_NAME":latest; then echo "Docker image tagged successfully." else echo "Error: Failed to tag Docker image." @@ -52,7 +52,7 @@ fi # Push image to ECR echo "Pushing Docker image to ECR..." -if docker push "$AWS_ACCOUNT_ID".dkr.ecr."$AWS_REGION".amazonaws.com/"$ECR_REPO_NAME":latest; then +if docker push "$AWS_ACCOUNT_ID".dkr.ecr."$REGION".amazonaws.com/"$ECR_NAME":latest; then echo "Docker image pushed to ECR successfully." else echo "Error: Failed to push Docker image to ECR." @@ -60,3 +60,5 @@ else fi echo "Docker image has been successfully pushed to ECR." + +./run_container_on_EC2.sh \ No newline at end of file