Exemple #1
0
        public static async Task <Instance> GetInstanceById(this EC2Helper ec2, string instanceId, bool throwIfNotFound = true, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (instanceId.IsNullOrEmpty())
            {
                throw new ArgumentException("instanceId was not defined");
            }

            var batch = await ec2.DescribeInstancesAsync(instanceIds : new List <string>()
            {
                instanceId
            }, filters : null, cancellationToken : cancellationToken);

            instanceId = instanceId?.Trim().ToLower();
            var instance = batch.SelectMany(x => x.Instances).FirstOrDefault(x => x?.InstanceId == instanceId);

            if (throwIfNotFound && instance == null)
            {
                throw new Exception($"Instance {instanceId ?? "undefined"} was not found.");
            }

            return(instance);
        }
Exemple #2
0
        public static async Task <Instance[]> ListInstancesByTagKey(this EC2Helper ec2, string tagKey, CancellationToken cancellationToken = default(CancellationToken))
        {
            var batch = await ec2.DescribeInstancesAsync(instanceIds : null, filters : null, cancellationToken : cancellationToken);

            return(batch.SelectMany(x => x.Instances).Where(x => (x?.Tags?.Any(t => t?.Key == tagKey) ?? false) == true).ToArray());
        }
Exemple #3
0
        public static async Task <Instance[]> ListInstances(this EC2Helper ec2, CancellationToken cancellationToken = default(CancellationToken))
        {
            var batch = await ec2.DescribeInstancesAsync(instanceIds : null, filters : null, cancellationToken : cancellationToken);

            return(batch.SelectMany(x => x.Instances).ToArray());
        }