techdays 2017 - creating real life serverless solutions with azure functions

48

Upload: jan-de-vries

Post on 23-Jan-2018

152 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 2: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 3: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 4: TechDays 2017 - Creating real life serverless solutions with azure functions

servers

Page 5: TechDays 2017 - Creating real life serverless solutions with azure functions

Like PaaS

Page 6: TechDays 2017 - Creating real life serverless solutions with azure functions

“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

Page 7: TechDays 2017 - Creating real life serverless solutions with azure functions

Keep It Small and Simple

Page 8: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 9: TechDays 2017 - Creating real life serverless solutions with azure functions

URL Minification

Page 10: TechDays 2017 - Creating real life serverless solutions with azure functions

GET minified URL redirect

CREATE minified URL

Page 11: TechDays 2017 - Creating real life serverless solutions with azure functions

CosmosDB

Page 12: TechDays 2017 - Creating real life serverless solutions with azure functions

Oh yeah!

Page 13: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 14: TechDays 2017 - Creating real life serverless solutions with azure functions

Web App CosmosDB

Page 15: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 16: TechDays 2017 - Creating real life serverless solutions with azure functions

CosmosDB

GET

POST

Page 17: TechDays 2017 - Creating real life serverless solutions with azure functions

CosmosDB

GET

POST

Azure cacheUPDATE Change

Feed

Page 18: TechDays 2017 - Creating real life serverless solutions with azure functions

CosmosDB

GET

POST

Azure cacheUPDATE

ChangeFeed

Queue TRIGGER

Page 19: TechDays 2017 - Creating real life serverless solutions with azure functions

CosmosDB

GET

POST

Page 20: TechDays 2017 - Creating real life serverless solutions with azure functions

Bindings are your friend

Page 21: TechDays 2017 - Creating real life serverless solutions with azure functions

"bindings": [

{

"type": "httpTrigger",

"route": "{slug}",

"methods": [

"get"

],

"authLevel": "anonymous",

"name": "req"

}

],

Function, User, System & Admin

Page 22: TechDays 2017 - Creating real life serverless solutions with azure functions

"bindings": [

{

"name": “minifiedUrl",

"type": "documentDB",

"databaseName": “MinifyRepository",

"collectionName": “MinifiedUrls",

"createIfNotExists": true,

"connection": “CosmosDbConnection",

"direction": "out"

}

],

Page 23: TechDays 2017 - Creating real life serverless solutions with azure functions

Don’t write JSON

Page 24: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 25: TechDays 2017 - Creating real life serverless solutions with azure functions

Where to put the code

Page 26: TechDays 2017 - Creating real life serverless solutions with azure functions

Existing patterns & principles still apply!

Page 27: TechDays 2017 - Creating real life serverless solutions with azure functions

private readonly ICosmosClient cosmosClient;

public CreateUrlHandler()

{

this.cosmosClient = new CosmosClient();

}

public CreateUrlHandler(ICosmosClient cosmosClient)

{

this.cosmosClient = cosmosClient;

}

Page 28: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 29: TechDays 2017 - Creating real life serverless solutions with azure functions

CosmosDB

GET

POST

Page 30: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 31: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 32: TechDays 2017 - Creating real life serverless solutions with azure functions

[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;

}

Page 33: TechDays 2017 - Creating real life serverless solutions with azure functions

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}");

}

}

Page 34: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 35: TechDays 2017 - Creating real life serverless solutions with azure functions

Easy mode

Page 36: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 37: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 38: TechDays 2017 - Creating real life serverless solutions with azure functions

master branch

to production

Page 39: TechDays 2017 - Creating real life serverless solutions with azure functions

Pro mode

Page 40: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 41: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 42: TechDays 2017 - Creating real life serverless solutions with azure functions

Check if you use slots

Point to the correct package

Page 43: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 44: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 45: TechDays 2017 - Creating real life serverless solutions with azure functions
Page 46: TechDays 2017 - Creating real life serverless solutions with azure functions

https://my.jdv.li/api/minifiedurl

or

https://my.jdv.li/minifiedurl

Page 47: TechDays 2017 - Creating real life serverless solutions with azure functions

{

"$schema": "http://json.schemastore.org/proxies",

"proxies": {

"getminifiedredirect": {

"matchCondition": {

"methods": [ "GET" ],

"route": "/{slug}"

},

"backendUri": "https://%WEBSITE_HOSTNAME%/api/{slug}"

}

}

}

Matching conditions

Page 48: TechDays 2017 - Creating real life serverless solutions with azure functions

https://github.com/Jandev/minifier

@Jan_de_V

[email protected]

https://jan-v.nl