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
Related
I am working on adding Dynatrace monitoring as a layer to a Lambda serverless function in a nested stack. The environment variables needed for the Dynatrace layer are being passed from the root stack to the serverless function stack where they are received in a Globals block.
One of the needed environment variables is AWS_LAMBDA_EXEC_WRAPPER, and I am trying to pass a relative path of /opt/dynatrace.
I am developing and testing using bash shell scripts on my workstation, so I am using a script to execute the aws cloudformation deploy command and passing parameter overrides for the Dynatrace monitoring. The variable in question is using pDtLambdaExecWrapper=/opt/dynatrace.
When passing the value as /opt/dynatrace, the path is being parsed as C:/Program Files/Git/opt/dynatrace, so gives an error that it does not exist.
I tried passing the value as ./opt/dynatrace, but instead get an error, ./opt/dynatrace: does not exist.
I am using a Windows 10 workstation, and using Git Bash for my terminal shell emulator.
Is there a way to pass in a relative file path from the AWS command line using the parameter-overrides option on the aws cloudformation deploy command in a way that will pass just /opt/dynatrace?
Deploy command below. I've removed or changed some of the other parameters being passed in to save space.
aws --region $REGION cloudformation deploy \
--template-file $OUTPUT_TEMPLATE_FILE \
--stack-name $STACK_NAME \
--no-fail-on-empty-changeset \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--parameter-overrides \
pDtTenant=26571e7e-xxxx-4fed-a826-67b899abfee8 \
pDtClusterId=-nnnnnnnnnn \
pDtLambdaExecWrapper=/opt/dynatrace \
pDtLambdaLayerArn=arn:aws:lambda:us-east-1:xxx:1
I am trying to create a lambda function through the AWS command.When I'm executing the below command Its gives me the Unknown option error.
AWS Command:
aws lambda create-function --function-name function_name ukmon-appd-disabled-
health-rules --runtime python3.7 --zip-file
fileb://bin/disabled_health_rules.zip --handler index.handler --timeout 10 -
-memory-size 1024 --role arn:aws:iam::99999999999:role/crossaccount
Error:
Unknown options: ukmon-appd-disabled-health-rules
I've tried with different names.I am not able to understand what does that error mean. Thanks in advance.
Remove the function_name. It is being recognized as the value to the parameter --function-name instead of the ukmon-appd-disabled-health-rules.
You have the argument function-name written twice. Just remove one like this
aws lambda create-function --function-name ukmon-appd-disabled-
health-rules --runtime python3.7 --zip-file
fileb://bin/disabled_health_rules.zip --handler index.handler --timeout 10 -
-memory-size 1024 --role arn:aws:iam::99999999999:role/crossaccount
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
My CI pipeline will do two things
generate new lambda version and publish
Update an alias to point at that new version
This will be done via cli commands. My question is, how do I access the version number that been generated from the first command. It is returned and posted to the CLI. Can this be access easily via some nifty was command or will I have to parse it myself?
e.g.
version=$(aws lambda publish-version \
--function-name test_lambda --description "updated via cli" --region eu-west-1 \
--query Version \
--output text)
See Controlling Command Output from the AWS Command Line Interface page of AWS CLI User Guide, specifically How to Filter the Output with the --query Option and Text Output Format
This works but still curious if there is a better way.
version=$(aws lambda publish-version --function-name test_lambda --description "updated via cli" --region eu-west-1| jq '.Version')
NEW_LAMBDA_VERSION=$(aws lambda list-versions-by-function --function-name $LAMBDA_NAME_FOR_DEPLOY --no-paginate --query "max_by(Versions, &to_number(to_number(Version) || '0'))")
NEW_LAMBDA_VERSION=$(echo $NEW_LAMBDA_VERSION | jq -r .Version)
echo $NEW_LAMBDA_VERSION
In this case, I use on .gitlab-ci.yml.
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.