/// <summary> /// This method will look up the current VPC NAT ami in the region and create an instance in the subnet specified. /// </summary> /// <param name="ec2Client">The ec2client used to create the NAT instance</param> /// <param name="request">The properties used to launch the NAT instance.</param> /// <returns></returns> public static RunningInstance LaunchNATInstance(AmazonEC2 ec2Client, LaunchNATInstanceRequest request) { if (ec2Client == null) { throw new ArgumentNullException("ec2Client"); } if (request == null) { throw new ArgumentNullException("request"); } if (string.IsNullOrEmpty(request.SubnetId)) { throw new ArgumentNullException("request.SubnetId"); } if (string.IsNullOrEmpty(request.InstanceType)) { throw new ArgumentNullException("request.InstanceType"); } List <Filter> filters = new List <Filter>() { new Filter() { Name = "architecture", Value = new List <string>() { "x86_64" } }, new Filter() { Name = "name", Value = new List <string>() { "ami-vpc-nat-*.x86_64-ebs" } } }; DescribeImagesResponse imageResponse = ec2Client.DescribeImages(new DescribeImagesRequest() { Filter = filters }); var image = ImageUtilities.FindImage(ec2Client, ImageUtilities.VPC_NAT); if (image == null) { throw new AmazonEC2Exception("No NAT image found in this region"); } RunInstancesRequest runRequest = new RunInstancesRequest() { InstanceType = request.InstanceType, KeyName = request.KeyName, ImageId = image.ImageId, MinCount = 1, MaxCount = 1, SubnetId = request.SubnetId }; RunInstancesResponse runResponse = ec2Client.RunInstances(runRequest); string instanceId = runResponse.RunInstancesResult.Reservation.RunningInstance[0].InstanceId; // Can't associated elastic IP address until the instance is available WaitForInstanceToStartUp(ec2Client, instanceId); ModifyInstanceAttributeRequest modifyRequest = new ModifyInstanceAttributeRequest() { InstanceId = instanceId, Attribute = "sourceDestCheck", Value = "false" }; ec2Client.ModifyInstanceAttribute(modifyRequest); ec2Client.CreateTags(new CreateTagsRequest() { ResourceId = new List <string>() { instanceId }, Tag = new List <Tag>() { new Tag() { Key = "Name", Value = "NAT" } } }); var allocationId = ec2Client.AllocateAddress(new AllocateAddressRequest() { Domain = "vpc" }).AllocateAddressResult.AllocationId; ec2Client.AssociateAddress(new AssociateAddressRequest() { InstanceId = instanceId, AllocationId = allocationId }); var instance = ec2Client.DescribeInstances(new DescribeInstancesRequest() { InstanceId = new List <string>() { instanceId } }).DescribeInstancesResult.Reservation[0].RunningInstance[0]; return(instance); }
/// <summary> /// The RunInstances operation launches a specified number of instances. /// If Amazon EC2 cannot launch the minimum number AMIs you request, no instances /// launch. If there is insufficient capacity to launch the maximum number of AMIs /// you request, Amazon EC2 launches as many as possible to satisfy the requested /// maximum values. /// Every instance is launched in a security group. If you do not specify a /// security group at launch, the instances start in your default security group. /// For more information on creating security groups, see CreateSecurityGroup. /// An optional instance type can be specified. For information about instance /// types, see Instance Types. /// You can provide an optional key pair ID for each image in the launch request /// (for more information, see CreateKeyPair). All instances that are created from /// images that use this key pair will have access to the associated public key at /// boot. You can use this key to provide secure access to an instance of an image /// on a per-instance basis. Amazon EC2 public images use this feature to provide /// secure access without passwords. /// Important: /// Launching public images without a key pair ID will leave them inaccessible. /// The public key material is made available to the instance at boot time by /// placing it in the openssh_id.pub file on a logical device that is exposed to /// the instance as /dev/sda2 (the ephemeral store). The format of this file is /// suitable for use as an entry within ~/.ssh/authorized_keys (the OpenSSH /// format). This can be done at boot (e.g., as part of rc.local) allowing for /// secure access without passwords. /// Optional user data can be provided in the launch request. All instances that /// collectively comprise the launch request have access to this data For more /// information, see Instance Metadata. /// Note: /// If any of the AMIs have a product code attached for which the user has not /// subscribed, the RunInstances call will fail. /// Important: /// We strongly recommend using the 2.6.18 Xen stock kernel with the c1.medium and /// c1.xlarge instances. Although the default Amazon EC2 kernels will work, the new /// kernels provide greater stability and performance for these instance types. For /// more information about kernels, see Kernels, RAM Disks, and Block Device /// Mappings. /// /// </summary> /// <param name="service">Instance of AmazonEC2 service</param> /// <param name="request">RunInstancesRequest request</param> public static void InvokeRunInstances(AmazonEC2 service, RunInstancesRequest request) { try { RunInstancesResponse response = service.RunInstances(request); Console.WriteLine ("Service Response"); Console.WriteLine ("============================================================================="); Console.WriteLine (); Console.WriteLine(" RunInstancesResponse"); if (response.IsSetResponseMetadata()) { Console.WriteLine(" ResponseMetadata"); ResponseMetadata responseMetadata = response.ResponseMetadata; if (responseMetadata.IsSetRequestId()) { Console.WriteLine(" RequestId"); Console.WriteLine(" {0}", responseMetadata.RequestId); } } if (response.IsSetRunInstancesResult()) { Console.WriteLine(" RunInstancesResult"); RunInstancesResult runInstancesResult = response.RunInstancesResult; if (runInstancesResult.IsSetReservation()) { Console.WriteLine(" Reservation"); Reservation reservation = runInstancesResult.Reservation; if (reservation.IsSetReservationId()) { Console.WriteLine(" ReservationId"); Console.WriteLine(" {0}", reservation.ReservationId); } if (reservation.IsSetOwnerId()) { Console.WriteLine(" OwnerId"); Console.WriteLine(" {0}", reservation.OwnerId); } if (reservation.IsSetRequesterId()) { Console.WriteLine(" RequesterId"); Console.WriteLine(" {0}", reservation.RequesterId); } List<String> groupNameList = reservation.GroupName; foreach (String groupName in groupNameList) { Console.WriteLine(" GroupName"); Console.WriteLine(" {0}", groupName); } List<RunningInstance> runningInstanceList = reservation.RunningInstance; foreach (RunningInstance runningInstance in runningInstanceList) { Console.WriteLine(" RunningInstance"); if (runningInstance.IsSetInstanceId()) { Console.WriteLine(" InstanceId"); Console.WriteLine(" {0}", runningInstance.InstanceId); } if (runningInstance.IsSetImageId()) { Console.WriteLine(" ImageId"); Console.WriteLine(" {0}", runningInstance.ImageId); } if (runningInstance.IsSetInstanceState()) { Console.WriteLine(" InstanceState"); InstanceState instanceState = runningInstance.InstanceState; if (instanceState.IsSetCode()) { Console.WriteLine(" Code"); Console.WriteLine(" {0}", instanceState.Code); } if (instanceState.IsSetName()) { Console.WriteLine(" Name"); Console.WriteLine(" {0}", instanceState.Name); } } if (runningInstance.IsSetPrivateDnsName()) { Console.WriteLine(" PrivateDnsName"); Console.WriteLine(" {0}", runningInstance.PrivateDnsName); } if (runningInstance.IsSetPublicDnsName()) { Console.WriteLine(" PublicDnsName"); Console.WriteLine(" {0}", runningInstance.PublicDnsName); } if (runningInstance.IsSetStateTransitionReason()) { Console.WriteLine(" StateTransitionReason"); Console.WriteLine(" {0}", runningInstance.StateTransitionReason); } if (runningInstance.IsSetKeyName()) { Console.WriteLine(" KeyName"); Console.WriteLine(" {0}", runningInstance.KeyName); } if (runningInstance.IsSetAmiLaunchIndex()) { Console.WriteLine(" AmiLaunchIndex"); Console.WriteLine(" {0}", runningInstance.AmiLaunchIndex); } List<String> productCodeList = runningInstance.ProductCode; foreach (String productCode in productCodeList) { Console.WriteLine(" ProductCode"); Console.WriteLine(" {0}", productCode); } if (runningInstance.IsSetInstanceType()) { Console.WriteLine(" InstanceType"); Console.WriteLine(" {0}", runningInstance.InstanceType); } if (runningInstance.IsSetLaunchTime()) { Console.WriteLine(" LaunchTime"); Console.WriteLine(" {0}", runningInstance.LaunchTime); } if (runningInstance.IsSetPlacement()) { Console.WriteLine(" Placement"); Placement placement = runningInstance.Placement; if (placement.IsSetAvailabilityZone()) { Console.WriteLine(" AvailabilityZone"); Console.WriteLine(" {0}", placement.AvailabilityZone); } } if (runningInstance.IsSetKernelId()) { Console.WriteLine(" KernelId"); Console.WriteLine(" {0}", runningInstance.KernelId); } if (runningInstance.IsSetRamdiskId()) { Console.WriteLine(" RamdiskId"); Console.WriteLine(" {0}", runningInstance.RamdiskId); } if (runningInstance.IsSetPlatform()) { Console.WriteLine(" Platform"); Console.WriteLine(" {0}", runningInstance.Platform); } } } } } catch (AmazonEC2Exception ex) { Console.WriteLine("Caught Exception: " + ex.Message); Console.WriteLine("Response Status Code: " + ex.StatusCode); Console.WriteLine("Error Code: " + ex.ErrorCode); Console.WriteLine("Error Type: " + ex.ErrorType); Console.WriteLine("Request ID: " + ex.RequestId); Console.WriteLine("XML: " + ex.XML); } }
/// <summary> /// This method will look up the current VPC NAT ami in the region and create an instance in the subnet specified. /// </summary> /// <param name="ec2Client">The ec2client used to create the NAT instance</param> /// <param name="request">The properties used to launch the NAT instance.</param> /// <returns></returns> public static RunningInstance LaunchNATInstance(AmazonEC2 ec2Client, LaunchNATInstanceRequest request) { if (ec2Client == null) throw new ArgumentNullException("ec2Client"); if (request == null) throw new ArgumentNullException("request"); if (string.IsNullOrEmpty(request.SubnetId)) throw new ArgumentNullException("request.SubnetId"); if (string.IsNullOrEmpty(request.InstanceType)) throw new ArgumentNullException("request.InstanceType"); List<Filter> filters = new List<Filter>() { new Filter(){Name = "architecture", Value = new List<string>(){"x86_64"}}, new Filter(){Name = "name", Value = new List<string>(){"ami-vpc-nat-*.x86_64-ebs"}} }; DescribeImagesResponse imageResponse = ec2Client.DescribeImages(new DescribeImagesRequest() { Filter = filters }); var image = ImageUtilities.FindImage(ec2Client, ImageUtilities.VPC_NAT); if (image == null) { throw new AmazonEC2Exception("No NAT image found in this region"); } RunInstancesRequest runRequest = new RunInstancesRequest() { InstanceType = request.InstanceType, KeyName = request.KeyName, ImageId = image.ImageId, MinCount = 1, MaxCount = 1, SubnetId = request.SubnetId }; RunInstancesResponse runResponse = ec2Client.RunInstances(runRequest); string instanceId = runResponse.RunInstancesResult.Reservation.RunningInstance[0].InstanceId; // Can't associated elastic IP address until the instance is available WaitForInstanceToStartUp(ec2Client, instanceId); ModifyInstanceAttributeRequest modifyRequest = new ModifyInstanceAttributeRequest() { InstanceId = instanceId, Attribute = "sourceDestCheck", Value = "false" }; ec2Client.ModifyInstanceAttribute(modifyRequest); ec2Client.CreateTags(new CreateTagsRequest() { ResourceId = new List<string>() { instanceId }, Tag = new List<Tag>() { new Tag() { Key = "Name", Value = "NAT" } } }); var allocationId = ec2Client.AllocateAddress(new AllocateAddressRequest() { Domain = "vpc" }).AllocateAddressResult.AllocationId; ec2Client.AssociateAddress(new AssociateAddressRequest() { InstanceId = instanceId, AllocationId = allocationId }); var instance = ec2Client.DescribeInstances(new DescribeInstancesRequest() { InstanceId = new List<string>() { instanceId } }).DescribeInstancesResult.Reservation[0].RunningInstance[0]; return instance; }
public void launch() { //instance started. if (string.IsNullOrEmpty(_instanceId) == false) { return; } try { RunInstancesRequest request = new RunInstancesRequest(); request.ImageId = _amiId; request.MinCount = 1; request.MaxCount = 1; if (_defaultSecurityGroup == false) { request.SecurityGroup.Add(_securityGroups); } else { if (securitryGroupExistOnServer() == false) { createSecurityGroup(); } request.SecurityGroup.Add(_securityGroups); } string keyPath = CAwsConfig.Instance.getKeyFilePath(_keyPairName); if (string.IsNullOrEmpty(keyPath) == true || File.Exists(keyPath) == false) { if (keyExistOnServer() == false) { createKayPair(); } } request.KeyName = _keyPairName; RunInstancesResponse response = _service.RunInstances(request); if (response.IsSetRunInstancesResult()) { RunInstancesResult runInstancesResult = response.RunInstancesResult; if (runInstancesResult.IsSetReservation()) { if (runInstancesResult.Reservation.RunningInstance[0].IsSetInstanceId()) { _instanceId = runInstancesResult.Reservation.RunningInstance[0].InstanceId; } if (runInstancesResult.Reservation.RunningInstance[0].IsSetPublicDnsName()) { _publicDns = runInstancesResult.Reservation.RunningInstance[0].PublicDnsName; } } } if (string.IsNullOrEmpty(_instanceId) == true) { throw new Exception("No instance id is returned."); } //return after the instance started up bool pending = true; while (pending == true) { System.Threading.Thread.Sleep(5 * 1000); DescribeInstancesRequest describeRequest = new DescribeInstancesRequest(); describeRequest.InstanceId.Add(_instanceId); DescribeInstancesResponse describeResponse = _service.DescribeInstances(describeRequest); DescribeInstancesResult describeResult = describeResponse.DescribeInstancesResult; if (describeResult.Reservation.Count != 1) { throw new Exception("more than one instance with the same id"); } if (describeResult.Reservation[0].RunningInstance.Count != 1) { throw new Exception("more than one running instance has the same id"); } pending = describeResult.Reservation[0].RunningInstance[0].InstanceState.Name != "running"; } } catch (AmazonEC2Exception ex) { throw new Exception("Caught Exception: " + ex.XML); } }