|
1 | | -# lambda-dotnet |
2 | | -A .NET library for interacting with the [LambdaLabs](https://lambdalabs.com/) Cloud API. |
| 1 | +# lambda-dotnet [](https://github.com/patchoulish/lambda-dotnet/actions/workflows/ci.yml) |
| 2 | +A .NET library for interacting with the [Lambda](https://lambdalabs.com/) API. |
| 3 | + |
| 4 | + |
| 5 | +## Installation |
| 6 | +Install the library via [NuGet](https://www.nuget.org/packages/lambda-dotnet): |
| 7 | +```bash |
| 8 | +dotnet add package lambda-dotnet |
| 9 | +``` |
| 10 | + |
| 11 | +### Extensions |
| 12 | +Install optional library extensions for more functionality, depending on your use case. |
| 13 | +#### Dependency Injection |
| 14 | +Integrate lambda-dotnet and your DI container of choice. Install the extension library via [NuGet](https://www.nuget.org/packages/lambda-dotnet-dependencyinjection): |
| 15 | +```bash |
| 16 | +dotnet add package lambda-dotnet-dependencyinjection |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | +## Usage |
| 21 | +1. Obtain an API key from the [Lambda Cloud Dashboard](https://cloud.lambdalabs.com/api-keys) (requires a Lambda account). |
| 22 | +2. Pass the API key into a new instance of the `LambdaCloudService` class or use a configured `HttpClient` if advanced configuration (e.g., proxies) is required. |
| 23 | +3. Use the methods available on `LambdaCloudService` to interact with the Lambda Cloud API. |
| 24 | + |
| 25 | +### Initialization |
| 26 | +The library can be initialized in three ways: |
| 27 | +#### Basic Initialization |
| 28 | +Pass in your API key directly: |
| 29 | +```csharp |
| 30 | +var lambdaCloud = new LambdaCloudService("YOUR_LAMBDA_API_KEY"); |
| 31 | +``` |
| 32 | +#### Advanced Initialization |
| 33 | +Use an existing `HttpClient`, ensuring that `BaseAddress` and an `Authorization` header have been set: |
| 34 | +```csharp |
| 35 | +var httpClient = new HttpClient |
| 36 | +{ |
| 37 | + BaseAddress = new Uri("https://cloud.lambdalabs.com/api/v1/"), |
| 38 | + Timeout = TimeSpan.FromSeconds(5) |
| 39 | +}; |
| 40 | + |
| 41 | +httpClient.DefaultRequestHeaders.Authorization = |
| 42 | + new AuthenticationHeaderValue("Bearer", "YOUR_LAMBDA_API_KEY"); |
| 43 | + |
| 44 | +var lambdaCloud = new LambdaCloudService(httpClient); |
| 45 | +``` |
| 46 | +#### Dependency Injection |
| 47 | +If you've installed the appropriate extension library. |
| 48 | +1. Register `LambdaCloudService` with your dependency container: |
| 49 | +```csharp |
| 50 | +services.AddLambdaCloudHttpClient(options => |
| 51 | +{ |
| 52 | + options.BaseUrl = new Uri("https://cloud.lambdalabs.com/api/v1/"); |
| 53 | + options.ApiKey = "YOUR_LAMBDA_API_KEY"; |
| 54 | +}); |
| 55 | +``` |
| 56 | +2. Inject `ILambdaCloudService` where needed: |
| 57 | +```csharp |
| 58 | +public class MyClass |
| 59 | +{ |
| 60 | + private readonly ILambdaCloudService lambdaCloud; |
| 61 | + |
| 62 | + public MyClass(ILambdaCloudService lambdaCloud) |
| 63 | + { |
| 64 | + this.lambdaCloud = lambdaCloud; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | +### List Instances |
| 69 | +To list your running instances: |
| 70 | +```csharp |
| 71 | +var instances = await lambdaCloud.Instances.GetAllAsync(); |
| 72 | +``` |
| 73 | +To retrieve the details of an instance: |
| 74 | +```csharp |
| 75 | +var instanceId = ... |
| 76 | + |
| 77 | +var instance = await lambdaCloud.Instances.GetAsync(instanceId); |
| 78 | +``` |
| 79 | + |
| 80 | +### List Instance Types |
| 81 | +To list the instance types offered by Lambda Cloud and explore their specs as well as their region-specific availability: |
| 82 | +```csharp |
| 83 | +var instanceTypeAvailabilities = lambdaCloud.Instances.GetAllTypeAvailabilityAsync(); |
| 84 | + |
| 85 | +var instanceTypesToLaunch = instanceTypeAvailabilities |
| 86 | + .Where(x => x.Type.Specifications.GpuCount >= 4) |
| 87 | + .Where(x => x.RegionsWithCapacity.Any()) |
| 88 | + .Select(x => x.Type) |
| 89 | +``` |
| 90 | + |
| 91 | +### Launching Instances |
| 92 | +You can launch an instance in the following way: |
| 93 | +```csharp |
| 94 | +var options = new LambdaCloudInstanceLaunchOptions() |
| 95 | +{ |
| 96 | + RegionName = "us-east-1", |
| 97 | + TypeName = "gpu_1x_a100_sxm4", |
| 98 | + KeyNames = [ |
| 99 | + "my-ssh-key" |
| 100 | + ], |
| 101 | + Quantity = 1 |
| 102 | +} |
| 103 | + |
| 104 | +var instance = await lambdaCloud.Instances.LaunchAsync(options); |
| 105 | +``` |
| 106 | + |
| 107 | +### Restarting or Terminating Instances |
| 108 | +To restart one or more running instances: |
| 109 | +```csharp |
| 110 | +var instances = ... |
| 111 | + |
| 112 | +var options = new LambdaCloudInstanceRestartOptions() |
| 113 | +{ |
| 114 | + Ids = [ instances[0].Id, instances[1].Id, ... ] |
| 115 | +} |
| 116 | + |
| 117 | +var restartedInstances = await lambdaCloud.RestartAsync(options); |
| 118 | +``` |
| 119 | +To terminate one or more running instances: |
| 120 | +```csharp |
| 121 | +var instances = ... |
| 122 | + |
| 123 | +var options = new LambdaCloudInstanceTerminateOptions() |
| 124 | +{ |
| 125 | + Ids = [ instances[0].Id, instances[1].Id, ... ] |
| 126 | +} |
| 127 | + |
| 128 | +var terminatedInstances = await lambdaCloud.TerminateAsync(options); |
| 129 | +``` |
| 130 | + |
| 131 | +### List SSH Keys |
| 132 | +To list the SSH keys saved in your account: |
| 133 | +```csharp |
| 134 | +var keys = await lambdaCloud.Keys.GetAllAsync(); |
| 135 | +``` |
| 136 | + |
| 137 | +### Adding or Generating a SSH Key |
| 138 | +To add an existing SSH key to your account: |
| 139 | +```csharp |
| 140 | +var options = new LambdaCloudKeyAddOrGenerateOptions() |
| 141 | +{ |
| 142 | + Name = "my-existing-key", |
| 143 | + PublicKey = "<YOUR_PUBLIC_KEY_HERE>" |
| 144 | +} |
| 145 | + |
| 146 | +var key = await lambdaCloud.Keys.AddOrGenerateAsync(options); |
| 147 | +``` |
| 148 | +To generate a new SSH key pair: |
| 149 | +```csharp |
| 150 | +var options = |
| 151 | + new LambdaCloudKeyAddOrGenerateOptions() |
| 152 | + { |
| 153 | + Name = "my-generated-key", |
| 154 | + PublicKey = null // Omit the public key |
| 155 | + } |
| 156 | + |
| 157 | +var key = await lambdaCloud.Keys.AddOrGenerateAsync(options); |
| 158 | + |
| 159 | +// Make sure to save the private key returned to you. |
| 160 | +await File.WriteAllTextAsync( |
| 161 | + "my-generated-key.pem", |
| 162 | + key.PrivateKey); |
| 163 | +``` |
| 164 | + |
| 165 | +### Deleting an SSH Key |
| 166 | +To delete an SSH key from your account: |
| 167 | +```csharp |
| 168 | +var key = ... |
| 169 | + |
| 170 | +await lambdaCloud.Keys.DeleteAsync(key.Id); |
| 171 | +``` |
| 172 | + |
| 173 | +### Listing Filesystems |
| 174 | +To list your persistent storage filesystems: |
| 175 | +```csharp |
| 176 | +var filesystems = await lambdaCloud.Filesystems.GetAllAsync(); |
| 177 | +``` |
| 178 | + |
| 179 | +## Documentation |
| 180 | +Refer to the [Usage](#usage) section above for a quick start, or consult the inline documentation while working in your IDE. |
| 181 | +For detailed information about the underlying API endpoints, parameters, and expected responses, refer to Lambda's [Cloud API documentation](https://docs.lambdalabs.com/public-cloud/cloud-api/) as well as their [Cloud API reference](https://cloud.lambdalabs.com/api/v1/docs). |
| 182 | + |
| 183 | + |
| 184 | +## Contributing |
| 185 | +Contributions are welcome! To contribute, fork the repository, create a new branch, and submit a pull request with your changes. Please make sure all tests pass before submitting. |
| 186 | + |
| 187 | + |
| 188 | +## License |
| 189 | +This project is licensed under the MIT license. See `license.txt` for full details. |
0 commit comments