Skip to content

Commit 1af989c

Browse files
authored
feat: support API key for model accessing (#118)
* feat: add api key for model invoking * fix: go mod update * fix: update permissions * feat: add output for api-key * fix: update output and cleanup * fix: update descriptions * fix: update status output * chore: cleanup
1 parent 11d3679 commit 1af989c

File tree

12 files changed

+448
-112
lines changed

12 files changed

+448
-112
lines changed

docs/en/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Extra parameters passed to the model deployment. extra-params should be a Json o
4949
}
5050
}
5151
```
52-
To learn some practice examples, please refer to the [Best Deployment Practices](docs/en/best_deployment_practices.md).
52+
To learn some practice examples, please refer to the [Best Deployment Practices](https://aws-samples.github.io/easy-model-deployer/en/best_deployment_practices/).
5353

5454

5555

src/emd/cfn/codepipeline/template.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ Resources:
6464
- logs:*
6565
- elasticloadbalancing:*
6666
- application-autoscaling:*
67+
- secretsmanager:GetSecretValue
68+
- secretsmanager:PutSecretValue
69+
- secretsmanager:CreateSecret
70+
- secretsmanager:UpdateSecret
6771
Resource:
6872
- "*"
6973
ManagedPolicyArns:
@@ -246,6 +250,12 @@ Resources:
246250
- parameters.json
247251
- template.yaml
248252
253+
EMDAPIKeySecret:
254+
Type: AWS::SecretsManager::Secret
255+
Properties:
256+
Name: EMD-APIKey-Secrets
257+
Description: Easy Model Deployer model API keys secrets
258+
249259
Pipeline:
250260
Type: AWS::CodePipeline::Pipeline
251261
Properties:

src/emd/cfn/ecs/template.yaml

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ Parameters:
8686
Region:
8787
Type: String
8888
Description: Not used currently
89+
APIKey:
90+
Type: String
91+
Default: ""
92+
NoEcho: true
93+
Description: API key to be used for invoking the model
94+
95+
Conditions:
96+
HasAPIKey: !Not [!Equals [!Ref APIKey, ""]]
97+
IsChinaRegion: !Or
98+
- !Equals [!Ref "AWS::Region", "cn-north-1"]
99+
- !Equals [!Ref "AWS::Region", "cn-northwest-1"]
100+
89101
Resources:
90102
ECSAutoScalingGroup:
91103
Type: AWS::AutoScaling::AutoScalingGroup
@@ -174,6 +186,13 @@ Resources:
174186
Properties:
175187
ServiceToken: !Ref LambdaDeploymentHelperArn
176188
ModelName: !Join ['', [!Ref ModelId, '/', !Ref ModelTag]]
189+
SetApiKey:
190+
Type: Custom::SetApiKey
191+
Condition: HasAPIKey
192+
Properties:
193+
ServiceToken: !Ref LambdaDeploymentHelperArn
194+
ModelName: !Join ['', [!Ref ModelId, '/', !Ref ModelTag]]
195+
APIKey: !Ref APIKey
177196

178197
ContainerHostSecurityGroup:
179198
Type: AWS::EC2::SecurityGroup
@@ -369,25 +388,6 @@ Resources:
369388
GroupId: !Ref ServiceSecurityGroup
370389
IpProtocol: -1
371390
SourceSecurityGroupId: !Ref PublicLoadBalancerSecurityGroup
372-
ListenerRulePath:
373-
Type: AWS::ElasticLoadBalancingV2::ListenerRule
374-
Properties:
375-
ListenerArn: !Ref PublicLoadBalancerListenerArn
376-
Priority: !GetAtt GetPriorityNumber.Priority
377-
Conditions:
378-
- Field: path-pattern
379-
PathPatternConfig:
380-
Values:
381-
- !Join
382-
- ''
383-
- - '/'
384-
- !Ref ModelId
385-
- '/'
386-
- !Ref ModelTag
387-
- '/*'
388-
Actions:
389-
- Type: forward
390-
TargetGroupArn: !Ref ServiceTargetGroup
391391

392392
ForceApiRouterDeployment:
393393
Type: Custom::ForceApiRouterDeployment
@@ -397,11 +397,18 @@ Resources:
397397

398398
Outputs:
399399
Model:
400-
Description: Model ID used to generate the response.
400+
Description: The unique model ID that determines which model will process your request in the OpenAI-compatible API.
401401
Value: !Join ['', [!Ref ModelId, '/', !Ref ModelTag]]
402402
BaseURL:
403403
Description: Please use this URL for the OpenAI-compatible model API, like /v1/chat/completions. To use HTTPS, create an SSL certificate in AWS Certificate Manager and attach it to the load balancer.
404404
Value: !Join ['', ['http://', !Ref DNSName]]
405405
ECSServiceConnect:
406406
Description: Service Connect allows for service-to-service communications with automatic discovery using short names and standard ports.
407407
Value: !Join ['', ['http://', !GetAtt ConvertDnsName.DnsName, ':', !Ref ContainerPort]]
408+
ModelAPIKey:
409+
Condition: HasAPIKey
410+
Description: "API key for accessing model is securely stored in AWS Secrets Manager and can be managed through the provided link."
411+
Value: !If
412+
- IsChinaRegion
413+
- !Sub "https://console.amazonaws.cn/secretsmanager/secret?name=EMD-APIKey-Secrets&region=${AWS::Region}"
414+
- !Sub "https://console.aws.amazon.com/secretsmanager/secret?name=EMD-APIKey-Secrets&region=${AWS::Region}"

src/emd/cfn/sagemaker_async/template.yaml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ Parameters:
2929
Region:
3030
Type: String
3131
Description: The region to be used for the SageMaker Endpoint
32+
LambdaDeploymentHelperArn:
33+
Type: String
34+
Description: The ARN of the Lambda function for capacity provider association
35+
APIKey:
36+
Type: String
37+
Default: ""
38+
NoEcho: true
39+
Description: API key to be used for invoking the model
40+
41+
Conditions:
42+
HasAPIKey: !Not [!Equals [!Ref APIKey, ""]]
43+
IsChinaRegion: !Or
44+
- !Equals [!Ref "AWS::Region", "cn-north-1"]
45+
- !Equals [!Ref "AWS::Region", "cn-northwest-1"]
46+
3247
Resources:
3348
ExecutionRole:
3449
Type: AWS::IAM::Role
@@ -92,10 +107,25 @@ Resources:
92107
EndpointName: !Sub '${AWS::StackName}-endpoint'
93108
EndpointConfigName: !GetAtt SageMakerEndpointConfig.EndpointConfigName
94109

110+
SetApiKey:
111+
Type: Custom::SetApiKey
112+
Condition: HasAPIKey
113+
Properties:
114+
ServiceToken: !Ref LambdaDeploymentHelperArn
115+
ModelName: !Join ['', [!Ref ModelId, '/', !Ref ModelTag]]
116+
APIKey: !Ref APIKey
117+
95118
Outputs:
96119
Model:
97-
Description: Model ID used to generate the response
120+
Description: The unique model ID that determines which model will process your request in the OpenAI-compatible API.
98121
Value: !Join ['', [!Ref ModelId, '/', !Ref ModelTag]]
99122
SageMakerEndpointName:
100123
Description: The name of the SageMaker Endpoint
101124
Value: !GetAtt SageMakerEndpoint.EndpointName
125+
ModelAPIKey:
126+
Condition: HasAPIKey
127+
Description: "API key for accessing model is securely stored in AWS Secrets Manager and can be managed through the provided link."
128+
Value: !If
129+
- IsChinaRegion
130+
- !Sub "https://console.amazonaws.cn/secretsmanager/secret?name=EMD-APIKey-Secrets&region=${AWS::Region}"
131+
- !Sub "https://console.aws.amazon.com/secretsmanager/secret?name=EMD-APIKey-Secrets&region=${AWS::Region}"

src/emd/cfn/sagemaker_realtime/template.yaml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,21 @@ Parameters:
4545
DNSName:
4646
Type: String
4747
Description: The DNS name of the public load balancer. To use HTTPS, create an SSL certificate in AWS Certificate Manager and attach it to the load balancer.
48+
LambdaDeploymentHelperArn:
49+
Type: String
50+
Description: The ARN of the Lambda function for capacity provider association
51+
APIKey:
52+
Type: String
53+
Default: ""
54+
NoEcho: true
55+
Description: API key to be used for invoking the model
4856

4957
Conditions:
5058
UseDefaultEndpointName: !Equals [!Ref SageMakerEndpointName, "Auto-generate"]
59+
HasAPIKey: !Not [!Equals [!Ref APIKey, ""]]
60+
IsChinaRegion: !Or
61+
- !Equals [!Ref "AWS::Region", "cn-north-1"]
62+
- !Equals [!Ref "AWS::Region", "cn-northwest-1"]
5163

5264
Resources:
5365
ExecutionRole:
@@ -154,13 +166,28 @@ Resources:
154166
ScaleInCooldown: 600
155167
ScaleOutCooldown: 600
156168

169+
SetApiKey:
170+
Type: Custom::SetApiKey
171+
Condition: HasAPIKey
172+
Properties:
173+
ServiceToken: !Ref LambdaDeploymentHelperArn
174+
ModelName: !Join ['', [!Ref ModelId, '/', !Ref ModelTag]]
175+
APIKey: !Ref APIKey
176+
157177
Outputs:
158178
Model:
159-
Description: Model ID used to generate the response.
179+
Description: The unique model ID that determines which model will process your request in the OpenAI-compatible API.
160180
Value: !Join ['', [!Ref ModelId, '/', !Ref ModelTag]]
161181
BaseURL:
162182
Description: Please use this URL for the OpenAI-compatible model API, like /v1/chat/completions. To use HTTPS, create an SSL certificate in AWS Certificate Manager and attach it to the load balancer.
163183
Value: !Join ['', ['http://', !Ref DNSName]]
164184
SageMakerEndpointName:
165185
Description: You can use invoke the endpoint by using the SageMaker runtime API.
166186
Value: !GetAtt SageMakerEndpoint.EndpointName
187+
ModelAPIKey:
188+
Condition: HasAPIKey
189+
Description: "API key for accessing model is securely stored in AWS Secrets Manager and can be managed through the provided link."
190+
Value: !If
191+
- IsChinaRegion
192+
- !Sub "https://console.amazonaws.cn/secretsmanager/secret?name=EMD-APIKey-Secrets&region=${AWS::Region}"
193+
- !Sub "https://console.aws.amazon.com/secretsmanager/secret?name=EMD-APIKey-Secrets&region=${AWS::Region}"

0 commit comments

Comments
 (0)