public Task <RunInstancesResponse> CreateInstanceAsync( string imageId, InstanceType instanceType, string keyName, string securityGroupId, string subnetId, string roleName, ShutdownBehavior shutdownBehavior, bool associatePublicIpAddress, Dictionary <string, string> tags, bool ebsOptymalized = false, CancellationToken cancellationToken = default(CancellationToken)) => _client.RunInstancesAsync(new RunInstancesRequest() { ImageId = imageId, InstanceType = instanceType, MinCount = 1, MaxCount = 1, KeyName = keyName, InstanceInitiatedShutdownBehavior = shutdownBehavior, DisableApiTermination = false, IamInstanceProfile = roleName.IsNullOrEmpty() ? null : new IamInstanceProfileSpecification() { Name = roleName }, NetworkInterfaces = new List <InstanceNetworkInterfaceSpecification>() { new InstanceNetworkInterfaceSpecification() { DeviceIndex = 0, SubnetId = subnetId, Groups = new List <string>() { securityGroupId }, AssociatePublicIpAddress = associatePublicIpAddress, Description = "Primary network interface", DeleteOnTermination = true, } }, TagSpecifications = new List <TagSpecification>() { new TagSpecification() { ResourceType = ResourceType.Instance, Tags = tags.Select(x => new Tag() { Key = x.Key, Value = x.Value }).ToList() } }, EbsOptimized = ebsOptymalized }, cancellationToken).EnsureSuccessAsync();
/// <summary> /// For root device naming scheme check: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html /// IOPS range: 100-10'000 GP2, 100-20'000 IO1 /// </summary> public async Task <RunInstancesResponse> CreateInstanceAsync( string imageId, InstanceType instanceType, string keyName, IEnumerable <string> securityGroupIDs, string subnetId, string roleName, ShutdownBehavior shutdownBehavior, bool associatePublicIpAddress, IDictionary <string, string> tags, bool ebsOptymalized, int rootVolumeSize, string rootDeviceName = "/dev/sd1", string rootVolumeType = "GP2", string rootSnapshotId = null, int rootIOPS = 100, bool monitoring = false, string kmsKeyId = null, CancellationToken cancellationToken = default(CancellationToken)) { if (securityGroupIDs.IsNullOrEmpty()) { throw new Exception($"Paramer '{nameof(securityGroupIDs)}' was not specified, can't create new instance."); } var sgrups = new List <string>(); var allSGroups = await DescribeSecurityGroupsAsync(cancellationToken : cancellationToken); if (!allSGroups.IsNullOrEmpty()) { foreach (var sg in securityGroupIDs) { var sgname = sg?.Trim()?.ToLower(); if (sgname.IsNullOrEmpty()) { continue; } var sgroup = allSGroups.FirstOrDefault(x => x.GroupId == sgname); if (sgroup == null) { sgroup = allSGroups.FirstOrDefault(x => x?.GroupName?.Trim()?.ToLower() == sgname); } if (sgroup != null) { sgrups.Add(sgroup.GroupId); } } } sgrups = sgrups.Distinct().ToList(); if (sgrups.IsNullOrEmpty()) { throw new Exception($"No security groups with following names nor id's were found '{securityGroupIDs?.JsonSerialize() ?? "undefined"}'"); } var volumeType = VolumeType.FindValue(rootVolumeType); EbsBlockDevice ebs; if (volumeType == VolumeType.Io1) { ebs = new EbsBlockDevice() { DeleteOnTermination = true, VolumeType = volumeType, Iops = rootIOPS, VolumeSize = rootVolumeSize, SnapshotId = rootSnapshotId, Encrypted = !kmsKeyId.IsNullOrEmpty(), KmsKeyId = kmsKeyId }; } else { ebs = new EbsBlockDevice() { DeleteOnTermination = true, VolumeType = volumeType, VolumeSize = rootVolumeSize, SnapshotId = rootSnapshotId, Encrypted = !kmsKeyId.IsNullOrEmpty(), KmsKeyId = kmsKeyId }; } var result = await _client.RunInstancesAsync(new RunInstancesRequest() { ImageId = imageId, InstanceType = instanceType, MinCount = 1, MaxCount = 1, KeyName = keyName, InstanceInitiatedShutdownBehavior = shutdownBehavior, DisableApiTermination = false, IamInstanceProfile = roleName.IsNullOrEmpty() ? null : new IamInstanceProfileSpecification() { Name = roleName }, NetworkInterfaces = new List <InstanceNetworkInterfaceSpecification>() { new InstanceNetworkInterfaceSpecification() { DeviceIndex = 0, SubnetId = subnetId, Groups = sgrups, AssociatePublicIpAddress = associatePublicIpAddress, Description = "Primary network interface", DeleteOnTermination = true, } }, TagSpecifications = new List <TagSpecification>() { new TagSpecification() { ResourceType = ResourceType.Instance, Tags = tags.Select(x => new Tag() { Key = x.Key, Value = x.Value }).ToList() } }, EbsOptimized = ebsOptymalized, BlockDeviceMappings = new List <BlockDeviceMapping>() { new BlockDeviceMapping() { DeviceName = rootDeviceName, Ebs = ebs, } }, CreditSpecification = new CreditSpecificationRequest() { CpuCredits = "unlimited" }, Monitoring = monitoring, }, cancellationToken).EnsureSuccessAsync(); return(result); }