11
votes

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?

2
Try wrapping Variables={DEV_URL=dev,PROD_URL=prod} in quotes?Anthony Neace

2 Answers

28
votes

You should try with Variables="{KeyName1=string,KeyName2=string}"

1
votes

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