简体   繁体   English

如何使用 AWS CLI 从 Elastic Container Registry 映像创建 lambda function?

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

I have created this lambda function:我已经创建了这个 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:我用这个 Dockerfile 创建了一个 Docker 图像:

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

and I have pushed it to ECR.我已将其推送到 ECR。 Now, I want to create a lambda function that runs it.现在,我想创建一个运行它的 lambda function。

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.调用 CreateFunction 操作时发生错误(InvalidParameterValueException):Runtime 和 Handler 是使用部署包创建的函数的必需参数。

It makes no sense because it's a Docker image based lambda function so that parameters shouldn't be needed.这是没有意义的,因为它是基于 Docker 图像的 lambda function ,因此不需要参数。

This seemed to work for me.这似乎对我有用。 You need to to remove handler, runtime and be sure to specify the package type as Image, and您需要删除处理程序、运行时并确保将 package 类型指定为 Image,并且

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) (AWS Cli 版本 2.1.7)

your create function looks like the below:您创建的 function 如下所示:

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.我想知道 lambda 是否需要设置package-type才能获得这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS CLI 创建 AWS Lambda 函数? - How can I create an AWS Lambda function using the AWS CLI? 如何使用AWS CLI创建python AWS Lambda函数? - How can I create an python AWS Lambda function using the AWS CLI? 如何使用 aws cli 创建 java aws lambda function? - how can i create an java aws lambda function using the aws cli? 如何通过忽略未使用的资源从 aws cli 创建 aws java lambda function? - how can i create the aws java lambda function from aws cli by ignoring unused resources? 如何使用AWS CLI为lambda函数创建计划事件? - How can I create a Scheduled Event for a lambda function using the AWS CLI? 我可以使用aws cli获取lambda函数触发器信息吗? - Can I get the lambda function trigger information using aws cli? 如何将AWS CodePipeline与Elastic Container Registry集成? - How do I integrate AWS CodePipeline with Elastic Container Registry? How can I call Lambda function from MySQL database in AWS and how can I pass a parameter to a container in ECS from the Lambda function? - How can I call Lambda function from MySQL database in AWS and how can I pass a parameter to a container in ECS from the Lambda function? 如何使用 AWS CLI 删除特定的 Lambda 版本? - How can I delete specific Lambda versions using the AWS CLI? AWS lambda:如何在 lambda 中运行 aws cli 命令 - AWS lambda: how can I run aws cli commands in lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM