How can I create a lambda function from an Elastic Container Registry image using AWS CLI?

Viewed 1786

I have created this lambda function:

exports.lambdaHandler = async event => {

    const body =
        message: "Hello"
    };

    return {
        statusCode: 200,
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(body)
    };

};

I have created a Docker image with this Dockerfile:

FROM amazon/aws-lambda-nodejs:12
COPY app.js package*.json ./
RUN npm install
CMD [ "app.lambdaHandler" ]

and I have pushed it to ECR. Now, I want to create a lambda function that runs it.

I have tried with this command:

aws lambda create-function --function-name greeting --role arn:aws:iam::xxxxxxxxxxxx:role/my-role --code ImageUri=xxxxxxxxxxxx.dkr.ecr.eu-central-1.amazonaws.com/greeting:latest

and I get this error:

An error occurred (InvalidParameterValueException) when calling the CreateFunction operation: Runtime and Handler are mandatory parameters for functions created with deployment packages.

It makes no sense because it's a Docker image based lambda function so that parameters shouldn't be needed.

2 Answers

This seemed to work for me. You need to to remove handler, runtime and be sure to specify the package type as Image, and

aws lambda create-function  \
--function-name greeting  \
--role  arn:aws:iam::xxxxxxxxxxx:role/my-role \
--code ImageUri=xxxxxxxxxxx.dkr.ecr.eu-west-1.amazonaws.com/greeting:latest \
--package-type Image

(AWS Cli version 2.1.7)

your create function looks like the below:

aws lambda create-function \
  --function-name greeting \
  --role arn:aws:iam::xxxxxxxxxxxx:role/my-role \
  --code ImageUri=xxxxxxxxxxxx.dkr.ecr.eu-central-1.amazonaws.com/greeting:latest

From the error, you need to specify handler and runtime:

aws lambda create-function \
  --function-name greeting \
  --runtime nodejs12.x
  --handler lambdaHandler
  --role arn:aws:iam::xxxxxxxxxxxx:role/my-role \
  --code ImageUri=xxxxxxxxxxxx.dkr.ecr.eu-central-1.amazonaws.com/greeting:latest

this is all pretty new and "feels" like it shouldn't be required bc the container definition defines it...

I wonder if lambda requires package-type to be set to obtains these values.

Related