AWS assesment files

This commit is contained in:
JustFiesta 2024-05-15 14:58:19 +02:00
parent cc8878a4f4
commit 5219b59e7f
3 changed files with 223 additions and 0 deletions

118
prepare_aws_enviroment.sh Normal file
View file

@ -0,0 +1,118 @@
#!/usr/bin/env bash
# -------------------
# This script sets up basic AWS environment for pushing docker images into the cloud
# It creates: VPC, Subnet, Elastic Container Registry (ECR), EC2 instance with a public IP, Security Groups
#
# Note: script needs preconfigured AWS CLI
# Global data
REGION="eu-west-1"
VPC_ID=""
SUBNET_ID=""
SECURITY_GROUP_ID=""
ECR_REPO_URI=""
INSTANCE_ID=""
# Get data from user - set it as env to be used in later scripts
read -p "Enter VPC name: " VPC_NAME && export VPC_NAME
read -p "Enter owner name: " OWNER && export OWNER
read -p "Enter project name: " PROJECT && export PROJECT
read -p "Enter ECR repository name: " ECR_NAME && export ECR_NAME
read -p "Enter EC2 instance name: " INSTANCE_NAME && export INSTANCE_NAME
read -p "Enter security group name: " SECURITY_GROUP_NAME && export SECURITY_GROUP_NAME
read -p "Enter key pair name: " KEY_PAIR_NAME && export KEY_PAIR_NAME
# Create VPC
echo "Creating VPC..."
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --region "$REGION" --query 'Vpc.VpcId' --output text)
if [ -z "$VPC_ID" ]; then
echo "Error during VPC creation."
exit 1
fi
echo "VPC with ID $VPC_ID has been created."
# Add tags to VPC
aws ec2 create-tags --resources "$VPC_ID" --tags Key=Name,Value="$VPC_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
echo "VPC is now correctly configured."
# Create Subnet
echo "Creating Subnet..."
SUBNET_ID=$(aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.0.0.0/24 --availability-zone "$REGION"a --query 'Subnet.SubnetId' --output text)
if [ -z "$SUBNET_ID" ]; then
echo "Error during Subnet creation."
exit 1
fi
echo "Subnet with ID $SUBNET_ID has been created."
# Add tags to Subnet
aws ec2 create-tags --resources "$SUBNET_ID" --tags Key=Name,Value="$VPC_NAME-Subnet" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
echo "Subnet is now correctly configured."
# Create Elastic Container Registry (ECR)
echo "Creating Elastic Container Registry (ECR)..."
ECR_REPO_URI=$(aws ecr create-repository --repository-name "$ECR_NAME" --region "$REGION" --query 'repository.repositoryUri' --output text)
export ECR_REPO_URI
if [ -z "$ECR_REPO_URI" ]; then
echo "Error during ECR creation."
exit 1
fi
echo "ECR repository created: $ECR_REPO_URI"
# Add tags to ECR repository
aws ecr create-repository --repository-name "$ECR_NAME" --tags Key=Name,Value="$ECR_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
echo "ECR repository is now correctly configured."
# Create Security Group
echo "Creating Security Group..."
SECURITY_GROUP_ID=$(aws ec2 create-security-group --group-name "$SECURITY_GROUP_NAME" --description "Security group for devOps internship assesment" --vpc-id "$VPC_ID" --region "$REGION" --output text)
if [ -z "$SECURITY_GROUP_ID" ]; then
echo "Error during Security Group creation."
exit 1
fi
echo "Security Group with ID $SECURITY_GROUP_ID has been created."
# Add tags to Security Group
aws ec2 create-tags --resources "$SECURITY_GROUP_ID" --tags Key=Name,Value="$SECURITY_GROUP_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
echo "Security Group is now correctly configured."
# Allow inbound SSH access (port 22) from anywhere
aws ec2 authorize-security-group-ingress --group-id "$SECURITY_GROUP_ID" --protocol tcp --port 22 --cidr 0.0.0.0/0 --region "$REGION"
echo "Inbound SSH access has been allowed for Security Group."
# Create EC2 instance
echo "Creating EC2 instance..."
# UserData script to install Docker and run it
USER_DATA_SCRIPT=$(cat <<EOF
#!/bin/bash
sudo yum update -y
sudo yum install -y docker
sudo service docker start
EOF
)
INSTANCE_ID=$(aws ec2 run-instances --image-id ami-0ac67a26390dc374d --count 1 --instance-type t3.micro --key-name "$KEY_PAIR_NAME" --security-group-ids "$SECURITY_GROUP_ID" --subnet-id "$SUBNET_ID" --region "$REGION" --user-data "$USER_DATA_SCRIPT" --query 'Instances[0].InstanceId' --output text)
export INSTANCE_ID
if [ -z "$INSTANCE_ID" ]; then
echo "Error during EC2 instance creation."
exit 1
fi
echo "EC2 instance with ID $INSTANCE_ID has been created."
# Add tags to EC2 instance
aws ec2 create-tags --resources "$INSTANCE_ID" --tags Key=Name,Value="$INSTANCE_NAME" Key=Owner,Value="$OWNER" Key=Project,Value="$PROJECT" --region "$REGION"
echo "EC2 instance is now correctly configured."
# Allocate and associate public IP address with EC2 instance
echo "Allocating and associating public IP address with EC2 instance..."
PUBLIC_IP=$(aws ec2 allocate-address --domain vpc --region "$REGION" --output text)
aws ec2 associate-address --instance-id "$INSTANCE_ID" --public-ip "$PUBLIC_IP" --region "$REGION"
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."

