Open In App

AWS Lambda Functions With AWS CLI

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Amazon Web Services (AWS) is a comprehensive cloud computing platform offering many services, including storage, computing, databases, and more. AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). we can create functions and self-contained applications.

With AWS Lambda functions, we can perform any kind of computing task, from serving web pages to building backend APIs, and we can integrate Lambda with other AWS services as well, In this article, we are integrating Lambda with API Gateway.

AWS CLI

Amazon’s command-line interface (CLI) is a powerful tool that allows users to interact with various AWS services through a command-line interface.

Install AWS CLI

Assuming you already have an AWS account, follow the steps below to install AWS CLI on your system (these steps are based on Ubuntu OS).

You can either follow the instructions from the AWS documentation or run the below commands in your terminal to install AWS CLI in your system

sudo apt-get install awscli -y
install_aws_cli-(1)

Install the AWS CLI

Configure AWS Credentials

  • Login to AWS Console
  • Click on your username at top right corner and click on Security Credentials
  • Under Access keys click on Create access key –> Choose Command Line Interface (CLI) –> add some description for it -> Create
  • Either copy Access key ID and Secret access key displayed on the screen or download csv file.
aws configure --profile <profile-name>

For example:

aws configure --profile dillip-tech
Configure AWS CLI with custom profile

Configure AWS CLI with custom profile

Fill the prompts for access key and secret you’ve copied in above steps, and now you’re all set to tryout AWS IAM resources.

Key Features of AWS CLI for AWS Lambda:

  • Function Management: You can create, update, and delete Lambda functions using the CLI.
  • Invocation: Invoke Lambda functions with specific input data.
  • Configuration: Set function-specific configuration options.
  • Versioning and Aliases: Manage function versions and aliases.
  • Event Sources: Configure event sources (such as S3 buckets, API Gateway, etc.) for your Lambda functions.
  • Logging and Monitoring: Retrieve logs and metrics for your functions.

AWS CLI for AWS Lambda:

Now, let’s dive into some practical examples of using the AWS CLI for Lambda:

1. Create Lambda Function

To create Lambda function using the CLI, we can use the below command.

aws lambda create-function --function-name your-function-name --runtime python3.10 --zip-file fileb://myfiles/my-function.zip --handler lambda_function.handler --role arn:aws:iam::111111111111:role/service-role/MyTestFunction-role-xyz

It will return the basic function details like below:

create-lambda-function

Create Lambda Function

2. Listing Lambda Functions

The below command will list all the lambda functions in the account.

aws lambda list-functions
list-aws-functions

List of Lambda Functions

3. Invoke Lambda Function

We can invoke the lambda function using the cli, with or without specifying the alias, and we can store the output as well in the file as shown below

aws lambda invoke --function-name your-function-name --qualifier my-alias output.json
invoke-lambda-function-(1)

Invoke Lambda Function

4. Invoke Function Asynchronously

We can also invoke the lambda function asynchronously using the invoke-async, this will allow us to invoke function without waiting for the response.

aws lambda invoke-async --function-name your-function-name --invoke-args input.json
invoke-lambda-async-(1)

Invoke Lambda Function asynchronously

5. Publish Version

We can name publish version from the code, this can be used to store different versions of code with appropriate aliases (we’ll learn about aliases in the below steps), here is the example of publishing version.

pubish-lambda-function

Publish Lambda Function

6. Create Alias for Function

We can create an alias (such as “prod” or “staging”) for a specific version of a Lambda function using the aws lambda create-alias command, using the alias we can many things like blue green deployments, and also we can access application with specific alias, same function with different versions of deployment.

aws lambda create-alias --function-name your-function-name --name prod --function-version 2

Create Lambda Alias

7. Tag-resource

To add tags for the existing lambda function we can use the below command, this command produce no ouput.

aws lambda tag-resource --resource arn:aws:lambda:ap-south-1:1234567890:function:my-gfg-function    --tags "author=Dillip"
tag-lambda-function

Add Tags for Lambda Function

8. Get Function

To get the function details we can run the below command, this command also returns an downloadable code link, that will expire after 10 minutes.

aws lambda get-function --function-name  my-gfg-function
Get Lambda Function

Get Lambda Function

9. Delete Function

We can delete a lambda function by using the function name or function arn , or even with partial arn, refer the below examples. The below commands do not produce any outputs.

Method-1 With Function Name: The below command deletes the function my-gfg-function

aws lambda delete-function --function-name my-gfg-function

Method-2 With Function Arn: The below command deletes the function my-gfg-another-function, here we’ve passed arn as an input

aws lambda delete-function --function-name arn:aws:lambda:ap-south-1:1234567890:function:my-gfg-another-function

Method-3 With Partial Function Arn: The below command deletes the function my-gfg-another-function, here we’ve passed partial arn as an input

aws lambda delete-function --function-name 1234567890:function:my-gfg-another-function
lambda-delete-fucntions-(1)

Delete Lambda Function using different methods

Conclusion

In this article we’ve learnt various commands to work with AWS Lambda in CLI, you can try various other commands available to get done tasks in the cli itself.

By leveraging these commands, you can streamline your serverless development workflow, handle tasks efficiently from your terminal, and gain greater control over your Lambda deployments

Lambda Functions with AWS CLI – FAQ’s

What does the –zip-file flag do in the create-function command?

The –zip-file flag specifies the path to the ZIP file containing your Lambda function code. When creating a new function, this flag is used to upload your code to Lambda.

Can I use the invoke command to test a Lambda function with different input payloads?

Yes, the invoke command allows you to test your Lambda function with different input payloads. You can provide the input payload as a JSON file or inline JSON within the command.

How do I use the create-alias command to manage different versions of my Lambda function?

The create-alias command lets you assign a friendly name (alias) to a specific version of your Lambda function. This makes it easier to manage and invoke different versions without changing the actual function name.

What is the purpose of the tag-resource command in Lambda?

The tag-resource command is used to add metadata to your Lambda function in the form of key-value pairs. These tags can help you organize and manage your Lambda functions, especially when you have many functions in your account.

How can I retrieve detailed information about a specific Lambda function using the AWS CLI?

You can use the get-function command to retrieve detailed information about a specific Lambda function. This includes the function’s configuration, code, and tags, among other details.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads