File size: 2,612 Bytes
951d434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash

# Variables
CLUSTER_NAME="CS553"
TASK_DEFINITION_NAME="CSS553_CaseStudy_4"
CONTAINER_NAME="CaseStudy4Image"
IMAGE_URI="venkateshroshan/mlops-cs4:latest"
VCPU="0.5"
MEMORY="2048"
PORT="7860"
SECURITY_GROUP_NAME="CS553-SG"

echo "Starting ECS setup..."

# Step 1: Create ECS Cluster
echo "Creating ECS cluster: $CLUSTER_NAME..."
aws ecs create-cluster --cluster-name $CLUSTER_NAME
echo "ECS cluster $CLUSTER_NAME created successfully."

# Step 2: Register Task Definition
echo "Registering task definition: $TASK_DEFINITION_NAME..."
aws ecs register-task-definition \
    --family $TASK_DEFINITION_NAME \
    --network-mode awsvpc \
    --requires-compatibilities FARGATE \
    --cpu $VCPU \
    --memory $MEMORY \
    --container-definitions "[
        {
            \"name\": \"$CONTAINER_NAME\",
            \"image\": \"$IMAGE_URI\",
            \"portMappings\": [
                {
                    \"containerPort\": $PORT,
                    \"protocol\": \"tcp\"
                }
            ],
            \"essential\": true
        }
    ]"
echo "Task definition $TASK_DEFINITION_NAME registered successfully."

# Step 3: Get Default VPC ID
echo "Retrieving default VPC ID..."
VPC_ID=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query "Vpcs[0].VpcId" --output text)
if [ -z "$VPC_ID" ]; then
    echo "Error: Default VPC not found. Exiting."
    exit 1
fi
echo "Default VPC ID: $VPC_ID."

# Step 4: Create Security Group
echo "Creating security group: $SECURITY_GROUP_NAME..."
SECURITY_GROUP_ID=$(aws ec2 create-security-group --group-name $SECURITY_GROUP_NAME --description "Security group for CS553 task" --vpc-id $VPC_ID --query 'GroupId' --output text)
echo "Security group created with ID: $SECURITY_GROUP_ID."

# Step 5: Add Inbound Rule to Security Group
echo "Adding inbound rule to security group for port $PORT..."
aws ec2 authorize-security-group-ingress \
    --group-id $SECURITY_GROUP_ID \
    --protocol tcp \
    --port $PORT \
    --cidr 0.0.0.0/0
echo "Inbound rule added successfully."

# Step 6: Run ECS Task
echo "Running ECS task in cluster $CLUSTER_NAME..."
aws ecs run-task \
    --cluster $CLUSTER_NAME \
    --launch-type FARGATE \
    --network-configuration "awsvpcConfiguration={
        subnets=[\"$(aws ec2 describe-subnets --filters Name=vpc-id,Values=$VPC_ID --query 'Subnets[0].SubnetId' --output text)\"],
        securityGroups=[\"$SECURITY_GROUP_ID\"],
        assignPublicIp=\"ENABLED\"}" \
    --task-definition $TASK_DEFINITION_NAME
echo "Task is running in cluster $CLUSTER_NAME."

echo "ECS setup completed successfully."