How can I create an AWS Lambda function using the AWS CLI? - amazon-web-services

I am trying to create an AWS Lambda function using the command
aws lambda create-function \
--function-name foo\
--runtime nodejs\
--role lambda_basic_execution \
--handler asdf --zip-file "fileb:://boom.zip"
I have a file called boom.zip available in the directory. But I cannot deploy using the above command.
The failure message I get is
--zip-file must be a file with the fileb:// prefix.
Does anyone have a working example to create a lambda function using the AWS CLI?

You have an extra colon ':' in the file spec.
$ aws lambda create-function --function-name foo --runtime nodejs --role lambda_basic_execution --handler asdf --zip-file "fileb:://boom.zip"
--zip-file must be a file with the fileb:// prefix.
Example usage: --zip-file fileb://path/to/file.zip
$ aws lambda create-function --function-name foo --runtime nodejs --role lambda_basic_execution --handler asdf --zip-file "fileb://boom.zip"
Error parsing parameter '--zip-file': Unable to load paramfile fileb://boom.zip: [Errno 2] No such file or directory: 'boom.zip'

On mac I had to use absolute path, but added to the prefix there are actually 3 slashes.
Prefix:
fileb://
Path
/Users/myuser/Apps/folder/zips/file.zip
Complete
fileb:///Users/myuser/Apps/folder/zips/file.zip

I've had the same issue on Ubuntu 18.04 and what did the trick was enclosing both the name of the function and the fileb:/// with double " quotes.
aws lambda update-function-code --function-name "FUNCTION" --zip-file "fileb:///an/absolute/path/to/your/lambda/FUNCTION.zip"

For me, the issue occurred (in windows) because I didn't create the zip file correctly. I used the winrar tool to create the .rar file, changed the extension to .zip, and tried to upload the zip in the same way #helloV said (without double ::), but I got the same error message. If you are in windows make sure to follow this guide (for golang), or use any other suitable tool to create a correct zip file and try the command out again.

Related

How can I update my AWS Lambda function from VSCode?

So I have an AWS Lambda function written in NodeJS, but I am tired of coding in the AWS Console or having to manually zip my code in my VSCode to manually upload it in he AWS Console.
I know that I can update my function with aws lambda update-function-code --function-name myFunction --zip-file "fileb://myZipFile". But how can I zip it and launch this command every time I save my work in VSCode ?
Also, I am on Windows.
You can't do this without some additional work.
A few options are:
use the Run on Save VS Code extension and configure a custom command to run when a file is saved
create a SAM project and install the AWS Toolkit for VS Code extension to provide deployment assistance
create a package.json that includes a script for zip/deployment and use the NPM extension for VS Code to execute the deploy script
build a CI/CD solution: use VS Code to commit and push your code, then the pipeline takes over and deploys it
use a shell script, or a Makefile with a target, that zips and deploys and then simply execute it, manually or otherwise, in the VS Code in-built terminal
I use a script with below and run it when need to update.
echo "Building zip file"
zip -rq testfunction.zip testfunctionfolder/
echo "update Lambda function"
FUNCTION_ARN=$(aws lambda update-function-code \
--function-name testfunction \
--zip-file fileb://testfunction.zip \
--query 'FunctionArn' \
--output text)
echo "Lambda function updated with ARN ${FUNCTION_ARN}"

Is there a way to set the Amazon S3 link URL in Lambda all from the AWS-CLI?

Basically, I want to know if I can configure everything you see in this image from the AWS CLI. It seems like I can't.
Can I modify this Lambda config to provide an S3 link and function handler all from the CLI?
Yes, using the create-function call.
aws lambda create-function \
--runtime python3.6 \
--handler file_name.lambda_handler \
--code S3Bucket=mybucket,S3Key=path/to/object.zip

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

AWS lambda function with python "errorMessage": "Unable to import module 'index'"

i am trying to make a post call from lambda function but not able to run the code on aws console but it it working properly on my system.
You need to install the dependencies in the folder where you have index.py then you need to zip the contents of the folder and upload the zip file to AWS Lambda.
Please note that you need to zip the contents of the folder, do not zip the folder itself.
On windows, you can install the packages in the folder using below command:
pip install package-name -t "/path/to/project-dir"
I had this error today, and this is the first result on Google, so I'll add my answer. In short, I had specified the handler incorrectly on the command line when I uploaded the function.
aws lambda create-function --function-name python-test-lambda --runtime python3.7 --role arn:aws:iam::123123123123:role/service-role/rolearn --handler lambda_function.lambda_handler --zip-file fileb://lambda_function.zip
ie this part was incorrect
--handler

AWS lambda create-function accepts only one environment variable

I am trying to use environment variables for my lambda but when I run the following Lambda AWS CLI create-function command from a gradle task that runs a shell script,
aws $PROFILESTR lambda create-function \
--region us-east-1 \
--function-name MyLambda \
--zip-file fileb://$ZIP \
--role arn:aws:iam::$AWS_ACCT_ID:role/my_lambda \
--handler com.test.MyLambda::handleRequest \
--runtime java8 \
--description "Lambda description..." \
--memory-size 256 \
--timeout 45 \
--environment Variables={DEV_URL=dev,PROD_URL=prod}
it gives me this message and doesn't create the lambda function
:my-lambda:createLambda
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
Unknown options: Variables=PROD_URL=prod
:my-lambda:createLambda FAILED
If I remove the second variable (declare only one env variable), it creates the function just fine.
From the lambda create-function cli documentation, the environment option accepts multiple variables:
--environment (structure)
The parent object that contains your environment's configuration
settings. Shorthand Syntax:
Variables={KeyName1=string,KeyName2=string}
I should be able to create the lambda through a script and not through AWS console (release should be automated).
What am I missing here or what am I doing wrong?
You should try with Variables="{KeyName1=string,KeyName2=string}"
The other option is to put the environment variables in a json file (ex: environment.json) and use that instead... and:
% aws lambda update-function-configuration --function-name MyLambda --environment file://environment.json
... or ... you could also use the file://environment.json command in your create-lambda statement