4
votes

I want to test my lambda functions locally with Serverless Application Model (SAM) In the AWS docs they write :

SAM Local leverages the docker-lambda Docker images to run your code in a sandbox that simulates the Lambda execution environment.

I pulled the docker image on my computer. I could successfully run a simple Hello World Lambda Function.

Command to run Lambda function locally:

$ docker run -v "$PWD/target/classes":/var/task lambci/lambda:java8 com.amazonaws.lambda.demo.LambdaFunctionHandler

results:

"Hello from Lambda!"

Code of Lambda function automatically generated with Eclipse Toolkit:

package com.amazonaws.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<Object, String> {

    @Override
    public String handleRequest(Object input, Context context) {
        context.getLogger().log("Input: " + input);

        // TODO: implement your handler
        return "Hello from Lambda!"
    }

}

This is my progress till yet. What i couldnt do is to use sam local which uses the docker-lambda image.(Maybe i should not have to download it manually?).

I installed sam local on my windows:

npm install -g aws-sam-local

created a template.yaml config sam file.

    AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Resources:
  ExampleJavaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: com.amazonaws.lambda.demo.LambdaFunctionHandler
      CodeUri: ./target/demo-1.0.0-shaded.jar
      Runtime: java8

the name for CodeUri: i choosed after i build my shaded jar file with:

mvn compile shade:shade

after this i should run to run my lambda function:

$ echo '{ "some": "input" }' | sam local invoke

NOW i have this ERROR:

  2017/12/05 14:56:36 Successfully parsed template.yaml
2017/12/05 14:56:36 Running AWS SAM projects locally requires Docker. Have you got it installed?
2017/12/05 14:56:36 error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/_ping: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.

What is my mistake to use SAM Local with Java? Can it be that its not working because my computer has not Hyper-V and iam using dockertoolbox?..

here you can see Advanced sam docs with compiled languages like java.

1

1 Answers

2
votes

It was a bug in Sam local.. fixed with new update

If you still have a problem in windows then try this :

COMPOSE_CONVERT_WINDOWS_PATHS=1

this should help if your Path is wrong. / \