Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Function with Cosmos DB trigger is not connecting with CosmosDB Emulator #1797

Closed
gmartinezsan opened this issue Dec 7, 2020 · 3 comments

Comments

@gmartinezsan
Copy link

hi Azure Functions team.
I have my Azure Function App with a CosmosDb Binding for that I'm using the CosmosDb emulator. Every time I run the Azure Function App with VS Code I got an error:

Failed to verify "AzureWebJobsStorage" connection specified in "local.settings.json". Is the local emulator installed and running?

I have followed the steps at the docs about using the CosmosDB Emulator in Mac (I'm using parallels).
The emulator runs fine in parallels.
In the Function App I'm using TLS and I have imported the certificate in Mac.
Steps are here

The settings of the app are like the following:

{
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "mydbconnection": "AccountEndpoint=https://###.##.#.#:8081/;AccountKey=%%$%$^^%;"
  },
  "IsEncrypted": false
}

This same app runs fine if it uses CosmosDB from the Azure Portal.

Is there anything else that should be done to make this setup work?

Thank you in advance.

@marcduiker
Copy link

marcduiker commented Dec 7, 2020

Hi @gmartinezsan,

I believe the UseDevelopmentStorage=true can only be used for emulation of a storage account (which contains, queues, blobs, tables). If you're not using any of the storage functionality, you could leave this empty for local development. If you do need the functionality you need to run Azurite (crossplatform) or the Azure Storage Emulator (Windows only & almost end of life).

To use CosmosDB emulator you can use this connectionstring when developing locally (and have the CosmosDb emulator installed and running):
AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

More info in https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator?tabs=cli%2Cssl-netstd21#authenticate-requests

@gmartinezsan
Copy link
Author

hi @marcduiker, my dev env runs on mac using VS Code, and the cosmos db emulator runs inside parallels with Windows 10, so my connection string is not using localhost but the IP of the guest Windows machine, same as the one you shared except by the ip address. This is documented at the MS docs in the section of using the emulator with a macOS

Within your application, change the endpoint URL to use the IPv4 address returned by ipconfig.exe instead of localhost.

Regarding the setting UseDevelopmentStorage it was mentioned in different places, that was why I added it. Here's one
github issue, although I will remove it and see if this helps and will share my result.
Thank you for helping out.

@gmartinezsan
Copy link
Author

Finally got this setup working. Here are the steps that made it work:

  • Use the flag UseDevelopmentStorage=false in local settings. This one does not apply to Cosmos DB, but it should be included. It does not work to leave it empty or remove it.
{
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureWebJobsStorage": "UseDevelopmentStorage=false",
    "mydbconnection": "AccountEndpoint=https://###.##.#.#:8081/;AccountKey=%%$%$^^%;"
  },
  "IsEncrypted": false
}
  • Make sure the connection string is using the IP that your VM has and both host and guest have communication.
  • Export the certificate after the IP has been assigned, otherwise it will not work and you will have SSL issues (I did not test it without SSL).
  • The emulator should start with the flag /AllowNetworkAccess as stated in the docs:
    Microsoft.Azure.Cosmos.Emulator.exe /AllowNetworkAccess /Key=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
  • If there are issues with the Lease Collection or there is an error that does not exist you can add the CreateLeaseCollectionIfNotExists flag to the Run method:
public static void Run([CosmosDBTrigger(
            databaseName: "mydb",
            collectionName: "location",
            ConnectionStringSetting = "myconnection",
            LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> input, ILogger log)
        {

Note: If this does not work it is recommended to reset the data in the emulator (cosmos icon tray ->reset data).

Second note: I tried to get the traces as suggested in the troubleshooting guide but it never created any. I do not know why. Maybe the combination with the AllowNetworkAccess flag?

I used it like this:
Microsoft.Azure.Cosmos.Emulator.exe /AllowNetworkAccess /startwprtraces /Key=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

Thanks @marcduiker for pointing out the usage of 'UseDevelopmentStorage' flag.

Hope this helps someone else with the same setup.

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants