Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you manage multiple AWS Lambda functions in Visual Studio?

In the AWS Lambda Visual Studio walkthrough to create a Lambda function: http://docs.aws.amazon.com/lambda/latest/dg/lambda-dotnet-create-deployment-package-toolkit.html you create a single AWS Lambda function in the Visual Studio project.

Does that mean that you can only create one function per project? What do you do if your serverless app has many functions? Is the function to VS project ratio 1:1, or am I missing something?

like image 661
Pete Lunenfeld Avatar asked May 07 '17 03:05

Pete Lunenfeld


People also ask

Can you have multiple Lambda functions?

Serverless applications usually consist of multiple Lambda functions. Each Lambda function can use only one runtime but you can use multiple runtimes across multiple functions. This enables you to choose the best runtime for the task of the function.

What is the best way to store the data used across multiple Lambda functions?

Amazon EFS for Lambda Amazon EFS is a fully managed, elastic, shared file system that integrates with other AWS services. It is durable storage option that offers high availability. You can now mount EFS volumes in Lambda functions, which makes it simpler to share data across invocations.

How do I write multiple AWS Lambda handlers in a .NET core project?

NET Core CLI execute the following command at the command line in the project root directory.","dotnet lambda help","All the command line options for the Lambda command can be specified in this file." In template file we need to set references to all function handler with different name under Recources. That's it.

How do I use AWS Lambda in Visual Studio?

Open Visual Studio, and on the File menu, choose New, Project. Do one of the following: For Visual Studio 2017, in the New Project dialog box, expand Installed, expand Visual C#,select AWS Lambda, choose the AWS Lambda Project (. NET Core - C#) template, and then choose OK.


2 Answers

If you use the AWS Lambda Project(.Net Core) template, you can only write one function per project. You can see that the aws-lambda-tools-defaults.json file only contains configuration for one function.

However, if you use AWS Serverless Application(.Net Core) template, you can manage multiple Lambda functions in one project to response to different API call using API Gateway. This is achieved through CloudFormation.

Check out this AWS ReInvent video: https://www.youtube.com/watch?v=Ymn6WGCSjE4&t=24s Jump to 31:08 to see how AWS Serverless Application with multiple Lambda functions works.

like image 128
codigube Avatar answered Sep 18 '22 15:09

codigube


You can create multiple lambda functions in one lambda project without using an API gateway or Serveless Project.

  1. Adding Serverless Template: Right click on your project and insert AWS server template. This template by default has a setup for AWS API gateway output. Modify this file to get rid of this. It should look like the following:

    {
        "AWSTemFormatVersion": "2010-09-09",
        "Transform": "AWS::Serverless-2016-10-31",
        "Description": "Test Project AWS Serverless Application.",
        "Parameters": {},
        "Resources": {
            "Register": {
                "Type": "AWS::Serverless::Function",
                "Properties": {
                    "Handler": "TestProject.Lambda::TestProject.Lambda.Function::RegisterAsync",
                    "Runtime": "dotnetcore2.1",
                    "CodeUri": "",
                    "Description": "Register Function",
                    "MemorySize": 256,
                    "Timeout": 30,
                    "Role": null,
                    "Policies": [
                        "AWSLambdaFullAccess"
                    ]
                }
            },
            "CreateUserFor": {
                "Type": "AWS::Serverless::Function",
                "Properties": {
                    "Handler": "TestProject.Lambda::TestProject.Lambda.Function::CreateUserForAsync",
                    "Runtime": "dotnetcore2.1",
                    "CodeUri": "",
                    "Description": "Create User Function",
                    "MemorySize": 256,
                    "Timeout": 30,
                    "Role": null,
                    "Policies": [
                        "AWSLambdaFullAccess"
                    ]
                }
            }
        }
    }
    

In this example we have two lambda functions defined Register and CreateUserFor.

  1. Now add a reference to this template in the projects aws-lambda-tools-defaults.json as follows:

     {
         "profile"     : "default",
         "region"      : "us-west-2",
         "configuration" : "Release",
         "framework"     : "netcoreapp2.1",
         "tem"      : "serverless.tem",
         "s3-bucket"     : "towmenot",
         "stack-name"    : "TowMeNot"
     }
    
  2. You can now define your function handlers with the names mentioned in the template. In this case:

     public async Task<bool> RegisterAsync(Registration registration, ILambdaContext context)
     {
         await RegisterHelper(registration);
         return true;
     }
    
     public async Task<User> CreateUserAsync(User newUser, ILambdaContext context)
     {
         return await CreateUserHelper(newUser);
     }
    
like image 25
LadderLogic Avatar answered Sep 22 '22 15:09

LadderLogic