Search code examples
amazon-web-servicesdockeraws-lambdaaws-api-gatewayamazon-ecr

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


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.


Solution

  • 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)