AWS- AWS command to view lambda function in Command Line - amazon-web-services

My Lambda function code can't be viewed in the AWS Console due to an error with the file being too large. Is there an AWS command to pull the Lambda function code and view it in the command line?
Or, can I push my lambda code onto an S3 bucket, then download it from there to view it? Is there a command for that?

Download the Lambda function code from the AWS console, or use the AWSCLI as follows:
aws lambda get-function --function-name xyz --region <region>
This will return a presigned URL link to the .zip file containing your Lambda function.

Related

How to use AWS CLI within AWS Lambda?

I want to Copy data from an S3 bucket in one account and Region to another account and Region which is why I want to use AWS CLI to be triggered by an entry to the source s3 bucket and the lambda function can then use AWS CLI to run aws s3 sync
So I tried using the techniques given here: https://bezdelev.com/hacking/aws-cli-inside-lambda-layer-aws-s3-sync/
Basically
Install AWS CLI in a local virtual environment
Package AWS CLI and all its dependencies to a zip file
Create a Lambda Layer
However even after I add the layer I still see the error ModuleNotFoundError: No module named 'awscli'

How can I download/pull lambda code to a local machine from command line?

I am using the sam deploy command with the AWS SAM command line tool to deploy.
Now I made some changes with the web IDE in the AWS Console.
How can I pull the changes to the local machine, so that the next sam deploy command won't override them? (I am looking for something similar to a git pull I guess)
To do this you will need to use the AWS CLI, the start of this process will require you to use the get-function function in the AWS CLI.
This will return a pre signed URL in the Code > Location structure, if you then download this (using a CLI tool such as curl) you can then download a zip file containing the contents of the Lambda function.
The expected function would look similar to the below
curl $(aws lambda get-function --function-name $FUNCTION_NAME --output text --query "Code.[Location]")
You should have a single source of truth for your source code. And that should really be your source control repository (Git). If you make changes to your source code in the web IDE then you should copy those changes into your Git repo.
To your original question, to download a Lambda function's source code from the command line, you would use the aws lambda get-function command to download information about the function. Part of the information included in the response is a URL to download the function's deployment package, which is valid for 10 minutes. Then you could download the deployment package at that URL using something like curl.

Is it possible to update Lambda function with .jar uploaded to S3?

I'm trying to update Lambda code with .jar file uploaded to S3.
Direct zip file upload works fine
aws lambda update-function-code --function-name function
--zip-file fileb://function.zip
Direct jar file upload works fine
aws lambda update-function-code --function-name function
--zip-file fileb://function.jar
Update with zip file from S3 works fine
aws lambda update-function-code --function-name function --s3-bucket bucket
--s3-key function.zip
Problem appears only when updating function with jar from S3
aws lambda update-function-code --function-name function --s3-bucket bucket
--s3-key function.jar
Apparently AWS Lambda tries to unpack the jar:
An error occurred (InvalidParameterValueException) when calling the UpdateFunctionCode operation: Unzipped size must be smaller than 262144000 bytes
I couldn't find any clue in update-function-code reference https://docs.aws.amazon.com/cli/latest/reference/lambda/update-function-code.html
Is it possible to update Lambda function with jar file from S3?
If no, is there any reference saying so?
You've hit the 250MB upload limit, that's all I can say from the error message.
Solution: Work on your application, or ask support to raise the limit.
Lambda size can be 250 MB (unzipped, including layers) for s3 uploads.
Direct uploads is 50MB (compressed), but this goes little higher to ~70MB & it uses based64 encoding.
You don't see errors on directly uploading for compressed (zip/jar) file <70MB.
If you don't want jar files to be extracted, you must zip it. Zip will be extracted!
Could probably tell more if you update with size of zip, jar, dependencies and presence of layers.

AWS lambda update-function-code with jar package via AWS CLI

I'm trying to update my lambda function code with jar from my local machine via AWS CLI.
The aws lambda has commands to update function code for zip file but not for jar.
I can upload by using s3 bucket, but I need to update from local itself.
I know following are the way to update from S3 bucket and for zip:
aws lambda update-function-code --function-name
--s3-bucket --s3-key
aws lambda update-function-code --function-name
--zip-file "fileb://"
I want to ask is there similar command exist for uploading jar as well?
You're probably missing the "fileb://" part for the jar.
aws lambda update-function-code --function-name my-lambda-name --zip-file fileb://./target/my-lambda-jar.1.0-SNAPSHOT.jar
run this command:
aws lambda update-function-code --function-name my-lambda-name --zip-file fileb://./target/my-lambda-jar.1.0-SNAPSHOT.jar

How do you look at console.log output of the amazon lambda function

When you do a
console.log('Loading function');
in an amazon lambda function, where does that go?
My setup
api gateway
lambda function nodejs6.10
curl https://n2tredacted.execute-api.us-east-1.amazonaws.com/prod/redactedFunc
AWS Lambda logs are written to CloudWatch Logs. Here's how to access them:
select your Lambda function in the AWS console
click the Monitoring tab
choose View logs in CloudWatch
If you prefer to retrieve CloudWatch Logs outside of the AWS Console, then there are numerous CLI options:
awscli: aws logs get-log-events
github: jorgebastida/awslogs
github: TylerBrock/saw
serverless: sls logs (if using serverless)
samcli: sam logs (if using SAM)
There are 2 ways to access logs of your Lambda Function.
Method 1 (Using Serverless CLI):
Navigate to the root of your project folder and enter this in your command line:
sls logs -f myFunctionName -t
-f is for specifying the function name and -t is live tailing the logs in the command line.
Method 2 (Using CloudWatch Logs):
Go to CloudWatch in the region where your function was deployed and head to Logs. You will find the logs of your function there.
In the console Cloudwatch > Logs
Or with awscli:
aws logs get-log-events --log-group-name /aws/lambda/my_group_name_here --log-stream-name stream_name_here
Some special characters like $ need to be escaped with a preceding \
If you are testing this with API Gateway, console.log won't print in Test of API test.
But don't be confused it works fine and print logs in cloud watch.
Just go to monitoring tab of your lambda you will find your logs.