コード例 #1
0
        public async Task StopScalingGroup(string groupName)
        {
            UpdateAutoScalingGroupRequest request = new UpdateAutoScalingGroupRequest();

            request.MaxSize = request.MinSize = request.DesiredCapacity = 0;
            request.AutoScalingGroupName = groupName;
            await client.UpdateAutoScalingGroupAsync(request);
        }
コード例 #2
0
        public virtual void failover()
        {
            try
            {
                // Modify the autoscaling group to remove the AZ affected which is the AZ passed in the input
                // Find the autoscaling group that this is deployed into
                // Note: This changes the asynchronous call to a synchronous one
                DescribeAutoScalingGroupsResponse autoScalingGroupsResponse = AUTO_SCALING_CLIENT.DescribeAutoScalingGroupsAsync().GetAwaiter().GetResult();

                if (autoScalingGroupsResponse != null && autoScalingGroupsResponse.AutoScalingGroups.Count > 0)
                {
                    // Note: This assumes an Auto Scaling group exists; no error checking for readability
                    AutoScalingGroup autoScalingGroup     = autoScalingGroupsResponse.AutoScalingGroups[0];
                    string           autoScalingGroupName = autoScalingGroup.AutoScalingGroupName;

                    // Find all subnets in the availability zone passed in the input
                    DescribeSubnetsResponse subnetsResult
                        = EC2_CLIENT.DescribeSubnetsAsync(new DescribeSubnetsRequest()
                    {
                        Filters = new List <Amazon.EC2.Model.Filter> {
                            new Amazon.EC2.Model.Filter {
                                Name   = "vpc-id",
                                Values = new List <string> {
                                    vpcId
                                }
                            }
                        }
                    }).GetAwaiter().GetResult();
                    IList <string> desiredSubnetsForASG = new List <string>();
                    foreach (Amazon.EC2.Model.Subnet subnet in subnetsResult.Subnets)
                    {
                        if (!string.Equals(subnet.AvailabilityZone, azId, StringComparison.OrdinalIgnoreCase))
                        {
                            desiredSubnetsForASG.Add(subnet.SubnetId);
                        }
                    }

                    List <string> desiredSubnets = new List <String>(autoScalingGroup.VPCZoneIdentifier.Split(new[] { ',' }, StringSplitOptions.None));

                    var tempList = new List <String>(desiredSubnets);
                    foreach (var subnet in desiredSubnets)
                    {
                        if (!desiredSubnetsForASG.Contains(subnet))
                        {
                            tempList.Remove(subnet);
                        }
                    }
                    desiredSubnets = tempList;

                    Console.WriteLine("Updating the auto scaling group " + autoScalingGroupName + " to remove the subnet in the AZ");

                    // Note: This turns the asynchronous call into a synchronous one
                    UpdateAutoScalingGroupResponse updateAutoScalingGroupResponse
                        = AUTO_SCALING_CLIENT.UpdateAutoScalingGroupAsync(new UpdateAutoScalingGroupRequest
                    {
                        AutoScalingGroupName = autoScalingGroupName,
                        VPCZoneIdentifier    = string.Join(",", desiredSubnets)
                    }).GetAwaiter().GetResult();
                }

                // Find all subnets in the availability zone passed in the input
                // Note: This turns the asynchronous call into a synchronous one
                DescribeSubnetsResponse describeSubnetsResult
                    = EC2_CLIENT.DescribeSubnetsAsync(new DescribeSubnetsRequest
                {
                    Filters = new List <Amazon.EC2.Model.Filter> {
                        new Amazon.EC2.Model.Filter {
                            Name   = "vpc-id",
                            Values = new List <string> {
                                vpcId
                            }
                        },
                        new Amazon.EC2.Model.Filter {
                            Name   = "availabilityZone",
                            Values = new List <string> {
                                azId
                            }
                        }
                    }
                }).GetAwaiter().GetResult();

                IList <string> desiredSubnetsForAddingNewNacl = new List <string>();
                foreach (Amazon.EC2.Model.Subnet subnet in describeSubnetsResult.Subnets)
                {
                    desiredSubnetsForAddingNewNacl.Add(subnet.SubnetId);
                }

                //Find all the network acl associations matching the subnets identified above
                // Note: This turns the asynchronous call into a synchronous one
                DescribeNetworkAclsResponse describeNetworkAclsResult
                    = EC2_CLIENT.DescribeNetworkAclsAsync(new DescribeNetworkAclsRequest()
                {
                    Filters = new List <Amazon.EC2.Model.Filter> {
                        new Amazon.EC2.Model.Filter {
                            Name   = "association.subnet-id",
                            Values = (List <string>)desiredSubnetsForAddingNewNacl
                        }
                    }
                }).GetAwaiter().GetResult();

                IList <NetworkAclAssociation> desiredAclAssociations = new List <NetworkAclAssociation>();
                // Note: This assumes a Network ACL is present for readability
                IList <NetworkAclAssociation> networkAclsAssociatedWithSubnet = describeNetworkAclsResult.NetworkAcls[0].Associations;
                foreach (string subnetId in desiredSubnetsForAddingNewNacl)
                {
                    foreach (NetworkAclAssociation networkAcl in networkAclsAssociatedWithSubnet)
                    {
                        if (string.Equals(networkAcl.SubnetId, subnetId, StringComparison.OrdinalIgnoreCase))
                        {
                            desiredAclAssociations.Add(networkAcl);
                        }
                    }
                }

                //create new network acl association with both ingress and egress denying to all the traffic
                CreateNetworkAclRequest createNetworkAclRequest = new CreateNetworkAclRequest();
                createNetworkAclRequest.VpcId = vpcId;
                // Note: This turns the asynchronous call into a synchronous one
                CreateNetworkAclResponse createNetworkAclResponse = EC2_CLIENT.CreateNetworkAclAsync(createNetworkAclRequest).GetAwaiter().GetResult();
                string networkAclId = createNetworkAclResponse.NetworkAcl.NetworkAclId;
                createNetworkAclEntry(networkAclId, 100, "0.0.0.0/0", true, "-1", createPortRange(0, 65535), RuleAction.Deny);
                createNetworkAclEntry(networkAclId, 101, "0.0.0.0/0", false, "-1", createPortRange(0, 65535), RuleAction.Deny);

                // replace all the network acl associations identified for the above subnets with the new network
                // acl association which will deny all traffic for those subnets in that AZ
                Console.WriteLine("Creating new network ACL associations");
                replaceNetworkAclAssociations(desiredAclAssociations, networkAclId);

                //fail over rds which is in the same AZ
                // Note: This turns the asynchronous call into a synchronous one
                DescribeDBInstancesResponse describeDBInstancesResult = RDS_CLIENT.DescribeDBInstancesAsync().GetAwaiter().GetResult();
                IList <DBInstance>          dbInstances = describeDBInstancesResult.DBInstances;
                string dbInstancedId = null;
                foreach (DBInstance dbInstance in dbInstances)
                {
                    if (string.Equals(dbInstance.DBSubnetGroup.VpcId, vpcId, StringComparison.OrdinalIgnoreCase) &&
                        (string.Equals(dbInstance.AvailabilityZone, azId, StringComparison.OrdinalIgnoreCase)) &&
                        dbInstance.MultiAZ && dbInstance.StatusInfos.Count == 0)
                    {
                        dbInstancedId = dbInstance.DBInstanceIdentifier;
                    }
                }
                // we want to fail over rds if rds is present in the same az where it is affected
                if (!string.IsNullOrEmpty(dbInstancedId))

                {
                    RebootDBInstanceRequest rebootDBInstanceRequest = new RebootDBInstanceRequest();
                    rebootDBInstanceRequest.DBInstanceIdentifier = dbInstancedId;
                    rebootDBInstanceRequest.ForceFailover        = true;
                    Console.WriteLine("Rebooting dbInstanceId to secondary AZ " + dbInstancedId);
                    // Note: This turns the asynchronous call into a synchronous one
                    RDS_CLIENT.RebootDBInstanceAsync(rebootDBInstanceRequest).GetAwaiter().GetResult();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Unkown exception occured " + exception.Message);
            }
        }