bezpieczenstwo-w-chmurze/skrypty/create-ec2-in-vpc.sh

62 lines
3.1 KiB
Bash
Raw Permalink Normal View History

2024-11-29 14:07:24 +01:00
#!/bin/bash
# Set variables
REGION="us-east-1"
VPC_NAME="uam-bwc-vpc1"
PRIVATE_SUBNET1_NAME="uam-bwc-private-subnet1"
PRIVATE_SUBNET2_NAME="uam-bwc-private-subnet2"
KEY_NAME="uam-bwc-key"
SECURITY_GROUP_NAME="uam-bwc-sg"
INSTANCE_TYPE="t2.micro"
AMI_ID="ami-005fc0f236362e99f"
# Get VPC ID
VPC_ID=$(aws ec2 describe-vpcs --filters "Name=tag:Name,Values=$VPC_NAME" --region $REGION --query 'Vpcs[0].VpcId' --output text)
echo "Found VPC ID: $VPC_ID for VPC Name: $VPC_NAME"
# Get Private Subnet IDs
PRIVATE_SUBNET1_ID=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=$PRIVATE_SUBNET1_NAME" --region $REGION --query 'Subnets[0].SubnetId' --output text)
echo "Found Private Subnet 1 ID: $PRIVATE_SUBNET1_ID for Subnet Name: $PRIVATE_SUBNET1_NAME"
PRIVATE_SUBNET2_ID=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=$PRIVATE_SUBNET2_NAME" --region $REGION --query 'Subnets[0].SubnetId' --output text)
echo "Found Private Subnet 2 ID: $PRIVATE_SUBNET2_ID for Subnet Name: $PRIVATE_SUBNET2_NAME"
# Create SSH Key Pair
aws ec2 create-key-pair --key-name $KEY_NAME --query 'KeyMaterial' --output text > ${KEY_NAME}.pem
chmod 400 ${KEY_NAME}.pem
echo "Created SSH Key Pair: $KEY_NAME"
# Create Security Group
SG_ID=$(aws ec2 create-security-group --group-name $SECURITY_GROUP_NAME --description "Security group for HTTP and SSH access" --vpc-id $VPC_ID --region $REGION --query 'GroupId' --output text)
echo "Created Security Group: $SG_ID"
# Allow inbound access on port 80 (HTTP) and port 22 (SSH)
aws ec2 authorize-security-group-ingress --group-id $SG_ID --protocol tcp --port 80 --cidr 0.0.0.0/0 --region $REGION
aws ec2 authorize-security-group-ingress --group-id $SG_ID --protocol tcp --port 22 --cidr 0.0.0.0/0 --region $REGION
echo "Configured Security Group to allow HTTP and SSH access"
# User Data script
USER_DATA=$(cat <<EOF
#!/bin/bash
apt-get update -y
apt-get install -y apache2
systemctl start apache2
2024-12-05 14:59:32 +01:00
2024-11-29 14:07:24 +01:00
systemctl enable apache2
echo "Hello from \$(uname -n)" > /var/www/html/index.html
2024-12-05 14:59:32 +01:00
echo "Page to secure" > /var/www/html/secure.html
echo "Login Page!" > /var/www/html/login.html
2024-11-29 14:07:24 +01:00
EOF
)
# Create EC2 instance in private subnet 1
INSTANCE1_ID=$(aws ec2 run-instances --image-id $AMI_ID --count 1 --instance-type $INSTANCE_TYPE --key-name $KEY_NAME --security-group-ids $SG_ID --subnet-id $PRIVATE_SUBNET1_ID --user-data "$USER_DATA" --region $REGION --query 'Instances[0].InstanceId' --output text)
aws ec2 create-tags --resources $INSTANCE1_ID --tags Key=Name,Value=uam-bwc-httpd1
echo "Created EC2 instance in private subnet 1: $INSTANCE1_ID with name uam-bwc-httpd1"
# Create EC2 instance in private subnet 2
INSTANCE2_ID=$(aws ec2 run-instances --image-id $AMI_ID --count 1 --instance-type $INSTANCE_TYPE --key-name $KEY_NAME --security-group-ids $SG_ID --subnet-id $PRIVATE_SUBNET2_ID --user-data "$USER_DATA" --region $REGION --query 'Instances[0].InstanceId' --output text)
aws ec2 create-tags --resources $INSTANCE2_ID --tags Key=Name,Value=uam-bwc-httpd2
echo "Created EC2 instance in private subnet 2: $INSTANCE2_ID with name uam-bwc-httpd2"
echo "EC2 instances setup complete"