43
run_container_on_EC2.sh Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env bash
# -------------------
# This script sets authenthicates and pulls docker image. Then runs it on host
# Global data
REGION="eu-west-1"
IMAGE_NAME=""
# Get data from user
read -p "Enter the ECR image name: " IMAGE_NAME
# Get the public IP of EC2 instance from previus script
echo "Getting public IP address of EC2 instance..."
PUBLIC_IP=$(aws ec2 describe-instances --instance-ids "$INSTANCE_ID" --query 'Reservations[0].Instances[0].PublicIpAddress' --output text --region "$REGION")
if [ -z "$PUBLIC_IP" ]; then
echo "Error: Failed to get public IP address of EC2 instance."
exit 1
fi
echo "Public IP address of EC2 instance: $PUBLIC_IP"
# Authorize ECR in docker
echo "Getting authentication token for ECR..."
DOCKER_LOGIN_CMD=$(aws ecr get-login-password --region "$REGION" | docker login --username AWS --password-stdin "$ACCOUNT_ID".dkr.ecr."$REGION".amazonaws.com 2>&1)
if [[ $DOCKER_LOGIN_CMD == *"Login Succeeded"* ]]; then
echo "Authentication with ECR successful."
else
echo "Error: Failed to authenticate with ECR."
exit 1
fi
# SSH to EC2 and run instance
echo "SSH-ing to EC2 instance and running Docker image from ECR..."
ssh -i /path/to/your/private-key.pem ec2-user@"$PUBLIC_IP" <<EOF
docker pull "$IMAGE_NAME"
docker run -d -p 80:8080 "$IMAGE_NAME"
EOF
if [ $? -eq 1 ]; then
echo "Could not ssh and run docker image from ECR"
fi
echo "Docker image has been successfully deployed on EC2 instance."

62
send_image_to_aws.sh Normal file
View file

@ -0,0 +1,62 @@
#!/usr/bin/env bash
# -------------------
# This script sets builds and pushes image to previously made ECR
#
# Note: docker is reqiured
# Global data
AWS_REGION="eu-west-1"
AWS_ACCOUNT_ID=""
ECR_REPO_NAME="$ECR_NAME"
DOCKERFILE_DIR=""
# Get data from user
read -p "Enter your AWS account ID: " AWS_ACCOUNT_ID
read -p "Enter the path to Dockerfile directory: " DOCKERFILE_DIR
# Build docker image locally
echo "Building Docker image..."
if docker build -t spring-petclinic "$DOCKERFILE_DIR"; then
echo "Docker image built successfully."
else
echo "Error: Failed to build Docker image."
exit 1
fi
# Log in to ECR
echo "Logging in to Amazon ECR..."
DOCKER_LOGIN_CMD=$(aws ecr get-login-password --region "$AWS_REGION")
if [ $? -eq 0 ]; then
echo "Got credentials from AWS CLI."
else
echo "Error: Failed to get credentials from AWS CLI."
exit 1
fi
if echo "$DOCKER_LOGIN_CMD" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID".dkr.ecr."$AWS_REGION".amazonaws.com; then
echo "Logged in to ECR successfully."
else
echo "Error: Failed to log in to ECR."
exit 1
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
echo "Docker image tagged successfully."
else
echo "Error: Failed to tag Docker image."
exit 1
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
echo "Docker image pushed to ECR successfully."
else
echo "Error: Failed to push Docker image to ECR."
exit 1
fi
echo "Docker image has been successfully pushed to ECR."