Example #1
0
        }                                          // Last error message


        /// <summary>
        /// Class constructors: default (no parameters), with region endpoint, and with endpoint + credentials
        /// </summary>
        public AWSEC2Helper()
        {
            Initialize(RegionEndpoint.USEast1,
                       Gadgets.LoadConfigurationSetting("AWSAccessKey", ""),
                       Gadgets.LoadConfigurationSetting("AWSSecretKey", ""));
        }
Example #2
0
        /// <summary>
        /// This function creates a set of instances into an EC2 VPC. It returns the Ids of the created instances if successful, or
        /// sets the error code and message otherwise
        /// </summary>
        /// <param name="regionEndpoint">Region where instances should be created</param>
        /// <param name="SubnetId">Id of the VPC subnet where the instances will be launched</param>
        /// <param name="AMI_ID">Id of the AMI that will be used as a base for the instances</param>
        /// <param name="SecurityGroupId">The name of the security group to be assigned to the instance(s)</param>
        /// <param name="KeyPairName">The name of the keypair to be assigned to the instance(s)</param>
        /// <param name="InstanceType">The type of the instance(s)</param>
        /// <param name="InstanceCount">The number of instances to be launched</param>
        /// <param name="UserData">The user-data script that will be run as the instance(s) is(are) initialized</param>
        /// <returns>The list of Instance Ids if successful</returns>
        public List <string> CreateVPCInstances(RegionEndpoint regionEndpoint, string SubnetId, string AMI_ID, string SecurityGroupId, string KeyPairName, string InstanceType, int InstanceCount = 1, string UserData = "")
        {
            List <string> InstanceIds = new List <string> ();

            // Initialize error values
            ErrorCode    = 0;
            ErrorMessage = "";

            // Create the list with security groups
            List <string> SecurityGroups = new List <string> ()
            {
                SecurityGroupId
            };

            // Create the network interface object (to connect with the VPC)
            var NetworkInterface = new InstanceNetworkInterfaceSpecification()
            {
                DeviceIndex = 0,
                SubnetId    = SubnetId,
                Groups      = SecurityGroups,
                AssociatePublicIpAddress = true
            };
            List <InstanceNetworkInterfaceSpecification> NetworkInterfaces = new List <InstanceNetworkInterfaceSpecification> ()
            {
                NetworkInterface
            };

            // Create the request object
            var launchRequest = new RunInstancesRequest()
            {
                ImageId           = AMI_ID,
                InstanceType      = InstanceType,
                MinCount          = InstanceCount,
                MaxCount          = InstanceCount,
                KeyName           = KeyPairName,
                NetworkInterfaces = NetworkInterfaces,
                UserData          = Gadgets.Base64Encode(UserData)
            };

            // Launch the instances
            try
            {
                var launchResponse = EC2client.RunInstances(launchRequest);

                // Check response for errors
                if (launchResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    ErrorCode    = Convert.ToInt32(launchResponse.HttpStatusCode);
                    ErrorMessage = "Http Error [" + launchResponse.HttpStatusCode.ToString() + "]";
                }
                else
                {
                    List <Instance> createdInstances = launchResponse.Reservation.Instances;
                    foreach (Instance instance in createdInstances)
                    {
                        InstanceIds.Add(instance.InstanceId);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCode    = -1;
                ErrorMessage = ex.Message + "::" + ex.InnerException;
            }

            return(InstanceIds);
        }
Example #3
0
        /// <summary>
        /// This function creates a spot instance request inside a VPC. It returns the request ID if successful, or sets the error
        /// code and message otherwise
        /// </summary>
        /// <param name="AvailabilityZone">Name of the Availability Zone where the instances will be launched</param>
        /// <param name="AMI_ID">Id of the AMI that will be used as a base for the instances</param>
        /// <param name="SecurityGroupId">The name of the security group to be assigned to the instance(s)</param>
        /// <param name="KeyPairName">The name of the keypair to be assigned to the instance(s)</param>
        /// <param name="InstanceType">The type of the instance(s)</param>
        /// <param name="InstancePrice">The max price to pay for the instance(s)</param>
        /// <param name="InstanceCount">The number of instances to be launched</param>
        /// <param name="UserData">The user-data script that will be run as the instance(s) is(are) initialized</param>
        /// <returns>The list of Request Ids if successful</returns>
        public List <string> RequestClassicSpotInstances(string AvailabilityZone, string AMI_ID, string SecurityGroupId, string KeyPairName, string InstanceType, double InstancePrice, int InstanceCount = 1, string UserData = "")
        {
            List <string> RequestIds = new List <string> ();

            // Initialize error values
            ErrorCode    = 0;
            ErrorMessage = "";

            // Create the list with security groups
            List <string> SecurityGroups = new List <string> ()
            {
                SecurityGroupId
            };

            // Create placement object
            SpotPlacement spotPlacement = new SpotPlacement()
            {
                AvailabilityZone = AvailabilityZone
            };

            // Create the launch specification
            LaunchSpecification launchSpecification = new LaunchSpecification()
            {
                ImageId        = AMI_ID,
                InstanceType   = InstanceType,
                KeyName        = KeyPairName,
                SecurityGroups = SecurityGroups,
                Placement      = spotPlacement,
                UserData       = Gadgets.Base64Encode(UserData)
            };

            // Create the request object
            RequestSpotInstancesRequest spotRequest = new RequestSpotInstancesRequest()
            {
                SpotPrice           = InstancePrice.ToString(),
                InstanceCount       = InstanceCount,
                LaunchSpecification = launchSpecification
            };

            // Request the instances
            try
            {
                var spotResponse = EC2client.RequestSpotInstances(spotRequest);

                // Check response for errors
                if (spotResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    ErrorCode    = Convert.ToInt32(spotResponse.HttpStatusCode);
                    ErrorMessage = "Http Error [" + spotResponse.HttpStatusCode.ToString() + "]";
                }
                else
                {
                    foreach (SpotInstanceRequest request in spotResponse.SpotInstanceRequests)
                    {
                        RequestIds.Add(request.SpotInstanceRequestId);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCode    = -1;
                ErrorMessage = ex.Message + "::" + ex.InnerException;
            }

            return(RequestIds);
        }
Example #4
0
        /// <summary>
        /// This function creates a spot instance request inside a VPC. It returns the request ID if successful, or sets the error
        /// code and message otherwise
        /// </summary>
        /// <param name="SubnetId">Id of the VPC subnet where the instances will be launched</param>
        /// <param name="AMI_ID">Id of the AMI that will be used as a base for the instances</param>
        /// <param name="SecurityGroupId">The name of the security group to be assigned to the instance(s)</param>
        /// <param name="KeyPairName">The name of the keypair to be assigned to the instance(s)</param>
        /// <param name="InstanceType">The type of the instance(s)</param>
        /// <param name="InstancePrice">The max price to pay for the instance(s)</param>
        /// <param name="InstanceCount">The number of instances to be launched</param>
        /// <param name="UserData">The user-data script that will be run as the instance(s) is(are) initialized</param>
        /// <returns>The list of Request Ids if successful</returns>
        public List <string> RequestVPCSpotInstances(string SubnetId, string AMI_ID, string SecurityGroupId, string KeyPairName, string InstanceType, double InstancePrice, int InstanceCount = 1, string UserData = "")
        {
            List <string> RequestIds = new List <string> ();

            // Initialize error values
            ErrorCode    = 0;
            ErrorMessage = "";

            // Create the list with security groups
            List <string> SecurityGroups = new List <string> ()
            {
                SecurityGroupId
            };

            // Create the network interface object (to connect with the VPC)
            var NetworkInterface = new InstanceNetworkInterfaceSpecification()
            {
                DeviceIndex = 0,
                SubnetId    = SubnetId,
                Groups      = SecurityGroups,
                AssociatePublicIpAddress = true
            };
            List <InstanceNetworkInterfaceSpecification> NetworkInterfaces = new List <InstanceNetworkInterfaceSpecification> ()
            {
                NetworkInterface
            };

            LaunchSpecification launchSpecification = new LaunchSpecification()
            {
                ImageId           = AMI_ID,
                InstanceType      = InstanceType,
                KeyName           = KeyPairName,
                NetworkInterfaces = NetworkInterfaces,
                UserData          = Gadgets.Base64Encode(UserData)
            };

            // Create the request object
            RequestSpotInstancesRequest spotRequest = new RequestSpotInstancesRequest()
            {
                SpotPrice           = InstancePrice.ToString(CultureInfo.InvariantCulture),
                InstanceCount       = InstanceCount,
                LaunchSpecification = launchSpecification,
            };


            // Request the instances
            try
            {
                var spotResponse = EC2client.RequestSpotInstances(spotRequest);

                // Check response for errors
                if (spotResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    ErrorCode    = Convert.ToInt32(spotResponse.HttpStatusCode);
                    ErrorMessage = "Http Error [" + spotResponse.HttpStatusCode.ToString() + "]";
                }
                else
                {
                    foreach (SpotInstanceRequest request in spotResponse.SpotInstanceRequests)
                    {
                        RequestIds.Add(request.SpotInstanceRequestId);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCode    = -1;
                ErrorMessage = ex.Message + "::" + ex.InnerException;
            }

            return(RequestIds);
        }