/// <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; }
/// <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 }; // Create the launch specification LaunchSpecification launchSpecification = new LaunchSpecification () { ImageId = AMI_ID, InstanceType = InstanceType, KeyName = KeyPairName, SecurityGroups = SecurityGroups, NetworkInterfaces = NetworkInterfaces, 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; }
private void CreateAndLaunchInstance(AwsRegionLocations region) { // Get an Ec2Client for the current region var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint())); var securityGroupId = EnsureSecurityGroupExists(region); var availableSubnets = client.DescribeSubnets().Subnets.OrderByDescending(x => x.AvailableIpAddressCount); var networkSpecification = new InstanceNetworkInterfaceSpecification() { DeviceIndex = 0, SubnetId = availableSubnets.First().SubnetId, Groups = new List<string>() { securityGroupId }, AssociatePublicIpAddress = true }; var networkSpecifications = new List<InstanceNetworkInterfaceSpecification>() { networkSpecification }; var launchRequest = new RunInstancesRequest() { ImageId = GetAmiId(client, amiName), InstanceType = "t2.micro", MinCount = 1, MaxCount = 1, KeyName = keyPairName, NetworkInterfaces = networkSpecifications }; client.RunInstances(launchRequest); }