AWS Lambda in C#
-
Upload
amazon-web-services -
Category
Technology
-
view
182 -
download
5
Transcript of AWS Lambda in C#
![Page 1: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/1.jpg)
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
John Chang (張書源)
Technology Evangelist
January, 2017
Serverless Development with
C# and AWS Lambda
![Page 2: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/2.jpg)
What to Expect from the Session
• Brief Introduction to AWS Lambda
• Why .NET Core
• New .NET Core Lambda tooling
• Lots of exciting demos!
![Page 3: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/3.jpg)
AWS Lambda Crash Course
• Compute part of the AWS serverless architecture
• Zero administration
• Event-driven scaling
• Never pay for idle time
• Upload your code and go
![Page 4: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/4.jpg)
Why .NET Core?
• Performance
• Modularized design
• Low memory usage
• Open Source
• Built and validated for Amazon Linux
• Allow AWS to respond to security issues
• Cross Platform
• Develop on any platform and run on Amazon Linux
![Page 5: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/5.jpg)
.NET Core Development
• .NET Core 1.0
• https://www.microsoft.com/net/download/core
• Visual Studio 2015 Update 3
• Visual Studio 2015 Tools (Preview 2)
• Target framework netcoreapp1.0
• Package with .NET Core CLI “dotnet publish”
• Upload as a zip file
![Page 6: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/6.jpg)
Declaring Lambda Functions
• Static or instance methods
• Must have a default constructor for instance methods
• Can take 0 or 1 input parameters
public void FunctionHandler()
public void FunctionHandler(S3Event evnt)
public string FunctionHandler(S3Event evnt, ILambdaContext context)
• Optional ILambdaContext parameter
![Page 7: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/7.jpg)
• async methods must return a Task or Task<T>
Declaring Lambda Functions
public async Task FunctionHandler<T>(T obj, ILambdaContext context)
public void FunctionHandler<T>(T obj, ILambdaContext context)
• No generic methods
public async Task FunctionHandler(S3Event evnt, ILambdaContext context) // return void
public async Task<MyType> FunctionHandler(S3Event evnt, ILambdaContext context)
![Page 8: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/8.jpg)
Invoking Lambda Functions
![Page 9: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/9.jpg)
Invoking from AWS Lambda Event Sources
• Function executes in response to events in AWS
services
• A few of the supported services
• Amazon S3
• Amazon DynamoDB
• Amazon Kinesis
• ….
• Scheduled Events
![Page 10: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/10.jpg)
Invoking with the AWS SDKs
• Invoke methods using any SDK
• Great for mobile apps
• Manage credentials with Amazon Cognito
var client = new AmazonLambdaClient(cognitoCredentials, RegionEndpoint.USWest2);
var request = new InvokeRequest
{
FunctionName = "ProcessGameScore",
Payload = gameStats
};
InvokeResponse response = await client.InvokeAsync(request);
![Page 11: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/11.jpg)
Invoking from Amazon API Gateway
• Frontend of the AWS Serverless architecture
• Expose Lambda functions as HTTP API
• Manages authentication, versioning, monitoring and
more
![Page 12: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/12.jpg)
Demo Time
![Page 13: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/13.jpg)
Demo:
Responding to AWS Events
![Page 14: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/14.jpg)
Recap
• Created a new Lambda project in Visual Studio
• Learned how serialization works
• Learned about the handler format
• assemblyname::namespaced-classname::methodname
• Published from Visual Studio
• Configured AWS event source
• Invoked the Lambda function from Visual Studio
![Page 15: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/15.jpg)
Demo:Cross Platform: Command Line
![Page 16: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/16.jpg)
Yeoman Project Generators
• Project generator ecosystem
• Installable via npm:
• Node.js dependency
• npm install –g yo
• AWS Lambda generators
• npm install –g generator-aws-lambda-dotnet
• yo aws-lambda-dotnet
![Page 17: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/17.jpg)
Recap Cross-Platform Demo
• Created a project with Yeoman
• npm install –g yo generator-aws-lambda-dotnet
• yo aws-lambda-dotnet
• Develop with any editor
• Command line deployment with the .NET CLI and
Amazon.Lambda.Tools NuGet package
• dotnet lambda <command> <args>
• In preview
![Page 18: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/18.jpg)
Demo:
Serverless Application
![Page 19: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/19.jpg)
Demo Application Architecture
internet
GetBlogs()
GetBlog()
AddBlog()
RemoveBlog()API Gateway
AWS Lambda
Amazon DynamoDB
AWS CloudFormation Stack
![Page 20: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/20.jpg)
Our Serverless Application Deployment
Visual Studio
Project
Amazon S3
AWS CloudFormation
App bundle
(.zip)
Serverless
template
2: Request stack
…other resources
Lambda Functions
…other resources
API Gateway
4: Stack launched
3: Fetch template
![Page 21: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/21.jpg)
Recap Serverless Application
• Created an AWS Serverless application
• Learned about the serverless.template file
• Exposed Lambda functions as a HTTP API
• Deployed using AWS CloudFormation
![Page 22: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/22.jpg)
ASP.NET Core and Web API
• ASP.NET Core is the web framework for .NET Core
• Includes framework for building Web API applications
• Define API controllers to respond to requests
• Maps request data to function parameters
[Route("api/[controller]")]
public class ValuesController : Controller
{
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
...
}
...
![Page 23: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/23.jpg)
How can we use ASP.NET Core in Lambda?
• Amazon API Gateway Proxy Integration
• Forwards all requests to a Lambda Function
• Lambda Function
• Converts how requests and responses are represented
between API Gateway and ASP.NET Core
• Implemented by Amazon.Lambda.AspNetCoreServer
NuGet package
• Currently in preview
![Page 24: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/24.jpg)
ASP.NET Core Web API Architecture
IIS / Nginx
KestrelASP.NET
Core Hosting
User’s Web API Controllers
Internet
![Page 25: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/25.jpg)
ASP.NET Core Architecture on Lambda
API Gateway
LambdaASP.NET
Core Hosting
User’s Web API Controllers
Internet
![Page 26: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/26.jpg)
Demo:
ASP.NET Core Web API
![Page 27: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/27.jpg)
Recap ASP.NET Core Web API Demo
• Used existing ASP.NET Core framework
• No change to controller code
• Used API Gateway Proxy Integration to forward all
requests
• Lambda function translates between API Gateway and
ASP.NET Core
• Amazon.Lambda.AspNetCoreServer NuGet Package
• In preview
• Looked at the handler source in GitHub
![Page 28: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/28.jpg)
Useful Links
• AWS Toolkit for Visual Studio
• https://aws.amazon.com/visualstudio/
• AWS Lambda .NET GitHub
• https://github.com/aws/aws-lambda-dotnet
• AWS .NET Developer Blog
• https://aws.amazon.com/blogs/developer/category/net/
• AWS Lambda Developer Guide
• http://docs.aws.amazon.com/lambda/latest/dg/welcome.html
• AWS Toolkit for Visual Studio Guide
• http://docs.aws.amazon.com/toolkit-for-visual-
studio/latest/user-guide/welcome.html
![Page 29: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/29.jpg)
Other Related Sessions
CMP211 - Getting Started with Serverless Architectures
SRV202 – What’s New with AWS Lambda
DEV301 - Amazon CloudWatch Logs and AWS Lambda: A Match Made in
Heaven
MBL306 - Serverless Authentication and Authorization: Identity Management
for Serverless Architectures
SVR311 - The State of Serverless Computing
![Page 30: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/30.jpg)
Thank you!
![Page 31: AWS Lambda in C#](https://reader030.fdocuments.in/reader030/viewer/2022013117/588a12081a28ab132f8b5aad/html5/thumbnails/31.jpg)
Remember to complete
your evaluations!