/// <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 List <CEc2Ami> describeImages(string owner) { List <CEc2Ami> amis = new List <CEc2Ami>(); try { DescribeImagesRequest request = new DescribeImagesRequest(); if (string.IsNullOrEmpty(owner) == false) { List <string> owners = new List <string>(); owners.Add(owner); request.Owner = owners; } DescribeImagesResponse response = _service.DescribeImages(request); if (response.IsSetDescribeImagesResult()) { DescribeImagesResult describeImagesResult = response.DescribeImagesResult; List <Image> imageList = describeImagesResult.Image; foreach (Image image in imageList) { CEc2Ami ami = new CEc2Ami(); if (image.IsSetImageId()) { ami.imageId = image.ImageId; } if (image.IsSetImageLocation()) { ami.imageLocation = image.ImageLocation; } if (image.IsSetArchitecture()) { ami.architecture = image.Architecture; } if (image.IsSetImageType()) { ami.imageType = image.ImageType; } if (image.IsSetPlatform()) { ami.platform = image.Platform; } amis.Add(ami); } } } catch (AmazonEC2Exception ex) { throw new Exception("Caught Exception: " + ex.XML); } return(amis); }
/// <summary> /// Find the Amazon machine image identified by the ImageDescriptor /// </summary> /// <param name="ec2Client">The EC2 client used to search for the image.</param> /// <param name="descriptor">The descriptor used to identify the image.</param> /// <returns>The Amazon machine image.</returns> public static Image FindImage(AmazonEC2 ec2Client, ImageDescriptor descriptor) { if (ec2Client == null) throw new ArgumentNullException("ec2Client"); if (descriptor == null) throw new ArgumentNullException("descriptor"); var result = ec2Client.DescribeImages(new DescribeImagesRequest() { Owner = new List<string>() { "amazon" }, Filter = new List<Filter>() { new Filter(){Name = "name", Value = new List<string>(){descriptor.NamePrefix}} } }).DescribeImagesResult; if (result.Image.Count == 0) return null; var image = result.Image.OrderByDescending(x => x.Name).First(); return image; }
/// <summary> /// Find the Amazon machine image identified by the ImageDescriptor /// </summary> /// <param name="ec2Client">The EC2 client used to search for the image.</param> /// <param name="descriptor">The descriptor used to identify the image.</param> /// <returns>The Amazon machine image.</returns> public static Image FindImage(AmazonEC2 ec2Client, ImageDescriptor descriptor) { if (ec2Client == null) { throw new ArgumentNullException("ec2Client"); } if (descriptor == null) { throw new ArgumentNullException("descriptor"); } var result = ec2Client.DescribeImages(new DescribeImagesRequest() { Owner = new List <string>() { "amazon" }, Filter = new List <Filter>() { new Filter() { Name = "name", Value = new List <string>() { descriptor.NamePrefix } } } }).DescribeImagesResult; if (result.Image.Count == 0) { return(null); } var image = result.Image.OrderByDescending(x => x.Name).First(); return(image); }
/// <summary> /// Find the Amazon machine image identified by the ImageDescriptor. /// </summary> /// <param name="ec2Client">The EC2 client used to search for the image.</param> /// <param name="descriptor">The descriptor used to identify the image.</param> /// <returns>The Amazon machine image.</returns> public static Image FindImage(AmazonEC2 ec2Client, ImageDescriptor descriptor) { if (ec2Client == null) throw new ArgumentNullException("ec2Client"); if (descriptor == null) throw new ArgumentNullException("descriptor"); int retryCount = 1; Image image = null; do { var result = ec2Client.DescribeImages(new DescribeImagesRequest() { Owner = new List<string>() { "amazon" }, Filter = new List<Filter>() { new Filter(){Name = "name", Value = new List<string>(){descriptor.NamePrefix}} } }).DescribeImagesResult; if (result.Image.Count != 0) image = result.Image.OrderByDescending(x => x.Name).First(); else { // backing control file may be outdated, reload and try once more if (retryCount == 1) { LOGGER.InfoFormat("FindImage - DescribeImages call for image descriptor '{0}' (name prefix '{1}') yielded no results, assuming outdated control file and reloading", descriptor.DefinitionKey, descriptor.NamePrefix); LoadDefinitionsFromWeb(); } retryCount++; } } while (image == null && retryCount <= 2); if (image == null) LOGGER.InfoFormat("FindImage - failed to find valid AMI image for descriptor '{0}' (name prefix '{1}')", descriptor.DefinitionKey, descriptor.NamePrefix); return image; }
/// <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> /// Find the Amazon machine image identified by the ImageDescriptor. /// </summary> /// <param name="ec2Client">The EC2 client used to search for the image.</param> /// <param name="descriptor">The descriptor used to identify the image.</param> /// <returns>The Amazon machine image.</returns> public static Image FindImage(AmazonEC2 ec2Client, ImageDescriptor descriptor) { if (ec2Client == null) { throw new ArgumentNullException("ec2Client"); } if (descriptor == null) { throw new ArgumentNullException("descriptor"); } int retryCount = 1; Image image = null; do { var result = ec2Client.DescribeImages(new DescribeImagesRequest() { Owner = new List <string>() { "amazon" }, Filter = new List <Filter>() { new Filter() { Name = "name", Value = new List <string>() { descriptor.NamePrefix } } } }).DescribeImagesResult; if (result.Image.Count != 0) { image = result.Image.OrderByDescending(x => x.Name).First(); } else { // backing control file may be outdated, reload and try once more if (retryCount == 1) { LOGGER.InfoFormat("FindImage - DescribeImages call for image descriptor '{0}' (name prefix '{1}') yielded no results, assuming outdated control file and reloading", descriptor.DefinitionKey, descriptor.NamePrefix); LoadDefinitionsFromWeb(); } retryCount++; } } while (image == null && retryCount <= 2); if (image == null) { LOGGER.InfoFormat("FindImage - failed to find valid AMI image for descriptor '{0}' (name prefix '{1}')", descriptor.DefinitionKey, descriptor.NamePrefix); } return(image); }
/// <summary> /// The DescribeImages operation returns information about AMIs, AKIs, and ARIs /// available to the user. Information returned includes image type, product codes, /// architecture, and kernel and RAM disk IDs. Images available to the user include /// public images available for any user to launch, private images owned by the /// user making the request, and private images owned by other users for which the /// user has explicit launch permissions. /// Launch permissions fall into three categories: /// Public: /// The owner of the AMI granted launch permissions for the AMI to the all group. /// All users have launch permissions for these AMIs. /// Explicit: /// The owner of the AMI granted launch permissions to a specific user. /// Implicit: /// A user has implicit launch permissions for all AMIs he or she owns. /// The list of AMIs returned can be modified by specifying AMI IDs, AMI owners, or /// users with launch permissions. If no options are specified, Amazon EC2 returns /// all AMIs for which the user has launch permissions. /// If you specify one or more AMI IDs, only AMIs that have the specified IDs are /// returned. If you specify an invalid AMI ID, a fault is returned. If you specify /// an AMI ID for which you do not have access, it will not be included in the /// returned results. /// If you specify one or more AMI owners, only AMIs from the specified owners and /// for which you have access are returned. The results can include the account IDs /// of the specified owners, amazon for AMIs owned by Amazon or self for AMIs that /// you own. /// If you specify a list of executable users, only users that have launch /// permissions for the AMIs are returned. You can specify account IDs (if you own /// the AMI(s)), self for AMIs for which you own or have explicit permissions, or /// all for public AMIs. /// Note: /// Deregistered images are included in the returned results for an unspecified /// interval after deregistration. /// /// </summary> /// <param name="service">Instance of AmazonEC2 service</param> /// <param name="request">DescribeImagesRequest request</param> public static void InvokeDescribeImages(AmazonEC2 service, DescribeImagesRequest request) { try { DescribeImagesResponse response = service.DescribeImages(request); Console.WriteLine ("Service Response"); Console.WriteLine ("============================================================================="); Console.WriteLine (); Console.WriteLine(" DescribeImagesResponse"); if (response.IsSetResponseMetadata()) { Console.WriteLine(" ResponseMetadata"); ResponseMetadata responseMetadata = response.ResponseMetadata; if (responseMetadata.IsSetRequestId()) { Console.WriteLine(" RequestId"); Console.WriteLine(" {0}", responseMetadata.RequestId); } } if (response.IsSetDescribeImagesResult()) { Console.WriteLine(" DescribeImagesResult"); DescribeImagesResult describeImagesResult = response.DescribeImagesResult; List<Image> imageList = describeImagesResult.Image; foreach (Image image in imageList) { Console.WriteLine(" Image"); if (image.IsSetImageId()) { Console.WriteLine(" ImageId"); Console.WriteLine(" {0}", image.ImageId); } if (image.IsSetImageLocation()) { Console.WriteLine(" ImageLocation"); Console.WriteLine(" {0}", image.ImageLocation); } if (image.IsSetImageState()) { Console.WriteLine(" ImageState"); Console.WriteLine(" {0}", image.ImageState); } if (image.IsSetOwnerId()) { Console.WriteLine(" OwnerId"); Console.WriteLine(" {0}", image.OwnerId); } if (image.IsSetVisibility()) { Console.WriteLine(" Visibility"); Console.WriteLine(" {0}", image.Visibility); } List<String> productCodeList = image.ProductCode; foreach (String productCode in productCodeList) { Console.WriteLine(" ProductCode"); Console.WriteLine(" {0}", productCode); } if (image.IsSetArchitecture()) { Console.WriteLine(" Architecture"); Console.WriteLine(" {0}", image.Architecture); } if (image.IsSetImageType()) { Console.WriteLine(" ImageType"); Console.WriteLine(" {0}", image.ImageType); } if (image.IsSetKernelId()) { Console.WriteLine(" KernelId"); Console.WriteLine(" {0}", image.KernelId); } if (image.IsSetRamdiskId()) { Console.WriteLine(" RamdiskId"); Console.WriteLine(" {0}", image.RamdiskId); } if (image.IsSetPlatform()) { Console.WriteLine(" Platform"); Console.WriteLine(" {0}", image.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); } }