Creating AWS Lambdas using the AWS Command Line Interface

You can use Linux shell, Windows command line, or remotely trough for instance putty or terminal to use the AWS CLI. After you have installed the CLI you have to configure it for programmatic access to AWS. So, you have to create an access key and secret key. For this example you should create a new user, and give it access type Programmatic access. Give the user proper permissions – I will give the user AdministratorAccess for this post. Once that is done, you should create an access key and a secret key. Open up your console, type aws configure and fill in the information prompted for.

Note that you should be careful when using access keys. Make sure that you keep them safe, anyone having access to your keys will be able to perform the operations granted to the IAM user the keys are issued for.

You should also create an execution role for your function. In IAM console add a role that has permissions policy AWSLambdaBasicExecutionRole. Make a copy of the ARN to the role, as it is needed later when creating the function.

Now lets look at how we can start to create Lambdas. Type aws lambda help to get the AWS Lambda API Reference. Lets look at aws lambda create-function help.

“Creates a new Lambda function. The function configuration is created
from the request parameters, and the code for the function is provided
by a .zip file. The function name is case-sensitive.”

When creating a function you need to specify at least function name, runtime, role, handler, and the code for the function. That is, you have to give your function a name, you have to specify what runtime to use, you have to specify which execution role the function should use, you have to give the name of the method within you code that Lambda calls to begin execute the function, and lastly you provide the code as a zip-file.

aws lambda create-function 
--function-name MyCliLambdaFunction01 
--runtime nodejs8.10 
--role arn:aws:iam::13371337:role/LambdaExecutionRole 
--handler index.handler 
--zip-file "fileb://Index.zip"

The response from AWS when the function is created succesfully are quite verbose. As seen below in the confirmation, the method returns function name, function arn, choosen runtime, role for the function, handler, size of provided code, choosen timeout, and so on.

{
    "FunctionName": "MyCliLambdaFunction01",
    "FunctionArn": "arn:aws:lambda:eu-north-1:13371337:function:MyCliLambdaFunction01",
    "Runtime": "nodejs8.10",
    "Role": "arn:aws:iam::13371337:role/LambdaExecutionRole",
    "Handler": "index.handler",
    "CodeSize": 369,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-01-22T19:25:30.212+0000",
    "CodeSha256": "L712mUWN7et62IgYGBeyZ0fikp3l8=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "79ff3431-8f1-4d10-8867-d96fee4ef1507"
}

Ok, so now we have a function. Looking in the AWS Lambda Management Console we see that a function is created.

So, lets try and invoke the newly created function.

aws lambda invoke --function-name MyCliLambdaFunction01 outfile

This command invokes the function synchronous. The command will return the result of the execution, as seen below. The output from the function will be stored in the file named outfile.

{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}

So, we have created a function and invoked it through the CLI, and received a 200 as a response. What else is there we can do through the CLI. Lets see if we can add an environment variable and then change the behaviour of the function based on the contents of the environment variable.

Lets update the function to add a environment variable.

aws lambda  update-function-configuration 
--function-name MyCliLambdaFunction01 
--environment Variables={KeyName1=true,KeyName2=StringToPrint}

Now that we’ve added to variables, we have to update our code. I’ll update my node.js-code with an if-statement that evaluates KeyName1 and if it is true it returns KeyName2. This is the code:

This is how the CLI is called to update the code of the function by giving it a zip-file.

aws lambda update-function-code
--function-name MyCliLambdaFunction01 
--zip-file "fileb://UpdatedIndex.zip"

I invoke the function in the same way as above.

aws lambda invoke --function-name MyCliLambdaFunction01 outfile
{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}

And the content of outfile is:

"StringToPrint"

This is quite a simple use case, but it shows that it is possible to work with functions from the CLI without touching the console.

I will look further into this in the future.

Further reading:
https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html
https://docs.aws.amazon.com/cli/latest/reference/lambda/
https://docs.aws.amazon.com/lambda/latest/dg/env_variables.html

Node.js code
https://gist.github.com/mHallne/ac06cbed68ecab7e0d137dd357f14c51