Hey friends, did you know that you can
create serverless event flows based on
activity in your Azure Cosmos DB database?
A new version of the Cosmos DB
bindings extension for Azure Functions
is now available in preview.
Matias Quaranta is here to show me
how it works, today on Azure Friday.
Hey friends, it's Azure Friday.
I'm here with Matias and we're going to
talk about a new version of the Cosmos DB
bindings extension for Azure Functions.
How are you, Sir? Pretty good,
thanks again Scott, for the invitation.
It's always a pleasure to be here. Yeah?
So I understand that you can now trigger
Azure Functions based on things that are
happening in your cosmos DB database.
That's correct.
We already have a Functions integration
that allows users to create bindings
and triggers to their database and
containers and we are releasing a new
version that will enable more interesting
things like wherein Azure AD identities.
Instead of connections and strings,
performance improvements and a bunch of
other stuff that we talk in the show,
yeah, in the past I've felt a little
bit kind of uncomfortable about how
I had to deal with my connection
strings and I really just want to think
about the identity of the user and,
you know, use Azure AD if I can.
So it'll be nice to see
how you've improved that.
That's for sure.
I I totally understand that from the
security perspective it's pretty awkward to,
you know,
have those keys laying around you
never know who can snoop and get them.
So yeah,
why don't we start and take a look at the
at the code and we can see it in action.
Alright, let's do it.
OK,
so as you see I have my functions
application.
The only thing I did was bump the
Azure Functions extension for
Cosmos DB to our new version.
You see there is still in preview.
We'll talk about a bit at the end,
but for now it's basically just pull
whatever is the latest version.
Once we do that,
we can just go back to our function
similar to what we've worked in
the past with the current extension
I'm using the Cosmos DB trigger.
And probably the first big difference
that we'll notice that the connection
is no longer called connection string.
It's now a connection.
And the reason is that we right now
support not only connection strings,
I could keep using connections strings,
why not?
But we also support Azure the
integrations or also call us or
also known as manager identities.
So in order to get this working,
I already went ahead and deployed
this function to Azure shells
to make it easier and faster,
but basically what I'm selling natural
functions is my connection will be
identified by this name Cosmo TV.
Then I went to my Azure Functions
which is already deployed in Azure and
reset configuration section called Identity.
Once we enable this,
this basically creates an Azure
identity or one entity.
In my Azure ready tenant and gives
me the idea of the identity.
Basically this identity now represents
the function it's telling nationality.
You know this is a function and
this is identity that represents
it as an entity in Azure ID.
Once that is exposed and I had the object ID,
the next step that I can do is Cosmos
DB now supports Azure AD integration,
not only for functions but for
any other flows, and I can create
my own role definitions there.
There is a list of different
permissions that I can give.
For example, I could create a rule
definition that can only read data
but cannot write or that has only
access to a particular subset
of my databases and not. Others.
Three comes with two predefined roles,
one that is data reader and one that
is data contributor or freedom, right?
So for the purpose of this demo,
what I did was I basically took and
used the role definition for the
data contributor because maybe I want
to do other things in the function,
like maybe write some documents
somewhere else.
The way right now to wire this
is basically through CLI.
So once I have the.
The idea of the function and the
role that I want to execute.
There are a couple of Azure CLI
commands that I can run which are.
Basically this is how to create a
custom role and there is a way for
you to say this is basically a snippet.
The account in that I want to give
access to this is the idea of the
role definition that I want to use
and then this would be the idea of my
managed identity in my function app
and I can just run this on natural CLI.
What it will do is it will provide or
give permission to my function app to
access my customer account with the
permissions that I'm already defining,
right?
So you're assigning a role to an
identity you're saying it is my role.
My job as this function?
To update this little table
or read this little thing,
you give that role a name and then
you assign it to the identity and
we don't think about connection
strings anymore and we're really
using the power of Azure the way
it's meant to be used exactly.
So basically once I I already did that,
I enable this function manager
identity to access my database.
Is the next thing that I need to do
is how to set the configuration so
in Azure Functions there is a way.
Need to tell the function or the
configuration of the function that
I want to use my identities.
This is basically how I would do
it is I need to set the connection
name prefix which is in this case.
Andresa underscore credential that
I need to put us manage identity
and I do this everything on there.
Function configurations that I would
have in this function configuration.
A part that says Cosmo TV
because that's my prefix.
The one that I'm setting on
the function under credential,
and this has a value of managed identity.
This is telling Azure Functions.
For this connection that is
called 'cause I want to use a
credential unnaturally credential,
but I want to use the manage
identity of this function.
And the second,
the second configuration that I
need to add is part of particular
or individual configuration,
which is which is the account
endpoint that I want to access
with this configuration.
This would be the account endpoint
for the account that I just gave
permissions to my function app.
So in the case of my example,
what I'm saying is.
I want to use my identity and
the account endpoint is this.
Particular account that I just run this
CLI for and I gave permissions for.
Once I do that.
And I deploy my function.
This is already giving the function
access to my Costco instance without me
putting any connection strings anywhere.
This not only brings improvements
in the security because there
is no way for me to leak this
credentials anywhere and no one
can steal them and access my data.
I also don't care about refreshing
or rolling these these keys in the
in the event that they might be
leaked because there is no potential.
Of leakage.
So once I have this already deployed.
I'm going to.
Hook into the functions locks,
so I should be able to see whatever
I'm outputting in my function,
which is very simple.
I just wanted to do something simple for you.
It will just print whatever document is
being detected as change in the function.
So basically if I come to
my country database.
And I add a new document,
let's call it Hello Scott.
And I'd say.
It should see the invocations and the
logs here. OK, let's wait and see.
So here is the invocation.
I totally miss allows.
OK, I'll I'll refresh the screen
just in case there is some.
Authentication thing going on OK
and I'll write another document.
So while you're doing that so a
new document has been created in
Cosmos DB so that is a, you know,
an event that we are subscribed to.
It could be an update.
In this case it was a create and we
saw Cosmos get that post and receive
it and then we can see here we're
going to connect to the logs and
then you can see you're now connected
to the log streaming service.
So now we're actually connected and have
a persistent connection to the log stream.
That's correct,
so let's come again and say.
Now that doesn't mean the log didn't happen,
it just means we didn't see it before.
For our friends that you're
following along at home.
So now we do our post.
We hear our save and then
there you go look at that.
Yep, so basically this function is accessing
a listening for events on my cosmic
account without any connections strings.
That's I, I think that the biggest
selling point of this new extension,
but it comes with other
new interesting things.
Can I show it to you?
Yeah, let's see it.
Awesome, so going back to the function.
This new version comes with a.
We are actually using ourlatest.net
underlying the function extensions
that comes with a really high
number of performance improvements.
We've done a really extensive
extensive shop during the past couple
of years to improve performance,
so all the operations for the trigger
and for the bindings will be faster than
the previous version of the tension.
There is one key point that is
super interesting is previously.
With the trigger you were limited in
terms of what's the type of document that
you received in the previous version.
You would get something like
a type called document,
which was a xenetic property bug
of Jason properties that it was
exposed in the previous SDK version.
There was a lot of users that didn't
want to use document because it's
just such an Eric property bug.
They wanted to use their own
business type or business classes.
So what they had to do is basically they
had they would have to take the input,
serialize it as a raw string and then
take that raw string and direct.
I'm sorry into their own business types.
All that double triple serialization.
It's obviously a performance penalty,
so now with the new extension you can
just use whatever type that you want.
That maps to whatever business
or user flow that you have,
and we will do is we will take the
rachets under is coming from the
service and we'll deserialize into
your type whatever it is so you
don't have to pay the cost of double
serialization if you want to handle
the data in your own custom types,
that's really cool.
Yeah, so you call them a poco,
a plain old Clr object,
a poco object for our friends
that are following along at home
means that this is a.net.
SDK so it's a native SDK,
so it feels like net 'cause itis.net
so you just get an object that's shaped
the way you want the object to be,
which is really great.
And then you have a generic
readonly list which is using the
net generic support so it feels
very comfortable and then you don't
have to think about serializing and
deserializing and just massaging data.
It looks like Cosmos is going to do it
all for us along with Azure Functions.
It removes a bunch of performance
pitfalls that we had
before and that leads me to my last.
Point which is civilization again,
but a bit more advanced now.
Our latest uses newtonsoft Jason
as a default serialization engine.
We know that system version is available,
but maybe there are some other
Jason serialization engines.
And there should be an easy way for you
to pick which is a serialization issue
that you that you want to use for that.
It's maybe the one that you
use in your business,
or the one that you prefer.
So what we enable with the new extensions
is that there is a way for you to
using the OR dependency injection,
define your own custom serializer.
This custom Serializer is an API
that basically contains 2 methods,
either from string from stream
or to a stream,
and you can use your own
custom implementation.
Just defining this serializer factory
with an implementation that returns
an unemployed and implementation
of the Cosmos Serializer API.
Uh, I could like in this example we used
in system as a serialization engine,
replacing the newtonsoft Jason
that comes with default where I
could be using whatever other.
And that I have as a preference.
So once I define this configuration
in my function,
I could then come back here
and use the type with whatever
serialization technology that I want,
and we'll use yours.
To the serialized the rotation
into your type.
Again, I could be using it
as I could be using Jason.
I could be using whatever you
want and it will just work.
Very cool,
this is great.
This sounds like a really great
release because you're you're
doing refactoring via subtraction,
which means I don't have to think
about my connection strings anymore.
I don't have to think about
serialization as much anymore,
but if I want to think about serialization,
you've given me full control
now with a pluggable model to
use any serializer that I want.
So things like things are just
getting better in Cosmos.
DB World with Azure Functions exactly.
I mean it's all in in giving users
choice and mid users where they are.
They can now have more freedom when
they are working with our extension.
Fantastic,
so this is using the latest Cosmos
SDK and where can folks go to
learn about this information?
So right now we we are in preview
and the reason we are in preview
is because some of you might be
familiar with Azure Functions model.
You have different plans,
there is consumption plan.
There is app Service plan.
There is premium plan for consumption plan.
There is a scaling mechanism that
dynamically will scale your function
based on your needs and the reason we
are in preview right now is because
we are still deploying that mechanism
that that updated mechanism that
will work with the new extension.
So once we have that in the coming weeks,
this will be completely out and we
should have no new announcements
in our Cosmo TV Devlog,
which you probably can.
Find in the blogs.
Dot Microsoft account Slash
that will be the place where we
normally write and announce this.
This new this new releases.
And if you are interested in
seeing part of the B3 SDK,
which is the one that we
are using under the hood,
that should be available in
GitHub because it's open source.
Yep, so this is completely open source.
And we can.
You can even if you are,
if you have any future requests,
you can certainly do them here and we'll
follow along and make sure that you have,
you know whatever can help you with
either your function, functions,
integrations or with your usage
of the cost to be net. Fantastic,
well thank you so much for your time today.
Thank you for the invitation.
It's always a great pleasure and I hope that
this has been of help for everyone out there.
Absolutely,
I am learning all about the Azure Azure
Cosmos DB extension for Azure Functions.
It's been updated with some great
new features that you can give a
try to right now and this has been
another episode of Azure Friday.
Hey, thanks for watching
this episode of Azure Friday.
Now I need you to like it.
Comment on it, tell your friends,
retweet it.
Watch more as your Friday.