-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-cluster.sh
executable file
·50 lines (46 loc) · 1.61 KB
/
create-cluster.sh
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
#!/bin/bash
#
# Creates an ECS Cluster
# check for all required variables
if [ "$KEY_NAME" == '' ]; then
echo "Please specify KEY_NAME and provide the filename (without the path and extension)"
exit 1
fi
if [ "$SECURITY_GROUP" == '' ]; then
echo "Please set a SECURITY_GROUP from your VPC (e.g. sg-12345678)"
exit 2
fi
if [ "$VPC_ID" == '' ]; then
echo "ECS requires using a VPC, so you must specify a VPC_ID"
exit 3
fi
if [ "$CLUSTER" == '' ]; then
echo "Please specify CLUSTER"
exit 4
fi
# Step 1 - Check all optional variables
if [ "$INSTANCE_TYPE" == '' ]; then
INSTANCE_TYPE=t3.small
fi
if [ "$INSTANCE_COUNT" == '' ]; then
INSTANCE_COUNT=1
fi
if [ "$SUBNET_ID" == '' ]; then
SUBNET_ID=$(aws ec2 describe-subnets --filters Name=vpc-id,Values=$VPC_ID --query 'Subnets[*].[SubnetId]' --output text | tr '\n' ',')
fi
# Step 2 - Create our ECS Cluster with INSTANCE_COUNT instances
echo "Detecting existing cluster/$CLUSTER"
CONTAINER_INSTANCE_COUNT=$(aws ecs describe-clusters --cluster $CLUSTER \
--query 'clusters[*].[registeredContainerInstancesCount]' --output text)
if [ "$CONTAINER_INSTANCE_COUNT" == $INSTANCE_COUNT ]; then
echo "Using existing cluster/$CLUSTER"
else
if [ "$CONTAINER_INSTANCE_COUNT" != '0' ]; then
echo "Instance count is $CONTAINER_INSTANCE_COUNT, but requested instance count is $INSTANCE_COUNT"
fi
echo "Creating cluster/$CLUSTER"
# to create
ecs-cli up --cluster $CLUSTER --size $INSTANCE_COUNT --capability-iam --instance-type $INSTANCE_TYPE --keypair $KEY_NAME \
--security-group $SECURITY_GROUP --vpc $VPC_ID --subnets $SUBNET_ID --force --verbose
fi
echo "Complete"