So, I'm finally at the point where I can test my first Lambda function locally. As background, I've installed the AWS CLI under MacOS 10.14.2 (Mojave) and am able to access my AWS account. I've successfully zipped up my Lambda function and used 'aws lambda create-function' to deploy it.
I've installed aws-lambda-local (https://www.npmjs.com/package/aws-lambda-local) using 'npm install -g aws-lambda-local'.
But when I invoke the following from the Lambda function root:
lambda-local -l index.js -e event.json
I get the following error:
Invalid function name. It should be accessible from invocation place
Would someone please tell me why this is happening? I mean, the function name is most definitely valid.
Totally confused here!
According to the aws-lambda-local docs, there is no -l option. Use -f or --function to specify the file with the lambda function
Related
Host Machine: Mac M1
SAM version: 1.70.0
CDK version: 2.60.0 (build 2d40d77)
In our api_stack.py file we have a simple setup of
API Gateway REST endpoints
Lambda functions triggered by API Gateway
The entire codebase was initially setup with cdk init
When we run cdk synth, we get a files generated in the folder cdk.out including the api.template.json which is the CloudFormation template file.
Following commands all work:
cdk synth
cdk deploy
However when we run sam local invoke or sam local start-api, I get an error that looks like below.
% sam local invoke GetUserTemporary -t cdk.out/api.template.json -e events/get_user/prod-user.json
Invoking get_user_temporary.handler.handle (python3.9)
DependenciesLayerDF300E31 is a local Layer in the template
Local image was not found.
Building image.......................................................................................................................................................................................................................................................................................................................
Failed to build Docker Image
NoneType: None
Error: Error building docker image: The command '/bin/sh -c mv /var/rapid/aws-lambda-rie-x86_64 /var/rapid/aws-lambda-rie && chmod +x /var/rapid/aws-lambda-rie' returned a non-zero code: 1
This used to work just about 1 week ago and now it is showing this error all of sudden. Exact same codebase is git cloned in my co-workers' machines.
Windows
Mac M1
And this all works for them. I try to do this in my M1 laptop and it fails now.
It turns out you have to specifically specify what architecture you are using when defining your lambda functions in your CDK Python source code. You can achieve this by adding the architecture= argument like below.
fn = _lambda.Function(
self,
func_name,
runtime=_lambda.Runtime.PYTHON_3_9,
code=_lambda.Code.from_asset('api/lambda_fns'),
handler=f'{func_path}.handle',
layers=[self.layer],
role=role,
architecture=_lambda.Architecture.X86_64,
)
I am tring to run my go project using amazon lambda, this is my current main.go
https://gist.github.com/krakiun/61e4e4dc5ab91f557e481f0230ed3ba0
I tried several methods, but none worked
How i can make run this project in lambda, in this moment if i run using router.Run(cfg.HTTP.ListenAddr) is working without any error,
with this log.Fatal(gateway.ListenAndServe(cfg.HTTP.ListenAddr, router)) is die with this error :
expected AWS Lambda environment variables [_LAMBDA_SERVER_PORT AWS_LAMBDA_RUNTIME_API] are not defined
exit status 1
How i can fix my code to run in aws lambda ?
You have to separate the environment
the below code for run at local
router.Run(cfg.HTTP.ListenAddr)
the lambda can only run on the AWS Lambda Function. You must deploy it to lambda
lambda.Start(router)
You can check the example at https://maxrohde.com/2021/05/01/lambda-go-starter-project/. And the source code is here: https://github.com/mxro/go-lambda-starter-project/tree/main/packages/lambda-go-gin
I'm developing an application with micronaut using SAM CLI to deploy it on AWS Lambda. As I was including dependencies and developing new features, the function packages got bigger an bigger (now they are around 250MB). This makes deployment take a while.
On top of that every time I edit template.yaml and then run sam build && sam deploy to try a new configuration on S3, RDS, etc... I have to wait for gradle to build the function again (even though it's unchanged since the last deployment) and upload the whole package to S3.
As I'm trying to configure this application with many trials and errors on SAM, waiting for this process to complete just to get an error because of some misconfiguration is getting quite counterproductive.
Also my SAM s3 bcuket is at 10GB size after just a single day of work. This may get expensive on the long run.
Is there a way to avoid those gradle rebuilds and reuploads when teh function code is unchanged?
If you are only updating the template.yml file, you could copy the new version to ./.aws-sam/build folder and then run sam deploy
$ cp template.yml ./.aws-sam/build/template.yml
$ sam deploy
If you are editing a lambda you could try to update the function code by itself (after you create it in the template and deploy of course). That can be done via the AWS CLI update-function-code command:
rm index.zip
cd lambda
zip –X –r ../index.zip *
cd ..
aws lambda update-function-code --function-name MyLambdaFunction --zip-file fileb://index.zip
more info can be found here:
Alexa Blogs - Publishing Your Skill Code to Lambda via the Command Line Interface
AWS CLI Command Reference - lambda - update-function-code
my SAM s3 bcuket is at 10GB size
Heh. Yea start deleting stuff. Maybe you can write a script using aws s3?
So I can deploy my lambda to aws no problem but trying to run it locally
serverless invoke local --function hello
TypeError: Cannot read property 'accessKeyId' of null
The congig and credentials file look ok./
Edit
~/.aws/config
[default]
region = eu-west-1
output = json
~
[default]
aws_access_key_id = A***************
aws_secret_access_key = /p*********************
What aws-sdk version are you using in your function? A quick google showed that there was a problem in the aws-sdk that contained the same error message. Make sure you have the latest version.
Also, remember that when you run a function locally, aws-sdk will look for credentials on your local system.
Run $ ls ~/.aws on a mac and C:\> dir "%UserProfile%\.aws" on windows to see if you have your credentials files stored locally. See this guide for more details.
I am working on a project using AWS SAM local. I am invoking a function with
sam local invoke which successfully executes the function. The lambda function writes an image file into /tmp/image.png when the function is invoked. This writing operation happens in the environment of SAM local, the /tmp folder belongs to its docker environment.
Is there a way for me to access the files written into the /tmp folder while developing locally?
This apparently has been answered on their github:
Link
You create a /c/tmp directory and set the TMPDIR=/c/tmp environment variable while invoking.
echo '<!DOCTYPE html><html><head><title>HTML doc</title></head><body>Content<body></html>' | TMPDIR=/c/tmp sam local invoke "HtmlToPdfFunction"