---
title: "Getting Started with AWS Lambda CLI"
description: "Learn the ins-and-outs of the AWS Lambda CLI and make deployments and management through this example-driven tutorial by ATA Learning!"
canonical: "https://adamtheautomator.com/aws-lambda-cli/"
---

# Getting Started with AWS Lambda CLI

> Learn the ins-and-outs of the AWS Lambda CLI and make deployments and management through this example-driven tutorial by ATA Learning!

Source: https://adamtheautomator.com/aws-lambda-cli/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Getting Started with AWS Lambda CLI](https://adamtheautomator.com/wp-content/uploads/2023/01/aws-lambda-cli.jpg)

# Getting Started with AWS Lambda CLI

[![](https://secure.gravatar.com/avatar/1bc5e1d52466fc3739f550c8d78be310684747bf1466a98d698320627ea243e2?s=192&d=mm&r=g)Michael Nguyen Tu](https://adamtheautomator.com/author/michael-nguyen-tu/)5 January 20236 min. read

Categories: [DevOps](/category/devops/)

Tags:[AWS](/tag/aws/)[AWS Lambda](/tag/aws-lambda/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating an Execution Role](#creating-an-execution-role)
*   [Creating a Deployment Package](#creating-a-deployment-package)
*   [Creating a Lambda Function](#creating-a-lambda-function)
*   [Invoking the Lambda Function](#invoking-the-lambda-function)
*   [Deleting a Lambda Function](#deleting-a-lambda-function)
*   [Conclusion](#conclusion)

Are you ready to take your serverless computing skills to the next level? Look no further! The AWS Lambda CLI is here to help you quickly manage and invoke your Lambda functions from the command line.

With the AWS Lambda CLI, you have all the power of the Lambda platform at your fingertips to easily create, deploy, and invoke your functions in response to various events. And in this tutorial, you will learn to jumpstart your journey toward becoming a serverless pro with AWS Lambda CLI.

Dive in and start building scalable, reliable applications in the cloud!

## Prerequisites

This tutorial will be a hands-on demonstration. To follow along, be sure you have the following:

*   An [AWS account](https://repost.aws/knowledge-center/create-and-activate-aws-account) with active billing – The [free tier](https://aws.amazon.com/free/) will suffice.
*   A computer (Linux, macOS, or Windows) AWS CLI [installed](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and [configured](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) – This tutorial uses Ubuntu 20.04.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

An IAM user with programmatic access (Access Key and Secret Key) to make API calls to AWS Lambda.

Related:[Helpful Guide to IAM in AWS Through Examples](https://adamtheautomator.com/iam-aws/)

## Creating an Execution Role

Before starting with the AWS Lambda CLI, you must create a dedicated Identity and Access Management (IAM) role for your Lambda functions.

An AWS Lambda function functions based on an execution role and a deployment package:

*   The execution role grants the necessary permissions to execute your Lambda functions
*   The deployment package contains all the code for your functions.

To create an execution role, follow these steps:

1\. Run the following command to create a new directory (`mkdir`) as your project directory, and move into it. This directory is where you will store all your Lambda functions and code and run subsequent commands.

You can name the directory differently, but this tutorial uses `my-lambda-project`.

```bash
mkdir my-lambda-project && cd my-lambda-project
```

![Creating a project directory](https://adamtheautomator.com/wp-content/uploads/2023/01/image-71.png)

Creating a project directory

2\. Next, create a JSON file named `trust-policy.json` with your preferred text editor, and populate the code below into the file.

The code below contains the trust policy for your IAM role (execution role), which defines the permissions granted. These permissions specifically `“Allow"` the `"lambda.amazonaws.com"` service to assume your IAM role (`"sts:AssumeRole"`).

By specifying the permissions granted to your function via the execution role, you can ensure that the role has access to the resources it needs and nothing more. Moreover, permissions help protect your resources and prevent unauthorized access or actions.

Related:[How to Edit Files with a Real PowerShell Text Editor](https://adamtheautomator.com/powershell-text-editor/)

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

Run the [`aws iam`](https://docs.aws.amazon.com/cli/latest/reference/iam/%3E\)%20command%20below%20to%20create%20an%20execution%20role%20\(%5Bcreate-role%5D\(%3Chttps://docs.aws.amazon.com/cli/latest/reference/iam/create-role.html) for your Lambda function named `lambda-ex`. The file, `trust-policy.json`, is located in the same directory where this command is run from.

```bash
aws iam create-role --role-name lambda-ex --assume-role-policy-document file://trust-policy.json
```

As shown below, the output indicates the role is created successfully.

![Creating an execution role](https://adamtheautomator.com/wp-content/uploads/2023/01/image-72.png)

Creating an execution role

4\. Lastly, run the command below to attach [`attach-role-policy`](https://docs.aws.amazon.com/cli/latest/reference/iam/attach-role-policy.html) to your IAM role. This policy grants the permissions needed for your Lambda functions to execute.

```json
aws iam attach-role-policy --role-name lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
```

![Attaching the required IAM policy to the IAM role](https://adamtheautomator.com/wp-content/uploads/2023/01/image-73.png)

Attaching the required IAM policy to the IAM role

## Creating a Deployment Package

With the role and permissions in place, you need a way to put your project resources in one place. How? You will create a deployment package containing all the necessary files, including your code and any required libraries, in a single package.

To create a deployment package, you will create a simple Node.JS function. This function logs the environment variables and event data to the console and returns the log stream when invoked.

> 💡 _Note that you must write the code for your function in a [language supported by AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html)._

Create a new JavaScript file named `index.js`, populate the code below for your AWS Lambda function, and save and close the file.

This file is the entry point for your function. When your function is invoked, the code in this file executes.

```javascript
// Exports the handler function as a Lambda function
exports.handler = async function(event, context) {
// Logs all environment variables to the console.
  console.log("ENVIRONMENT VARIABLES\\n" + JSON.stringify(process.env, null, 2))
// Logs the incoming event to the console
  console.log("EVENT\\n" + JSON.stringify(event, null, 2))
// Returns the log stream name of the current execution context
  return context.logStreamName
}
```

Lastly, run the below [`zip`](https://adamtheautomator.com/unzip-files-in-linux/) command to create a deployment package (`*function.zip*`) for your Lambda function (`*index.js*`).

> 💡 _A deployment package makes managing your function easier and ensures everything it needs is available when invoked._

Related:[Learn How to Zip and Unzip Files in Linux and be a Zip Master](https://adamtheautomator.com/unzip-files-in-linux/)

```bash
zip function.zip index.js
```

![Creating a deployment package](https://adamtheautomator.com/wp-content/uploads/2023/01/image-74.png)

Creating a deployment package

## Creating a Lambda Function

Now that you have both the execution role and the deployment package needed to create a function, it is time to create your first Lambda function.

To create a Lambda function, you first need your account ID handy:

1\. Run the below [`aws sts`](https://docs.aws.amazon.com/cli/latest/reference/sts/) command to find your account ID ([`get-caller-identity`](https://docs.aws.amazon.com/cli/latest/reference/sts/get-caller-identity.html)).

```bash
aws sts get-caller-identity
```

Note the Account ID from the output, as you will need it to create your Lambda function.

![Finding your account ID](https://adamtheautomator.com/wp-content/uploads/2023/01/image-75.png)

Finding your account ID

2\. Next, run the [`aws lambda`](https://docs.aws.amazon.com/cli/latest/reference/lambda/index.html) command below to create a new Lambda function ([`create-function`](https://docs.aws.amazon.com/cli/latest/reference/lambda/create-function.html)) named `my-first-function`. Replace `<YOUR_ACCOUNT_ID>` with your actual AWS account ID and `my-first-function` with your preferred function name.

The command below specifies the following:

<table><tbody><tr><td><code>--runtime nodejs18.x</code></td><td>The runtime for your function is the programming language in which your function is written.</td></tr><tr><td><code>--zip-file fileb://function.zip</code></td><td>The name and location of the deployment package for your function.</td></tr><tr><td><code>--zip-file fileb://function.zip</code></td><td>The function handler for your function (the entry point for your function code). The function handler is typically in the form of filename.handler, where filename is the name of your code file, and handler is the name of the function within that file.</td></tr></tbody></table>

```bash
aws lambda create-function --function-name my-first-function \
--zip-file fileb://function.zip  --runtime nodejs18.x \
--role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/lambda-ex
```

![Creating a Lambda function](https://adamtheautomator.com/wp-content/uploads/2023/01/image-76.png)

Creating a Lambda function

3\. Now, run the below command to list all available functions (`list-functions`) in your account. This command comes in handy when you lose track of your functions.

```bash
aws lambda list-functions
```

As shown below, you will see all functions you have in your account, including your newly-created function, with their names and details.

![Listing all available functions](https://adamtheautomator.com/wp-content/uploads/2023/01/image-77.png)

Listing all available functions

## Invoking the Lambda Function

With your Lambda function successfully created, how do you know it actually works? You must invoke your function using the AWS Lambda CLI [`invoke`](https://docs.aws.amazon.com/cli/latest/reference/lambda/invoke.html) subcommand.

Run the below `aws lambda` command to `invoke` your Lambda function (`my-first-function` ), which includes the execution log (`Tail`) in the output.

```bash
aws lambda invoke --function-name my-first-function out --log-type Tail
```

Below, the output looks like a mess since it is just a plain string encoded as base64. Jump to the following step to decode and see the actual output of the function.

![Invoking the Lambda function](https://adamtheautomator.com/wp-content/uploads/2023/01/image-78.png)

Invoking the Lambda function

Now, run the same command below, but this time decode (`--decode`) the function’s output.

This command keeps the connection open (`RequestResponse`) until the function returns a response or times out.

```bash
aws lambda invoke --function-name my-first-function --invocation-type RequestResponse --log-type Tail - | jq '.LogResult' -r | base64 --decode
```

This time, you can see the output below is readable, containing the environment variables and event data logged in your _index.js_ file.

![Invoking the Lambda function with decoded output](https://adamtheautomator.com/wp-content/uploads/2023/01/image-79.png)

Invoking the Lambda function with decoded output

## Deleting a Lambda Function

Having too many functions can be a pain and incur unnecessary charges. When you are done with your functions, deleting them is best to clean up and prevent breaking the bank.

Run the below command, which does not provide output, but deletes (`delete-function`) a specific Lambda function (`my-first-function`).

Ensure you got the `--function-name` right, and run the `aws lambda list-functions` command first to be on the safe side. Otherwise, you might be deleting a precious function you spent plenty of time creating.

```bash
aws lambda delete-function --function-name my-first-function
```

Now, run the below command to list all functions (`list-functions`) and verify that the function has been deleted.

```bash
aws lambda list-functions
```

The output below shows a blank output since not even one function exists.

![Verifying a function has been deleted](https://adamtheautomator.com/wp-content/uploads/2023/01/image-80.png)

Verifying a function has been deleted

## Conclusion

Lambda functions let you quickly build and deploy cloud-based applications without the hassle of setting up and maintaining servers. And in this tutorial, you have learned how the AWS Lambda CLI allows you to create, invoke, and delete Lambda functions easily. These features make the AWS Lambda CLI an essential tool for any developer working with Lambda.

By now, you understand the basics of working with the AWS Lambda CLI and be confident in creating your own serverless applications. But the journey continues as there are endless possibilities for what you can do with AWS Lambda and the CLI!

AWS Lambda CLI boosts your ability to build scalable, reliable applications in the cloud. Why not take the next step and [update yo](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/update-function-code.html)[u](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/update-function-code.html)[r function](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/update-function-code.html) to add even more functionality?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Faws-lambda-cli%2F&text=Getting%20Started%20with%20AWS%20Lambda%20CLI)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Faws-lambda-cli%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Faws-lambda-cli%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/12/aws-lambda-environment-variables.jpg)

### [Getting Started with AWS Lambda Environment Variables](/aws-lambda-environment-variables/)

Learn through examples how to use AWS Lambda environment variables and create robust serverless code in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/How-to-Leverage-AWS-Lambda-Go-as-an-IT-System-Administrator.jpg)

### [How to Leverage AWS Lambda Go as an IT System Administrator](/aws-lambda-go/)

Deploy and use AWS Lambda Go applications from the edge in this step-by-step walkthrough and take your IT game to the next level!

![](https://adamtheautomator.com/wp-content/uploads/2021/07/How-to-Provision-AWS-Lambda-Functions-with-Terraform.jpg)

### [Terraform and AWS Lambda : The Right Way to Go](/terraform-and-aws-lambda/)

Using Terraform and AWS Lambda functions, build a serverless infrastructure in no time with this step-by-step tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
