Browse Source

Add auto scaling group, change infrastructure file name

Anuj Bansal 3 years ago
parent
commit
4a74778ea5

+ 2 - 0
devops/infrastructure/bash-config.sample.cfg

@@ -18,6 +18,8 @@ DATA_PATH="data-$NEW_STACK_NAME"
 
 INVENTORY_PATH="$DATA_PATH/inventory"
 
+NUMBER_OF_VALIDATORS=2
+
 #### PARAMETERS USED BY ANSIBLE
 
 LOCAL_CODE_PATH="~/Joystream/joystream"

+ 1 - 1
devops/infrastructure/delete-stack.sh

@@ -25,7 +25,7 @@ BUCKET_NAME=$(get_aws_export "S3BucketName")
 
 echo -e "\n\n=========== Emptying bucket $BUCKET_NAME ==========="
 
-aws s3 rm s3://$BUCKET_NAME --recursive --profile $CLI_PROFILE
+aws s3 rm s3://$BUCKET_NAME --recursive --profile $CLI_PROFILE || echo "No bucket"
 
 echo -e "\n\n=========== Deleting stack $NEW_STACK_NAME ==========="
 

+ 16 - 4
devops/infrastructure/deploy-infra.sh

@@ -35,20 +35,32 @@ aws cloudformation deploy \
   --region $REGION \
   --profile $CLI_PROFILE \
   --stack-name $NEW_STACK_NAME \
-  --template-file main.yml \
+  --template-file infrastructure.yml \
   --no-fail-on-empty-changeset \
   --capabilities CAPABILITY_NAMED_IAM \
   --parameter-overrides \
     EC2InstanceType=$EC2_INSTANCE_TYPE \
     KeyName=$AWS_KEY_PAIR_NAME \
-    EC2AMI=$EC2_AMI_ID
+    EC2AMI=$EC2_AMI_ID \
+    NumberOfValidators=$NUMBER_OF_VALIDATORS
 
 # If the deploy succeeded, get the IP, create inventory and configure the created instances
 if [ $? -eq 0 ]; then
   # Install additional Ansible roles from requirements
   ansible-galaxy install -r requirements.yml
 
-  VALIDATORS=$(get_aws_export "PublicIp")
+  ASG=$(get_aws_export "AutoScalingGroup")
+
+  VALIDATORS=""
+
+  INSTANCES=$(aws autoscaling describe-auto-scaling-instances --profile $CLI_PROFILE \
+    --query "AutoScalingInstances[?AutoScalingGroupName=='${ASG}'].InstanceId" --output text);
+
+  for ID in $INSTANCES
+  do
+    IP=$(aws ec2 describe-instances --instance-ids $ID --query "Reservations[].Instances[].PublicIpAddress" --profile $CLI_PROFILE --output text)
+    VALIDATORS+="$IP\n"
+  done
 
   RPC_NODES=$(get_aws_export "RPCPublicIp")
 
@@ -60,7 +72,7 @@ if [ $? -eq 0 ]; then
 
   mkdir -p $DATA_PATH
 
-  echo -e "[build]\n$BUILD_SERVER\n\n[validators]\n$VALIDATORS\n\n[rpc]\n$RPC_NODES" > $INVENTORY_PATH
+  echo -e "[build]\n$BUILD_SERVER\n\n[validators]\n$VALIDATORS\n[rpc]\n$RPC_NODES" > $INVENTORY_PATH
 
   if [ -z "$EC2_AMI_ID" ]
   then

+ 19 - 24
devops/infrastructure/main.yml → devops/infrastructure/infrastructure.yml

@@ -14,6 +14,10 @@ Parameters:
     Type: 'AWS::EC2::KeyPair::KeyName'
     Default: 'joystream-key'
     ConstraintDescription: must be the name of an existing EC2 KeyPair.
+  NumberOfValidators:
+    Description: Number of validator instances to launch
+    Type: Number
+    Default: 2
 
 Conditions:
   HasAMIId: !Not [!Equals [!Ref EC2AMI, ""]]
@@ -109,25 +113,22 @@ Resources:
 
             /opt/aws/bin/cfn-signal -e $? -r "Instance Created" '${WaitHandle}'
 
-  Instance:
-    Type: AWS::EC2::Instance
+  AutoScalingGroup:
+    Type: AWS::AutoScaling::AutoScalingGroup
     Properties:
+      MinSize: '0'
+      MaxSize: '10'
+      DesiredCapacity: !Ref NumberOfValidators
+      AvailabilityZones:
+        Fn::GetAZs:
+          Ref: "AWS::Region"
       LaunchTemplate:
         LaunchTemplateId: !Ref InstanceLaunchTemplate
         Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
       Tags:
         - Key: Name
-          Value: !Sub '${AWS::StackName}_1'
-
-  Instance2:
-    Type: AWS::EC2::Instance
-    Properties:
-      LaunchTemplate:
-        LaunchTemplateId: !Ref InstanceLaunchTemplate
-        Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
-      Tags:
-        - Key: Name
-          Value: !Sub '${AWS::StackName}_2'
+          Value: !Sub '${AWS::StackName}'
+          PropagateAtLaunch: "true"
 
   RPCInstance:
     Type: AWS::EC2::Instance
@@ -159,7 +160,7 @@ Resources:
     Properties:
       Handle: !Ref 'WaitHandle'
       Timeout: '600'
-      Count: 4
+      Count: !Ref NumberOfValidators
 
   S3Bucket:
     Type: AWS::S3::Bucket
@@ -200,17 +201,11 @@ Resources:
         HttpVersion: http2
 
 Outputs:
-  PublicIp:
-    Description: The DNS name for the created instance
-    Value:  !Sub "${Instance.PublicIp}"
-    Export:
-      Name: !Sub "${AWS::StackName}PublicIp"
-
-  PublicIp2:
-    Description: The DNS name for the created instance
-    Value:  !Sub "${Instance2.PublicIp}"
+  AutoScalingId:
+    Description: The Auto Scaling ID
+    Value:  !Ref AutoScalingGroup
     Export:
-      Name: !Sub "${AWS::StackName}PublicIp2"
+      Name: !Sub "${AWS::StackName}AutoScalingGroup"
 
   RPCPublicIp:
     Description: The DNS name for the created instance