Invoke AWS Lambda functions with using AWS CLI Commands

In this article we are going to Develop AWS Lambda Functions with using AWS CLI. These will be the part of Programmatic Access with using Serverless APIs.

Invoke AWS Lambda functions with using AWS CLI Commands

By the end of the article, we will create, develop, deploy and invoke AWS Lambda function with using AWS CLI Commands.

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

AWS Access Types

As you remember AWS gives us 2 main way to call their Serverless APIs.

1- AWS management console access; which we have already see that you can think as a web application allows us to manage AWS resources for particular AWS accounts.
2- Programmatic access; which gives us to manage AWS resources from our development environments and manage by writing codes. This access is really helpful when you manage large scale of resources on AWS.

You can find different ways to use Programmatic accesses;

  • AWS CLI, AWS SDK, AWS CloudFormation — IaC, AWS SAM, AWS CDK

So for AWS Lambda, we will use AWS CLI commands for programmatic access.

AWS CLI Lambda Interactions

What we will do for interacting AWS Lambda using AWS CLI ?

  • Create lambda functions
  • Develop lambda functions
  • Zip and deploy function
  • Invoke and test function

Prerequisites and Tools

Before we start, we should have 5 main Prerequisites for now;

  • AWS Account and User: we will use AWS free tier account
  • AWS CLI: required for any interaction on AWS
  • Node.js: will be runtime of our AWS lambda
  • IDE: developing lambda functions = vs code

Using AWS Lambda with the AWS CLI

We are going to learn how to use Lambda with using AWS CLI commands. See basic CLI commands:

 aws --version
aws lambda list-functions
aws lambda get-function — function-name my-function

As you can see that CLI commands starts with “aws lambda” and perform actions with list, get, create and so on. If there is no function , we can create a basic lambda function with default configurations.

Where we find these cli command for AWS Lambda ?

From The AWS Lambda API Reference document. Use link and examine cli commands.

The Lambda API Reference provides information about each of the API methods, including details about the parameters in each API request and response.

Invoke Lambda functions with the AWS CLI

We can invoke Lambda functions with different ways, invoke directly using the Lambda console, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits.

We can also configure other AWS services to invoke your function, or we can configure Lambda to read from a stream or queue and invoke our function.

Before we start to invoke a lambda function we should understand the invoke types. When you invoke a function, we can choose to invoke it synchronously or asynchronously. With synchronous invocation, you wait for the function to process the event and return a response. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately. Ok lets focus on synchronous invocation.

Invoke AWS Lambda functions with using AWS CLI Commands

When we invoke a function synchronously, Lambda runs the function and waits for a response. When the function completes, Lambda returns the response from the function’s code with additional data, such as the version of the function that was invoked. In order to invoke a function synchronously with the AWS CLI, use the invoke command.

aws lambda invoke — function-name my-function — cli-binary-format raw-in-base64-out — payload ‘{ “”key””: “”value”” }’ response.json

You should see the following output:

{
“StatusCode”: 200,
“ExecutedVersion”: “$LATEST”
}

We can also verify lambda execution from amazon cloudwatch logs. We can also set Multiline Characters when running invoke command on command window.

aws lambda invoke \
— function-name my-function \
— cli-binary-format raw-in-base64-out \
— payload ‘{ “”key””: “”value”” }’ \
response.json

Invoking Lambda functions with passing event.json file using AWS CLI

We are going to learn invoke Lambda functions with passing event.json file using AWS CLI.

Invoking Lambda functions with passing event.json file using AWS CLI

As you know that AWS lambda functions invoking with event objects so events are the fuel of lambda functions. So its good practice to create event.json file in order to store these payload data.

Create event.json file into folder:

{
“key”:”value”
}

Invoke Lambda Function with passing event.json file:

aws lambda invoke `
— function-name my-function `
— cli-binary-format raw-in-base64-out `
— payload file://event.json `
response.json

See response:

{
“StatusCode”: 200,
“ExecutedVersion”: “$LATEST”
}

We can also verify lambda execution from cloudwatch logs.

You can see the image on below that shows clients invoking a Lambda function synchronously.

Invoking Lambda functions with passing event.json file using AWS CLI

Lambda sends the events directly to the function and sends the function’s response back to the invoker. The payload is a string that contains an event in JSON format. The name of the file where the AWS CLI writes the response from the function is response.json. If the function returns an object or error, the response is the object or error in JSON format. If the function exits without error, the response is null.

Step by Step Design AWS Architectures w/ Course

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

In this course, we will learn almost all the AWS Serverless Services with all aspects. We are going to build serverless applications with using AWS Lambda, Amazon API Gateway, Amazon DynamoDB, Amazon Cognito, Amazon S3, Amazon SNS, Amazon SQS, Amazon EventBridge, AWS Step Functions, DynamoDB and Kinesis Streams. This course will be 100% hands-on, and you will be developing a real-world application with hands-on labs together and step by step.

Source Code

Get the Source Code from Serverless Microservices GitHub — Clone or fork this repository, if you like don’t forget the star. If you find or ask anything you can directly open issue on repository.

--

--

Mehmet Ozkaya
AWS Lambda & Serverless — Developer Guide with Hands-on Labs

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya