Invalid Reference when Deploying Bot
# support
d
Hi there I am self-hosting and I'm having an issue deploying our first test bot. I'm running into an error when calling the
$deploy
function from the UI and it's returning "Invalid Reference". I think the issue is coming from how the
executableCode
value is stored. Looking at my bot in the UI and what is stored in the DB, I see this:
"executableCode": { "contentType": "text/typescript", "url": "https://medplum-storage.mydomain.com/signedcloudfronturl", "title": "index.js" } }
but looking at the deploy.test.js the bot is being created with
executableCode: { url: Binary/${binary.id} },
see https://github.com/medplum/medplum/blob/main/packages/server/src/fhir/operations/deploy.test.ts#L99 Given this difference it's pretty clear that when we call
Copy code
const binary = await repo.readReference<Binary>({
      reference: (bot.executableCode as Attachment).url as string,
    });
it will fail. I'm running on server version 4.4.1. Any advice on how to fix this?
Also I know that this is not the correct long-term path for production Bots but I'm trying to get a small bot out the door quickly
s
Hi Doug, just confirming: you're trying to deploy this to lambda rather than the vmcontext correct? And in the example above you've edited in the
medplum-storage.mydomain.com
it's not copy/pasted verbatim from the bot JSON right?
d
Yes and yes. The url in the
"url"
path opens up the
.js
file being served through cloudfront, and I have the following ENV vars setup:
Copy code
{
  "name": "MEDPLUM_BOT_LAMBDA_ROLE_ARN",
  "value": "arn:aws:iam::my-role"
},
{ "name": "MEDPLUM_BOT_LAMBDA_LAYER_NAME", "value": "medplum-bot-layer" },
{ "name": "MEDPLUM_VM_CONTEXT_BOTS_ENABLED", "value": "false" },
{ "name": "MEDPLUM_DEFAULT_BOT_RUNTIME_VERSION", "value": "awslambda" }
Although this is making me think to double check the lambda role is correct. Yesterday I dealt with a completely obscure error when uploading that turned out to be a lack of S3 permissions b/c I assigned them to my EC2 role instead of my ECS Task Role
Ok, I just re-verified that my task can
GetObject
from my s3 storage bucket and
ListLayerVersions
so it seems unlikely that this is a permissions issue. Looking at my staging ENV which is deployed to EC2 via docker-compose, it also has this same structure for the bots in the
executableCode
and is working... I don't understand how this works in the first place
m
Hi @Doug DeBold ! I think the issue here is that in the medplum UI, the binary reference in the bot is resolved into a URL, but in the DB, it is still stored as a reference to the Binary resource (something like the below). I think the deploy operation is failing because it's trying to read executableCode.url as a Reference, but it's getting the resolved signed URL instead. Is there a chance you manually updated the executable code rather than re-deploying or editing in the bot editor UI?
Copy code
{
  "contentType": "text/typescript",
  "url": "Binary/{binary-id}"
}
d
Hi Maddy, I suspected this as well and went into the DB and in the DB the
executableCode
is being stored the exact same as I'm seeing it in the UI
m
Ah I see, how did you deploy this bot that is failing initially? And was that deployment different than the bots that are working?
d
I have 2 deployments: - staging: Docker on EC2 -- bot deploys from the UI work - prod: ECS on EC2 -- no bot deploy has ever worked here
I'm considering just biting the bullet and adapting the CLI based deployment workflow to my needs so that I can put effort into a long-term solution instead of a short term fix
Ahh interesting, so my staging deployment which is running 4.1.9 has the expected
url: "Binary/:id"
syntax in the database... which explains why that is working. But my production deployment is storing the literal response from the creation of the file
The next interesting thing is that both UI's are sending effectively identical Patch requests. Both are sending:
Copy code
{ op: 'add', path: '/executableCode', value: { contentType: 'text/typescript', url: 'https://..', title: 'index.js }
So as best as I can tell, in version 4.1.9 the above patch is being mapped into
url: 'Binary/:id'
while in 4.4.1 it's not
m
Hm interesting, I would definitely encourage users to use our CLI or generally our $deploy operation to update their bots. Something like the below could be used if you would rather use FHIR url than a CLI command
Copy code
import { MedplumClient } from '@medplum/core';

const medplum = new MedplumClient();

// Deploy bot using the $deploy operation
const deployResult = await medplum.post(
  medplum.fhirUrl('Bot', botId, '$deploy'), 
  {
    code: botCode,
    filename: 'index.js'
  }
);
d
Yeah, I was hoping for a quick and dirty solution to get something out the door. I am now using the CLI directly from my machine and it worked and will worry about merging into our CI/CD pipeline later. On a similar note, I haven't found any good documentation on building rules for subscription criteria. Can you point me towards any?
m
@Doug DeBold not too much beyond the subscription extensions page https://www.medplum.com/docs/subscriptions/subscription-extensions
d
Thank you! I somehow missed this page
62 Views