예제 #1
0
        public string EnsureSecurityGroupExists(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Find the VPC ID for this region
            var vpcId = client.DescribeVpcs().Vpcs.Single().VpcId;

            // Does the security group with our preset name exist?
            var matchingGroups = client.DescribeSecurityGroups().SecurityGroups.Where(x => x.GroupName == securityGroupName).ToList();

            if (matchingGroups.Any())
            {
                // If it exists, assert that it has the same VPC ID as the one we found earlier
                if (matchingGroups.Single().VpcId != vpcId)
                {
                    throw new Exception("Security Group already exists with invalid VPC.");
                }
                return(matchingGroups.Single().GroupId);
            }
            else
            {
                // It does not exist, so create it.
                return(CreateSecurityGroup(client, vpcId, securityGroupName));
            }
        }
        public string EnsureSecurityGroupExists(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Find the VPC ID for this region
            var vpcId = client.DescribeVpcs().Vpcs.Single().VpcId;

            // Does the security group with our preset name exist?
            var matchingGroups = client.DescribeSecurityGroups().SecurityGroups.Where(x => x.GroupName == securityGroupName).ToList();
            if (matchingGroups.Any())
            {
                // If it exists, assert that it has the same VPC ID as the one we found earlier
                if (matchingGroups.Single().VpcId != vpcId)
                {
                    throw new Exception("Security Group already exists with invalid VPC.");
                }
                return matchingGroups.Single().GroupId;
            }
            else
            {
                // It does not exist, so create it.
                return CreateSecurityGroup(client, vpcId, securityGroupName);
            }
        }
예제 #3
0
        public RegionDetails GetRegionData(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" or "Pending" state.
            request.Filters.Add(new Filter("instance-state-code", new List <string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
                ((ushort)InstanceStatuses.Pending).ToString()
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            return(new RegionDetails()
            {
                Name = regionEndpoint.SystemName,
                Region = region,
                Instances = reservations.SelectMany(x => x.Instances).Select(x => x.ToInstanceInfo()).ToList()
            });
        }
        public RegionDetails GetRegionData(AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, regionEndpoint));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" or "Pending" state.
            request.Filters.Add(new Filter("instance-state-code", new List<string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
                ((ushort)InstanceStatuses.Pending).ToString()
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            return new RegionDetails()
            {
                Name = regionEndpoint.SystemName,
                Region = region,
                Instances = reservations.SelectMany(x => x.Instances).Select(x => x.ToInstanceInfo()).ToList()
            };
        }
예제 #5
0
        public byte[] DownloadData(string bucketName, string keyName, AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = s3Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonS3Client(credentials, regionEndpoint));

            // download the data
            using (var stream = client.GetObject(bucketName, keyName).ResponseStream)
            {
                return(ReadFully(stream));
            }
        }
예제 #6
0
        public byte[] DownloadData(string bucketName, string keyName, AwsRegionLocations region)
        {
            var regionEndpoint = region.ToAwsRegionEndpoint();

            // Get an Ec2Client for the current region
            var client = s3Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonS3Client(credentials, regionEndpoint));

            // download the data
            using (var stream = client.GetObject(bucketName, keyName).ResponseStream)
            {
                return ReadFully(stream);
            }
        }
예제 #7
0
        private void CreateAndLaunchInstance(AwsRegionLocations region)
        {
            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var securityGroupId      = EnsureSecurityGroupExists(region);
            var availableSubnets     = client.DescribeSubnets().Subnets.OrderByDescending(x => x.AvailableIpAddressCount);
            var networkSpecification = new InstanceNetworkInterfaceSpecification()
            {
                DeviceIndex = 0,
                SubnetId    = availableSubnets.First().SubnetId,
                Groups      = new List <string>()
                {
                    securityGroupId
                },
                AssociatePublicIpAddress = true
            };
            var networkSpecifications = new List <InstanceNetworkInterfaceSpecification>()
            {
                networkSpecification
            };

            var launchRequest = new RunInstancesRequest()
            {
                ImageId           = GetAmiId(client, amiName),
                InstanceType      = "t2.micro",
                MinCount          = 1,
                MaxCount          = 1,
                KeyName           = keyPairName,
                NetworkInterfaces = networkSpecifications
            };

            client.RunInstances(launchRequest);
        }
예제 #8
0
        private void TerminateInstance(AwsRegionLocations region, string instanceId)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var deleteRequest = new TerminateInstancesRequest()
            {
                InstanceIds = new List <string>()
                {
                    instanceId
                }
            };

            client.TerminateInstances(deleteRequest);
        }
예제 #9
0
        private string FindNewestInstanceId(AwsRegionLocations region)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" state.
            request.Filters.Add(new Filter("instance-state-code", new List <string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            var instances = reservations.SelectMany(x => x.Instances);

            if (instances.Any())
            {
                var newestInstance = instances.OrderByDescending(i => i.LaunchTime).First();
                return(newestInstance.InstanceId);
            }
            return(null);
        }
예제 #10
0
        private void CreateAndLaunchInstance(AwsRegionLocations region)
        {
            // Get an Ec2Client for the current region
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var securityGroupId = EnsureSecurityGroupExists(region);
            var availableSubnets = client.DescribeSubnets().Subnets.OrderByDescending(x => x.AvailableIpAddressCount);
            var networkSpecification = new InstanceNetworkInterfaceSpecification()
            {
                DeviceIndex = 0,
                SubnetId = availableSubnets.First().SubnetId,
                Groups = new List<string>() { securityGroupId },
                AssociatePublicIpAddress = true
            };
            var networkSpecifications = new List<InstanceNetworkInterfaceSpecification>() { networkSpecification };

            var launchRequest = new RunInstancesRequest()
            {
                ImageId = GetAmiId(client, amiName),
                InstanceType = "t2.micro",
                MinCount = 1,
                MaxCount = 1,
                KeyName = keyPairName,
                NetworkInterfaces = networkSpecifications
            };

            client.RunInstances(launchRequest);
        }
예제 #11
0
        private void TerminateInstance(AwsRegionLocations region, string instanceId)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            var deleteRequest = new TerminateInstancesRequest()
            {
                InstanceIds = new List<string>() { instanceId }
            };

            client.TerminateInstances(deleteRequest);
        }
예제 #12
0
        private string FindNewestInstanceId(AwsRegionLocations region)
        {
            var client = ec2Clients.GetOrAdd(region, r => AWSClientFactory.CreateAmazonEC2Client(credentials, region.ToAwsRegionEndpoint()));

            // Get instances within the region
            // Start by creating the request
            var request = new DescribeInstancesRequest();

            // Add a filter to the request so that it only returns instances that are either in "Running" state.
            request.Filters.Add(new Filter("instance-state-code", new List<string>()
            {
                ((ushort)InstanceStatuses.Running).ToString(),
            }));

            // Send the request to Amazon
            var reservations = client.DescribeInstances(request).Reservations;

            var instances = reservations.SelectMany(x => x.Instances);
            if (instances.Any())
            {
                var newestInstance = instances.OrderByDescending(i => i.LaunchTime).First();
                return newestInstance.InstanceId;
            }
            return null;
        }