Skip to content

Commit 8571e0c

Browse files
feat: Add key management
1 parent c6c38ab commit 8571e0c

File tree

13 files changed

+1156
-209
lines changed

13 files changed

+1156
-209
lines changed

README.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LiteLLM Terraform Provider
22

3-
This Terraform provider allows you to manage LiteLLM resources through Infrastructure as Code. It provides support for managing models, teams, and team members via the LiteLLM REST API.
3+
This Terraform provider allows you to manage LiteLLM resources through Infrastructure as Code. It provides support for managing models, teams, team members, and API keys via the LiteLLM REST API.
44

55
## Features
66

@@ -10,6 +10,7 @@ This Terraform provider allows you to manage LiteLLM resources through Infrastru
1010
- Set usage limits and budgets
1111
- Control access to specific models
1212
- Specify model modes (e.g., completion, embeddings, image generation)
13+
- Manage API keys with fine-grained controls
1314

1415
## Requirements
1516

@@ -55,11 +56,78 @@ resource "litellm_model" "gpt4" {
5556

5657
For full details on the `litellm_model` resource, see the [model resource documentation](docs/resources/model.md).
5758

59+
Here's an example of creating an API key with various options:
60+
61+
```hcl
62+
resource "litellm_key" "example_key" {
63+
models = ["gpt-4", "claude-3.5-sonnet"]
64+
max_budget = 100.0
65+
user_id = "user123"
66+
team_id = "team456"
67+
max_parallel_requests = 5
68+
tpm_limit = 1000
69+
rpm_limit = 60
70+
budget_duration = "monthly"
71+
key_alias = "prod-key-1"
72+
duration = "30d"
73+
metadata = {
74+
environment = "production"
75+
}
76+
allowed_cache_controls = ["no-cache", "max-age=3600"]
77+
soft_budget = 80.0
78+
aliases = {
79+
"gpt-4" = "gpt4"
80+
}
81+
config = {
82+
default_model = "gpt-4"
83+
}
84+
permissions = {
85+
can_create_keys = "true"
86+
}
87+
model_max_budget = {
88+
"gpt-4" = 50.0
89+
}
90+
model_rpm_limit = {
91+
"claude-3.5-sonnet" = 30
92+
}
93+
model_tpm_limit = {
94+
"gpt-4" = 500
95+
}
96+
guardrails = ["content_filter", "token_limit"]
97+
blocked = false
98+
tags = ["production", "api"]
99+
}
100+
```
101+
102+
The `litellm_key` resource supports the following options:
103+
104+
- `models`: List of allowed models for this key
105+
- `max_budget`: Maximum budget for the key
106+
- `user_id` and `team_id`: Associate the key with a user and team
107+
- `max_parallel_requests`: Limit concurrent requests
108+
- `tpm_limit` and `rpm_limit`: Set tokens and requests per minute limits
109+
- `budget_duration`: Specify budget duration (e.g., "monthly", "weekly")
110+
- `key_alias`: Set a friendly name for the key
111+
- `duration`: Set the key's validity period
112+
- `metadata`: Add custom metadata to the key
113+
- `allowed_cache_controls`: Specify allowed cache control directives
114+
- `soft_budget`: Set a soft budget limit
115+
- `aliases`: Define model aliases
116+
- `config`: Set configuration options
117+
- `permissions`: Specify key permissions
118+
- `model_max_budget`, `model_rpm_limit`, `model_tpm_limit`: Set per-model limits
119+
- `guardrails`: Apply specific guardrails to the key
120+
- `blocked`: Flag to block/unblock the key
121+
- `tags`: Add tags for organization and filtering
122+
123+
For full details on the `litellm_key` resource, see the [key resource documentation](docs/resources/key.md).
124+
58125
### Available Resources
59126

60127
- `litellm_model`: Manage model configurations. [Documentation](docs/resources/model.md)
61128
- `litellm_team`: Manage teams. [Documentation](docs/resources/team.md)
62129
- `litellm_team_member`: Manage team members. [Documentation](docs/resources/team_member.md)
130+
- `litellm_key`: Manage API keys. [Documentation](docs/resources/key.md)
63131

64132
## Development
65133

@@ -73,6 +141,10 @@ terraform-provider-litellm/
73141
│ ├── provider.go
74142
│ ├── resource_model.go
75143
│ ├── resource_model_crud.go
144+
│ ├── resource_team.go
145+
│ ├── resource_team_member.go
146+
│ ├── resource_key.go
147+
│ ├── resource_key_utils.go
76148
│ ├── types.go
77149
│ └── utils.go
78150
├── main.go
@@ -132,3 +204,4 @@ This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENS
132204
- Always use environment variables or secure secret management solutions to handle sensitive information like API keys and AWS credentials.
133205
- Refer to the `examples/` directory for more detailed usage examples.
134206
- Make sure to keep your provider version updated for the latest features and bug fixes.
207+
- Recent updates have improved the handling of the Key resource to ensure all values are correctly persisted in the Terraform state.

client.go

Lines changed: 0 additions & 105 deletions
This file was deleted.

docs/resources/key.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# litellm_key Resource
2+
3+
Manages a LiteLLM API key.
4+
5+
## Example Usage
6+
7+
```hcl
8+
resource "litellm_key" "example" {
9+
models = ["gpt-3.5-turbo", "gpt-4"]
10+
max_budget = 100.0
11+
user_id = "user123"
12+
team_id = "team456"
13+
max_parallel_requests = 5
14+
metadata = {
15+
"environment" = "production"
16+
}
17+
tpm_limit = 1000
18+
rpm_limit = 60
19+
budget_duration = "monthly"
20+
allowed_cache_controls = ["no-cache", "max-age=3600"]
21+
soft_budget = 80.0
22+
key_alias = "prod-key-1"
23+
duration = "30d"
24+
aliases = {
25+
"gpt-3.5-turbo" = "chatgpt"
26+
}
27+
config = {
28+
"default_model" = "gpt-3.5-turbo"
29+
}
30+
permissions = {
31+
"can_create_keys" = "true"
32+
}
33+
model_max_budget = {
34+
"gpt-4" = 50.0
35+
}
36+
model_rpm_limit = {
37+
"gpt-3.5-turbo" = 30
38+
}
39+
model_tpm_limit = {
40+
"gpt-4" = 500
41+
}
42+
guardrails = ["content_filter", "token_limit"]
43+
blocked = false
44+
tags = ["production", "api"]
45+
}
46+
```
47+
48+
## Argument Reference
49+
50+
The following arguments are supported:
51+
52+
* `models` - (Optional) List of models that can be used with this key. This restricts the key to only use the specified models.
53+
54+
* `max_budget` - (Optional) Maximum budget for this key. This sets an upper limit on the total spend allowed for this key.
55+
56+
* `user_id` - (Optional) User ID associated with this key. This links the key to a specific user in the LiteLLM system.
57+
58+
* `team_id` - (Optional) Team ID associated with this key. This links the key to a specific team in the LiteLLM system.
59+
60+
* `max_parallel_requests` - (Optional) Maximum number of parallel requests allowed for this key. This helps in controlling concurrent usage.
61+
62+
* `metadata` - (Optional) Metadata associated with this key. This can be used to store additional, custom information about the key.
63+
64+
* `tpm_limit` - (Optional) Tokens per minute limit for this key. This sets a rate limit based on the number of tokens processed.
65+
66+
* `rpm_limit` - (Optional) Requests per minute limit for this key. This sets a rate limit based on the number of API calls.
67+
68+
* `budget_duration` - (Optional) Duration for the budget (e.g., "monthly", "weekly"). This defines the time period for which the `max_budget` applies.
69+
70+
* `allowed_cache_controls` - (Optional) List of allowed cache control directives. This can be used to control caching behavior for requests made with this key.
71+
72+
* `soft_budget` - (Optional) Soft budget limit for this key. This can be used to set a warning threshold before reaching the `max_budget`.
73+
74+
* `key_alias` - (Optional) Alias for this key. This provides a human-readable identifier for the key.
75+
76+
* `duration` - (Optional) Duration for which this key is valid. This sets an expiration time for the key.
77+
78+
* `aliases` - (Optional) Map of model aliases. This allows you to create custom names for models when using this key.
79+
80+
* `config` - (Optional) Configuration options for this key. This can be used to set key-specific settings.
81+
82+
* `permissions` - (Optional) Permissions associated with this key. This defines what actions are allowed with this key.
83+
84+
* `model_max_budget` - (Optional) Maximum budget per model. This allows setting different budget limits for each model.
85+
86+
* `model_rpm_limit` - (Optional) Requests per minute limit per model. This allows setting different RPM limits for each model.
87+
88+
* `model_tpm_limit` - (Optional) Tokens per minute limit per model. This allows setting different TPM limits for each model.
89+
90+
* `guardrails` - (Optional) List of guardrails applied to this key. This can be used to enforce certain safety or quality checks.
91+
92+
* `blocked` - (Optional) Whether this key is blocked. If set to true, the key will be unable to make any requests.
93+
94+
* `tags` - (Optional) List of tags associated with this key. This can be used for organization and filtering of keys.
95+
96+
## Attribute Reference
97+
98+
In addition to all arguments above, the following attributes are exported:
99+
100+
* `key` - The generated API key. This is the actual key value that will be used for authentication.
101+
102+
* `spend` - The current spend for this key. This reflects the total amount spent using this key so far.
103+
104+
## State Management
105+
106+
Recent updates have improved how the Key resource manages its state. The provider now ensures that all non-zero and non-empty values are correctly persisted in the Terraform state file. This means that any value you set will be accurately reflected in your state, preventing unnecessary updates and ensuring consistency between your configuration and the actual resource state.
107+
108+
## Import
109+
110+
LiteLLM keys can be imported using the `id`, e.g.,
111+
112+
```
113+
$ terraform import litellm_key.example 12345
114+
```
115+
116+
This allows you to import existing keys into your Terraform state, enabling management of keys that were created outside of Terraform.

0 commit comments

Comments
 (0)