C# & AWS Lambda

Preview:

Citation preview

C# on AWS Lambda

1 @phermens

About me

Pat Hermens

● Father of two

● Based in Rotterdam

● Lead Developer at Coolblue

● Still have a soft-spot for VB6

https://hermens.com.au/ 2

@phermens

Every day a little more Dutch

3

A little bit of history

4 @phermens

Launched in Nov 2014.5

@phermens

C# support in Dec 2016.6

@phermens

Officially only supports .NET Core.7

@phermens

AWS was first of the “major providers”.8

@phermens

They have an open-source SDKhttps://github.com/aws/aws-sdk-net/ 9

@phermens

They also have a great ‘dotnet’ repo…https://github.com/aws/aws-lambda-dotnet/ 10

@phermens

… & most of my tooling is from here11

@phermens

It’s “Hello world” time!

12 @phermens

13 @phermens

Hello World! (including ILambdaContext)

“Relatively simple” configuration14

@phermens

Wait, we need more than code!

15 @phermens

16 @phermens

Set up an account

Install the CLI

17 @phermens

18 @phermens

19 @phermens

Regions? AZ’s?

20 @phermens

IAM?

21 @phermens

Required values can be set in code!

22 @phermens

… or on the command line!

NOW it’s “Hello world” time!

23 @phermens

24 @phermens

Hello World! (including ILambdaContext)

Deploying “Hello-World”

25 @phermens

26 @phermens

Running deploy-function & invoke-function

Calling our “Hello” function

27 @phermens

28 @phermens

We can use the ‘Amazon’ NuGet package(s)

We can use AWS SNS29

@phermens

We can use any ‘webhook’-type integration30

@phermens

Creating a “Hello-World-Caller”

31 @phermens

32 @phermens

Pretty straightforward using the SDK

Just F5 in VS Code!33

@phermens

Larger architectures?

34 @phermens

Our “infra team” had a fun exercise...35

@phermens

CloudFormation - scripted infrastructure36

@phermens

Normally looks like this...37

@phermens

… but can also look like this!38

@phermens

“Add trigger” to your Lambda39

@phermens

Pick your integration - whatever you want!40

@phermens

Or just, “deploy-serverless” with the CLI41

@phermens

AWS Serverless Application Modelhttp://github.com/awslabs/ 42

@phermens

http://serverless.com/framework/docs/ 43

@phermens

So, what does this mean for Coolblue?44

@phermens

This makes it look simple!45

@phermens

Let’s recap some “gotchas”

46 @phermens

47 @phermens

Timings can vary wildly!

Default values can bite you!48

@phermens

Know your costs! 800,000 sec = 9.25 dayshttps://aws.amazon.com/lambda/pricing/ 49

@phermens

Almost everything is case-sensitive!50

@phermens

PS> dotnet lambda deploy-function HelloWorld-EUWest# New Lambda function created

PS> dotnet lambda invoke-function helloworld-euwest -p "Pat"# Error invoking Lambda function: Function not found: arn:aws:lambda:eu-west-1:955206359773:function:helloworld-euwest

‘ILambdaContext’ should ALWAYS be last51

@phermens

public string SayHi(ILambdaContext context, string name = "World"){

// … do something}

RESPONSE: (only on invocation, after deployment){ "errorType": "LambdaException", "errorMessage":

"Method 'SayHi' of type 'MyFirstLambdaFunction.HelloWorld' is not supported:the method has 2 parameters, but the second parameter is not of type'Amazon.Lambda.Core.ILambdaContext'."

}

Read the FAQ’s! https://aws.amazon.com/lambda/faqs/ 52

@phermens

Q: How are compute resources assigned to an AWS Lambda function?In the AWS Lambda resource model, you choose the amount of memory you want for your function, and are allocated proportional CPU power and other resources.

For example, choosing 256MB of memory allocates approximately twice as much CPU power to your Lambda function as requesting 128MB of memory and half as much CPU power as choosing 512MB of memory. You can set your memory in 64MB increments from 128MB to 1.5GB.

Containers can be reused!https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/ 53

@phermens

Handy links

54 @phermens

Tooling● https://github.com/aws/aws-sdk-net/● https://github.com/aws/aws-lambda-dotnet/

Documentation● http://github.com/awslabs/● http://serverless.com/framework/docs/● https://aws.amazon.com/lambda/faqs/

https://aws.amazon.com/documentation/ 55

@phermens

56 @phermens

Thanks!

57 @phermens

Tonight’s demos are on GitHub (but they’re really nothing special!)

● https://github.com/pheonix25/lambda-helloworld

● https://github.com/pheonix25/lambda-helloworldcaller

Thanks!

58 @phermens