techdays 2017 - creating real life serverless solutions with azure functions
TRANSCRIPT
servers
Like PaaS
“If your PaaS can efficiently start instances in 20ms that run for half a second, then call it serverless.”Adrian Cockcroft - VP Cloud Architecture Strategy AWS
Keep It Small and Simple
URL Minification
GET minified URL redirect
CREATE minified URL
CosmosDB
Oh yeah!
Web App CosmosDB
CosmosDB
GET
POST
CosmosDB
GET
POST
Azure cacheUPDATE Change
Feed
CosmosDB
GET
POST
Azure cacheUPDATE
ChangeFeed
Queue TRIGGER
CosmosDB
GET
POST
Bindings are your friend
"bindings": [
{
"type": "httpTrigger",
"route": "{slug}",
"methods": [
"get"
],
"authLevel": "anonymous",
"name": "req"
}
],
Function, User, System & Admin
"bindings": [
{
"name": “minifiedUrl",
"type": "documentDB",
"databaseName": “MinifyRepository",
"collectionName": “MinifiedUrls",
"createIfNotExists": true,
"connection": “CosmosDbConnection",
"direction": "out"
}
],
Don’t write JSON
Where to put the code
Existing patterns & principles still apply!
private readonly ICosmosClient cosmosClient;
public CreateUrlHandler()
{
this.cosmosClient = new CosmosClient();
}
public CreateUrlHandler(ICosmosClient cosmosClient)
{
this.cosmosClient = cosmosClient;
}
CosmosDB
GET
POST
[FunctionName("Get")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "{slug}")]
HttpRequestMessage req,
string slug,
TraceWriter log)
{
var getUrl = new GetUrlHandler();
var minifiedUrl = await getUrl.Execute(slug);
if (minifiedUrl == null)
{
return req.CreateErrorResponse(HttpStatusCode.NotFound,
$"Minified value `{slug}` is not found.");
}
var response = req.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Location = new Uri(minifiedUrl.FullUrl);
return response;
}
public static class Create
{
[FunctionName("Create")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "create")]
HttpRequestMessage req,
[DocumentDB("datbaseName", "collectionName",
PartitionKey = "MinifiedUrls",
CreateIfNotExists = true,
ConnectionStringSetting = "minifiedUrlConnectionString")]
out dynamic minifiedUrl,
TraceWriter log)
{
string jsonContent = req.Content.ReadAsStringAsync().Result;
var data = JsonConvert.DeserializeObject<MinifiedUrl>(jsonContent);
var create = new CreateUrlHandler();
minifiedUrl = create.Execute(data);
return req.CreateResponse(HttpStatusCode.Created,
$"api/{data.MinifiedSlug}");
}
}
Easy mode
master branch
to production
Pro mode
Check if you use slots
Point to the correct package
https://my.jdv.li/api/minifiedurl
or
https://my.jdv.li/minifiedurl
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"getminifiedredirect": {
"matchCondition": {
"methods": [ "GET" ],
"route": "/{slug}"
},
"backendUri": "https://%WEBSITE_HOSTNAME%/api/{slug}"
}
}
}
Matching conditions