简体   繁体   中英

Running existing AWS Lambdas locally

I've created AWS Lambda in C# using Visual Studio that returns some JSON from the API endpoint. Now I want to run that lambda locally. All the examples use AWS SAM, but they create a new function using the SAM template.

When I run the command sam local start-lambda I get an error saying that the template isn't found. So what is certain is that I need template.yaml, but I'm not sure is there a way to generate this template for existing Lambda?

Any help is appreciated!

Check out the Template Anatomy resource on the AWS documentation .

You might find this example helpful (it's greatly simplified). I use NodeJS for development, but the differences between programming languages when you're creating a SAM Template are trivial. The example is an outline for a simple Lambda function someFunction being invoked by an API Gateway (HTTP) event.


AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: My Serverless Application
Parameters:
  # Manually define this in AWS IAM for just the services needed.
  lambdaExecutionRole:
    Description: 'Required. The role used for lambda execution.'
    Type: 'String'
    Default: 'arn:aws:iam::nnnnnnnnnnnn:role/LambdaExecutionRole'


Globals:
  Function:
    Runtime: nodejs10.x
  #   Environment:
  #     Variables:
  #       NODE_ENV: test
  #       DEBUG: myapp:foo

Resources:

  performSomeFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: performSomeFunction
      Handler: lambda.someFunction
      CodeUri: ./
      Description: description of the function being performed
      MemorySize: 256
      Timeout: 60
      Role:
        Ref: lambdaExecutionRole
      Events:
        # API Gateway proxy endpoint.
        ProxyApiRoot:
          Type: Api
          Properties:
            Path: '/'
            Method: ANY
        ProxyApiGreedy:
          Type: Api
          Properties:
            Path: '/{proxy+}'
            Method: ANY

As you're getting started with AWS Lambda, one of the big concepts to keep in mind is how your function will be triggered. Functions are triggered by different kinds of events, and there can be many many different types of events . I tend to use API Gateway, Simple Queue Service and CloudWatch Events to trigger mine, but it entirely depends on your use case.

It turned out that you can export Lambda function, and get the generated.yaml template, which was exactly what I needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM