Table of contents
AWS Command Line Interface (CLI) is a powerful tool that allows developers and system administrators to manage their AWS resources from the command line.
AWS Lambda is a powerful serverless computing service that lets you run code without provisioning or managing servers. In this blog post, we will explore some of the most useful AWS CLI commands for the AWS Lambda function.
Before running the AWS CLI commands, we need to Install the AWS CLI on your computer. You can download the CLI from AWS CLI. Open a command prompt or terminal window and run the following command.
aws configure
When prompted, enter the access key ID and secret access key. Enter the default region you want to use for the CLI. Optionally, you can also set a default output format for the CLI. Once you have entered all the required information, the AWS CLI will be configured and ready to use.
With the AWS CLI setup, you can now start using it to manage your AWS resources from the command line.
Create a Lambda function
You'll need to provide the function name, runtime, handler function name, IAM role ARN, and the path to your function package:
aws lambda create-function --function-name my-lambda-function --runtime nodejs16.x --role arn:aws:iam::123456789012:role/my-lambda-role --handler index.handler --zip-file fileb://function-package.zip
To allow your Lambda function to access other AWS resources, you need to create an IAM role that grants the necessary permissions. You can use the AWS CLI to create an IAM role with the required permissions.
aws iam create-role --role-name my-lambda-role --assume-role-policy-document file://trust-policy.json
aws iam attach-role-policy --role-name my-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Invoke a Lambda Function
Once your function is created, you can test it using the AWS CLI. You can use the following command to invoke your function: For example, you can create a role with the "AWSLambdaBasicExecutionRole" policy attached to it:
aws lambda invoke --function-name my-lambda-function --payload '{ "name": "Rahul" }' response.json
Update the Lambda function
If you need to make changes to your Lambda function, you can use the AWS CLI to update it. You'll need to provide the function name, the path to the updated function package, and any other parameters that need to be updated:
aws lambda update-function-code --function-name my-lambda-function --zip-file fileb://function-package.zip
Delete the Lambda function
When you're done with your Lambda function, you can use the AWS CLI to delete it:
aws lambda delete-function --function-name my-lambda-function
List all Lambda functions
This command lists all the Lambda functions in your AWS account region ap-south-1.
aws lambda list-functions --region ap-south-1
Single Lambda Function
This command retrieves information about the specified Lambda function, including its configuration and code.
aws lambda get-function --function-name my-lambda-function --region ap-south-1
Publish a new version of Lambda
This command publishes a new version of the specified Lambda function with the specified description.
aws lambda publish-version --function-name my-lambda-function --description "New version"
List all versions of a Lambda function
This command lists all the versions of the specified Lambda function
aws lambda list-versions-by-function --function-name my-lambda-function
Create an alias for a Lambda function
This command creates a new alias for the specified Lambda function with the specified version number.
aws lambda create-alias --function-name my-lambda-function --name my-alias --function-version 1
List all Lambda function names
This command lists the names of all Lambda functions in your account.
aws lambda list-functions --query 'Functions[*].FunctionName'
List all versions of a Lambda function
This command lists all the versions of the specified Lambda function.
aws lambda list-versions-by-function --function-name my-lambda-function --query 'Versions[*].Version'
Get the ARN of a Lambda function
This command retrieves the ARN of the specified Lambda function.
aws lambda get-function --function-name my-lambda-function --query 'Configuration.FunctionArn'
Lambda functions with a specific prefix
This command lists all Lambda functions in your account that start with the prefix "my-lambda-function"
aws lambda list-functions --query "Functions[?starts_with(FunctionName, 'my-lambda-function')].FunctionName"
Lambda functions with a specific prefix but not containing another string
This command lists all Lambda functions in your account that start with the prefix "my-lambda-function" but does not contain the string "test"
aws lambda list-functions --query "Functions[?starts_with(FunctionName, 'my-lambda-function') && !contains(FunctionName, 'test')].FunctionName"
Get the environment variables of a specific Lambda function
This command retrieves the environment variables of the specified Lambda function.
aws lambda get-function-configuration --function-name my-lambda-function --query 'Environment.Variables'
List all the layers of a specific Lambda function
This command lists the names of all the layers of the specified Lambda function.
aws lambda list-layers --query 'Layers[].LayerName'
Event source mappings of a specific Lambda function
This command lists the ARNs of all the event source mappings of the specified Lambda function.
aws lambda list-event-source-mappings --function-name my-lambda-function --query 'EventSourceMappings[].EventSourceArn'
Conclusion
In conclusion, AWS CLI is a powerful tool that allows you to manage your Lambda functions and their configurations from the command line. With AWS CLI, you can easily create, update, delete, and test Lambda functions, as well as retrieve information about their configurations, versions, aliases, and event source mappings.
In this blog, we discussed several AWS CLI commands with examples to help you manage your Lambda functions. We covered how to create a new Lambda function, update an existing function, invoke a function, and test a function locally using the aws lambda
command-line tool. Additionally, we explored how to use JMESPath queries to extract specific data from Lambda functions, such as their configuration, environment variables, aliases, and versions.
By leveraging the power of AWS CLI and its rich feature set, you can automate the deployment and management of your Lambda functions and streamline your serverless workflows. Whether you are a developer, DevOps engineer, or system administrator, AWS CLI is a must-have tool in your arsenal for managing AWS Lambda functions efficiently and effectively.