示例#1
0
        /// <summary>
        /// This function associates an elastic IP to an EC2-Classic instance.
        /// </summary>
        /// <param name="InstanceId">The ID of the instance to be associated to the elastic IP</param>
        /// <param name="PublicId">The public ip ("Elastic IP" column in the AWS console)</param>
        public void AssociateElasticIpToClassicInstance(string InstanceId, string PublicId)
        {
            // Initializing request
            AssociateAddressRequest associateRequest = new AssociateAddressRequest();

            associateRequest.InstanceId = InstanceId;
            associateRequest.PublicIp   = PublicId;

            // Associating address & fetching response
            EC2client.AssociateAddress(associateRequest);
        }
示例#2
0
 private Amazon.EC2.Model.AssociateAddressResponse CallAWSServiceOperation(IAmazonEC2 client, Amazon.EC2.Model.AssociateAddressRequest request)
 {
     Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Elastic Compute Cloud (EC2)", "AssociateAddress");
     try
     {
         #if DESKTOP
         return(client.AssociateAddress(request));
         #elif CORECLR
         return(client.AssociateAddressAsync(request).GetAwaiter().GetResult());
         #else
                 #error "Unknown build edition"
         #endif
     }
     catch (AmazonServiceException exc)
     {
         var webException = exc.InnerException as System.Net.WebException;
         if (webException != null)
         {
             throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
         }
         throw;
     }
 }
        /// <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 Instance LaunchNATInstance(IAmazonEC2 ec2Client, LaunchNATInstanceRequest request)
        {
            if (ec2Client == null)
            {
                throw new ArgumentNullException("ec2Client");
            }
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (string.IsNullOrEmpty(request.SubnetId))
            {
                throw new InvalidOperationException("request.SubnetId is null");
            }
            if (string.IsNullOrEmpty(request.InstanceType))
            {
                throw new InvalidOperationException("request.InstanceType is null");
            }

            List <Filter> filters = new List <Filter>()
            {
                new Filter()
                {
                    Name = "architecture", Values = new List <string>()
                    {
                        "x86_64"
                    }
                },
                new Filter()
                {
                    Name = "name", Values = new List <string>()
                    {
                        "ami-vpc-nat-*.x86_64-ebs"
                    }
                }
            };
            DescribeImagesResponse imageResponse = ec2Client.DescribeImages(new DescribeImagesRequest()
            {
                Filters = filters
            });
            var image = FindNATImage(ec2Client);

            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.Reservation.Instances[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()
            {
                Resources = new List <string>()
                {
                    instanceId
                },
                Tags = new List <Tag>()
                {
                    new Tag()
                    {
                        Key = "Name", Value = "NAT"
                    }
                }
            });

            var allocationId = ec2Client.AllocateAddress(new AllocateAddressRequest()
            {
                Domain = "vpc"
            }).AllocationId;

            ec2Client.AssociateAddress(new AssociateAddressRequest()
            {
                InstanceId = instanceId, AllocationId = allocationId
            });

            var instance = ec2Client.DescribeInstances(new DescribeInstancesRequest()
            {
                InstanceIds = new List <string>()
                {
                    instanceId
                }
            }).Reservations[0].Instances[0];

            return(instance);
        }
示例#4
0
        /// <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 Instance LaunchNATInstance(IAmazonEC2 ec2Client, LaunchNATInstanceRequest request)
        {
            if (ec2Client == null)
                throw new ArgumentNullException("ec2Client");
            if (request == null)
                throw new ArgumentNullException("request");
            if (string.IsNullOrEmpty(request.SubnetId))
                throw new InvalidOperationException("request.SubnetId is null");
            if (string.IsNullOrEmpty(request.InstanceType))
                throw new InvalidOperationException("request.InstanceType is null");

            List<Filter> filters = new List<Filter>()
            {
                new Filter(){Name = "architecture", Values = new List<string>(){"x86_64"}},
                new Filter(){Name = "name", Values = new List<string>(){"ami-vpc-nat-*.x86_64-ebs"}}
            };
            DescribeImagesResponse imageResponse = ec2Client.DescribeImages(new DescribeImagesRequest() { Filters = filters });
            var image = FindNATImage(ec2Client);
            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.Reservation.Instances[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()
            {
                Resources = new List<string>() { instanceId },
                Tags = new List<Tag>() { new Tag() { Key = "Name", Value = "NAT" } }
            });

            var allocationId = ec2Client.AllocateAddress(new AllocateAddressRequest() { Domain = "vpc" }).AllocationId;
            ec2Client.AssociateAddress(new AssociateAddressRequest() { InstanceId = instanceId, AllocationId = allocationId });

            var instance = ec2Client.DescribeInstances(new DescribeInstancesRequest() { InstanceIds = new List<string>() { instanceId } }).Reservations[0].Instances[0];

            return instance;
        }