/// <summary>
        /// The AssociateAddress operation associates an elastic IP address with an
        /// instance.
        /// If the IP address is currently assigned to another instance, the IP address is
        /// assigned to the new instance. This is an idempotent operation. If you enter it
        /// more than once, Amazon EC2 does not return an error.
        /// 
        /// </summary>
        /// <param name="service">Instance of AmazonEC2 service</param>
        /// <param name="request">AssociateAddressRequest request</param>
        public static void InvokeAssociateAddress(AmazonEC2 service, AssociateAddressRequest request)
        {
            try 
            {
                AssociateAddressResponse response = service.AssociateAddress(request);
                
                
                Console.WriteLine ("Service Response");
                Console.WriteLine ("=============================================================================");
                Console.WriteLine ();

                Console.WriteLine("        AssociateAddressResponse");
                if (response.IsSetResponseMetadata())
                {
                    Console.WriteLine("            ResponseMetadata");
                    ResponseMetadata  responseMetadata = response.ResponseMetadata;
                    if (responseMetadata.IsSetRequestId())
                    {
                        Console.WriteLine("                RequestId");
                        Console.WriteLine("                    {0}", responseMetadata.RequestId);
                    }
                }

            } 
            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;
        }