/// <summary> /// The AllocateAddress operation acquires an elastic IP address for use with your /// account. /// /// </summary> /// <param name="service">Instance of AmazonEC2 service</param> /// <param name="request">AllocateAddressRequest request</param> public static void InvokeAllocateAddress(AmazonEC2 service, AllocateAddressRequest request) { try { AllocateAddressResponse response = service.AllocateAddress(request); Console.WriteLine ("Service Response"); Console.WriteLine ("============================================================================="); Console.WriteLine (); Console.WriteLine(" AllocateAddressResponse"); if (response.IsSetResponseMetadata()) { Console.WriteLine(" ResponseMetadata"); ResponseMetadata responseMetadata = response.ResponseMetadata; if (responseMetadata.IsSetRequestId()) { Console.WriteLine(" RequestId"); Console.WriteLine(" {0}", responseMetadata.RequestId); } } if (response.IsSetAllocateAddressResult()) { Console.WriteLine(" AllocateAddressResult"); AllocateAddressResult allocateAddressResult = response.AllocateAddressResult; if (allocateAddressResult.IsSetPublicIp()) { Console.WriteLine(" PublicIp"); Console.WriteLine(" {0}", allocateAddressResult.PublicIp); } } } 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); }
/// <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; }