Skip to content

Commit

Permalink
Fix output of deploy model lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Aug 14, 2024
1 parent 5cd1195 commit 97c7c84
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
48 changes: 44 additions & 4 deletions data_services/deploy_model_lambda/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const fetch = async (params) => {
});

const response = await awsFetch(request);
console.log(`${request.method} ${request.path}\n${response}`);
const { statusCode, body } = response;
if (statusCode >= 200 && statusCode <= 299) {
return JSON.parse(body);
Expand Down Expand Up @@ -79,6 +78,27 @@ const createConnector = async (connector_spec) => {
});
};

const updateConnector = async (connector_spec) => {
connector_id = await findExisting("connector", { name: connector_spec.name });
if (!connector_id) {
console.warn(`No existing connector named ${connector_spec.name}`);
return null;
}
model_id = await findExisting("model", { connector_id });
if (model_id) {
await undeployModel(model_id);
}
const result = await fetch({
method: "PUT",
path: `_plugins/_ml/connectors/${connector_id}`,
body: JSON.stringify(connector_spec)
});
if (model_id) {
await deployModel(model_id);
}
return { model_id };
};

const createModel = async (name, version, model_group_id, connector_id) => {
return await fetch({
path: "_plugins/_ml/models/_register",
Expand All @@ -94,6 +114,20 @@ const createModel = async (name, version, model_group_id, connector_id) => {
});
};

const deployModel = async (model_id) => {
return await fetch({
method: "POST",
path: `_plugins/_ml/models/${model_id}/_deploy`
});
};

const undeployModel = async (model_id) => {
return await fetch({
method: "POST",
path: `_plugins/_ml/models/${model_id}/_undeploy`
});
};

const create = async (event) => {
const { model_group_id } = await createModelGroup(event.namespace);
const { connector_id } = await createConnector(event.connector_spec);
Expand Down Expand Up @@ -134,13 +168,13 @@ const destroy = async (event) => {
};

const update = async (event) => {
await destroy(event.tf.prev_input);
return await create(event);
return await updateConnector(event.connector_spec);
};

const handler = async (event) => {
try {
let body;
console.log("Handling", event.tf.action, "event");
switch (event.tf.action) {
case "create":
body = await create(event);
Expand All @@ -158,4 +192,10 @@ const handler = async (event) => {
}
};

module.exports = { handler };
module.exports = {
handler,
findExisting,
deployModel,
undeployModel,
updateConnector
};
6 changes: 6 additions & 0 deletions data_services/opensearch.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ resource "aws_opensearch_domain" "elasticsearch" {
"rest.action.multi.allow_explicit_index" = "true"
}

log_publishing_options {
cloudwatch_log_group_arn = "arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current_user.account_id}:log-group:/aws/OpenSearchService/domains/${local.namespace}-index/application-logs"
enabled = true
log_type = "ES_APPLICATION_LOGS"
}

cluster_config {
instance_type = "m6g.large.search"
instance_count = var.opensearch_cluster_nodes
Expand Down
8 changes: 7 additions & 1 deletion data_services/opensearch_connector.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ locals {
request_body = "{\"texts\": $${parameters.input}, \"input_type\": \"search_document\"}"
}
]

client_config = {
max_connection = 200
connection_timeout = 5000
read_timeout = 60000
}
}
}

Expand Down Expand Up @@ -79,7 +85,7 @@ data "aws_iam_policy_document" "deploy_model_lambda" {

statement {
effect = "Allow"
actions = ["es:ESHttpGet", "es:ESHttpPost"]
actions = ["es:ESHttp*"]
resources = ["${aws_opensearch_domain.elasticsearch.arn}/*"]
}
}
Expand Down

0 comments on commit 97c7c84

Please sign in to comment.