admin管理员组

文章数量:1404924

I was following the article baeldung site

I did the step

docker run \
    --rm -it \
    -p 127.0.0.1:4566:4566 \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v ./target:/opt/code/localstack/target \
    localstack/localstack

docker shell

$ winpty docker exec -it 6efc21b9a53b bash

and trying to execute

awslocal lambda create-function \
    --function-name baeldung-lambda-function \
    --runtime java21 \
    --handler com.baeldung.lambda.LambdaHandler\
    --role arn:aws:iam::000000000000:role/lambda-role \
    --zip-file fileb:///opt/code/localstack/target/java-lambda-function-0.0.1.jar

Error:

Error parsing parameter '--zip-file': Unable to load paramfile fileb:///opt/code/localstack/target/snslambda-1.0-SNAPSHOT.jar: [Errno 2] No such file or directory: '/opt/code/localstack/target/snslamb da-1.0-SNAPSHOT.jar'

my jar file name is correct. agreed -v ./target:/opt/code/localstack/target is the volume and so used in zip-file. But how is the file mapped ? I don't see any file in localstack target folder

I am not sure on what path I have to run any commands. Please can I ask what are the check points and how to solve?

EDIT:

Test

awslocal lambda invoke --function-name sns-lambda-function --payload '{{ "Records": [ { "Sns": { "Message": "Hello from SNS!", "MessageId": "12345", "Timestamp": "2025-02-23T12:34:56.000Z" } } ] }' output.txt

I was following the article baeldung site

I did the step

docker run \
    --rm -it \
    -p 127.0.0.1:4566:4566 \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v ./target:/opt/code/localstack/target \
    localstack/localstack

docker shell

$ winpty docker exec -it 6efc21b9a53b bash

and trying to execute

awslocal lambda create-function \
    --function-name baeldung-lambda-function \
    --runtime java21 \
    --handler com.baeldung.lambda.LambdaHandler\
    --role arn:aws:iam::000000000000:role/lambda-role \
    --zip-file fileb:///opt/code/localstack/target/java-lambda-function-0.0.1.jar

Error:

Error parsing parameter '--zip-file': Unable to load paramfile fileb:///opt/code/localstack/target/snslambda-1.0-SNAPSHOT.jar: [Errno 2] No such file or directory: '/opt/code/localstack/target/snslamb da-1.0-SNAPSHOT.jar'

my jar file name is correct. agreed -v ./target:/opt/code/localstack/target is the volume and so used in zip-file. But how is the file mapped ? I don't see any file in localstack target folder

I am not sure on what path I have to run any commands. Please can I ask what are the check points and how to solve?

EDIT:

Test

awslocal lambda invoke --function-name sns-lambda-function --payload '{{ "Records": [ { "Sns": { "Message": "Hello from SNS!", "MessageId": "12345", "Timestamp": "2025-02-23T12:34:56.000Z" } } ] }' output.txt

Share Improve this question edited Mar 9 at 13:10 Kris Swat asked Mar 9 at 10:18 Kris SwatKris Swat 1,0442 gold badges18 silver badges49 bronze badges 2
  • Using {PWD} in windows and (pwd) in unix works mapping volume -v ${PWD}/target:/opt/code/localstack/target localstack/localstack . but test - An error occurred (ResourceConflictException) when calling the Invoke operation: The operation cannot be performed at this time. The function is currently in the following state: Pending. and later An error occurred (ServiceException) when calling the Invoke operation (reached max retries: 4): Internal error while executing lambda. – Kris Swat Commented Mar 9 at 12:27
  • someone liked and upvoted the question and some other expert downvoted without giving answer? – Kris Swat Commented Mar 9 at 21:43
Add a comment  | 

1 Answer 1

Reset to default 0

I have solved it following these steps

Run localstack >> open a docker shell >> create a topic >> create a lambda function that can respond to SNSEvent >> create a subscription >> and Test

  1. docker run --rm -it -p 127.0.0.1:4566:4566 -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}/target:/opt/code/localstack/target localstack/localstack

  2. docker exec -it containerid bash

  3. awslocal sns create-topic --name user-account-created

  4. awslocal lambda create-function --function-name sns-lambda-function --runtime java21 --handler com.example.SNSEventHandler --role arn:aws:iam::000000000000:role/lambda-role --zip-file fileb:///opt/code/localstack/target/java-lambda-function.jar

  5. awslocal sns subscribe --topic-arn arn:aws:sns:us-east-1:000000000000:user-account-created --protocol lambda --notification-endpoint arn:aws:lambda:us-east-1:000000000000:function:sns-lambda-function

  6. Test using below and check the logs of the lambda function container

awslocal sns publish --topic-arn arn:aws:sns:us-east-1:000000000000:user-account-created --message "Hello from LocalStack SNS!"

本文标签: amazon web servicesHow to local test a java AWS lambda function using LocalStackStack Overflow