AWS Lambda Functions With AWS CLI
Last Updated :
04 Jan, 2025
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 the AWS CLIConfigure 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 profileFill 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 Function2. Listing Lambda Functions
The below command will list all the lambda functions in the account.
aws lambda list-functions
List of Lambda Functions3. 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 Function4. 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 Function asynchronously5. 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.
Publish Lambda Function6. 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"
Add Tags for Lambda Function8. 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 Function9. 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
Delete Lambda Function using different methodsConclusion
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