DEV Community

Raz
Raz

Posted on

How to create and invoke a lambda using the AWS CLI

This article was also published on razcodes.dev

Let's say you create a cool little program that does some automation for you, or chore you need, and you then turn it into a lambda in AWS, but you don't want to go into the console and log in all the time you want to run it. You can invoke this program right from the AWS CLI, and here is how.

The Code

I will be creating a simple Node.js function that gives us a random Star Wars Quote every time we invoke it.

Using the terminal, create the folder and cd into it and create the function file

mkdir randomStarWarsQuoteGen
cd randomStarWarsQuoteGen
touch index.js

Using your favorite text editor add the code to the file.

vim index.js

Here is the simple code:

var messages = [
    'Help me, Obi-Wan Kenobi. You’re my only hope. — Leia Organa',
    'I find your lack of faith disturbing. — Darth Vader',
    'The Force will be with you. Always. — Obi-Wan Kenobi',
    'Never tell me the odds! — Han Solo',
    'Do. Or do not. There is no try. — Yoda',
    'No. I am your father. — Darth Vader',
    'There’s always a bigger fish. — Qui-Gon Jinn',
    'You can’t stop the change, any more than you can stop the suns from setting. — Shmi Skywalker',
    'I’m just a simple man trying to make my way in the universe. — Jango Fett',
    'Power! Unlimited power! — Darth Sidious'
];

exports.handler = async (event) => {
    let message = messages[Math.floor(Math.random()*10)];
    return message;
};

The Lambda Role

To follow along from here you will need to have the AWS CLI installed and configured on your machine. My last article covered that, so make sure you go and read it if you need to.

The lambda will need a role so let's create one. First we need to create a new file called trust.json for the assume role policy.

touch trust.json
vim trust.json

Add the following to the file:

{
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Principal": {
          "Service": "lambda.amazonaws.com"
        },
        "Action": "sts:AssumeRole"
      }
    ]
}

Now we can create the role using the CLI:

aws iam create-role --role-name randomStarWarsQuoteGenRole --assume-role-policy-document file://trust.json --description "Random Star Wars Quote Generator Role"

Make sure you copy down somewhere the result ARN as we will need to use it to create the function.

Now let's attach to that new role the AWSLambdaBasicExecutionRole policy, which is managed by AWS.

aws iam attach-role-policy --role-name randomStarWarsQuoteGenRole --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

The Lambda

With the role created and policy attached, we can now create the function.

First, create a zip file of your code:

zip function.zip index.js

Now you are ready to create the function, making sure to replace your-role-arn with the ARN that you wrote down in the above step.

aws lambda create-function --function-name randomStarWarsQuoteGen --runtime nodejs12.x --handler index.handler --role <your-role-arn> --zip-file fileb://function.zip

If you need to update the lambda code in the future, you can just update your code, create the zip file again and then use the update command:

aws lambda update-function-code --function-name randomStarWarsQuoteGen --zip-file fileb://function.zip

Invocation Time

To call the function, you simply use the following command, where result.json is the file that will contain the function response.

aws lambda invoke --function-name randomStarWarsQuoteGen result.json

Happy Coding!

Top comments (3)

Collapse
 
rubenslj00 profile image
Ruben Louis Jean

Thanks

Collapse
 
rubenslj00 profile image
Ruben Louis Jean

Uploading image

Collapse
 
rubenslj00 profile image
Ruben Louis Jean

That is very helpful