How to Create a Lambda Function Using AWS CLI

Hemanta Sundaray

Published December 3, 2023


Run the following command to create a Lambda function assuming it has been packaged as a Docker image and pushed to Amazon Elastic Container Registry (ECR):

aws lambda create-function \
  --function-name <FUNCTION_NAME> \
  --package-type Image \
  --code ImageUri=<ECR_REPOSITORY_URI>:latest \
  --role <EXECUTION_ROLE_ARN> \
  --memory-size 1024 \
  --timeout 900

In this command:

Finding the ECR Repository URI

If you don't remember the repository name, you can run the following command to list all the ECR repositories in your current AWS account and region:

aws ecr describe-repositories

The output includes information such as the repository name, URI, and details about the repository configuration. From this list, you can easily identify the relevant repository and its corresponding URI.

If you already know the name of your ECR repository, run the following command:

aws ecr describe-repositories --repository-names <REPOSITORY_NAME>

Replace <REPOSITORY_NAME> with the name of your repository. This command will return information, including the URI, for only the specified repository.

Finding the ARN of the IAM Execution Role

If you don't remember the name of the execution role you've set up for your Lambda function, you can run the following command to list all the roles in your AWS account:

aws iam list-roles

This command will display a list of all IAM roles, including their names and ARNs. You can then scan through this list to find the execution role you previously created for your Lambda function.

If you remember the name of the execution role you intend to use for your Lambda function, then run the following command to retrieve its ARN:

aws iam get-role --role-name <ROLE_NAME>

Replace <ROLE_NAME> with the actual name of your IAM role. This command will provide detailed information about the role, including its Amazon Resource Name (ARN).