简体   繁体   English

AWS CDK:如何从 ECR 存储库上的现有 Docker 映像创建 Lambda function?

[英]AWS CDK: How to create a Lambda function from an existing Docker image on ECR repo?

I have a Docker image pushed already on ECR.我已经在 ECR 上推送了一个 Docker 图像。 I have also used it to create lambda function from a container image through AWS console, and it worked successfully.我还使用它通过 AWS 控制台从容器映像创建 lambda function,并且成功运行。

Now, I want to create the function through AWS CDK.现在,我想通过 AWS CDK 创建 function。

Inside the __init__ function of the lambda stack class, I have added:在 lambda 堆栈 class 的__init__ function 内部,我添加了:

repo = aws_ecr.Repository.from_repository_name(scope, "Repository", repository_name="my-repo-name")
lambdaFn = aws_lambda.DockerImageFunction(
    self, "Test Function",
    code=aws_lambda.DockerImageCode.from_ecr(repo),
    timeout=core.Duration.seconds(600),
    memory_size=8192,
    environment=dict(PATH="/opt"),
    role = role
)

I have an issue with defining repo variable from an existing repo on ECR.我在从 ECR 上的现有 repo 定义repo变量时遇到问题。

Solved!解决了!

The code shows an error: jsii.errors.JSIIError: Import at 'Repository' should be created in the scope of a Stack, but no Stack found代码显示错误: jsii.errors.JSIIError: Import at 'Repository' should be created in the scope of a Stack, but no Stack found

The first attribute of Repository object should be self to refer to the same scope of the stack. Repository object 的第一个属性应该是self以引用堆栈的相同 scope。

Soultion:灵魂:

repo = aws_ecr.Repository.from_repository_name(self, "Repository", repository_name="my-repo-name")
lambdaFn = aws_lambda.DockerImageFunction(
    self, "Test Function",
    code=aws_lambda.DockerImageCode.from_ecr(
        repository=repo,
        tag="latest"
    ),
    timeout=core.Duration.seconds(600),
    memory_size=8192,
    environment=dict(PATH="/opt"),
    role = role
)

Optionally, I have explicitly specified also the parameter tag as per Miguel answer.或者,我还根据 Miguel 的回答明确指定了参数tag

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

相关问题 AWS CDK 引用 ECR 上的现有映像 - AWS CDK reference existing image on ECR AWS CDK - 在 Lambda Function 中有条件地创建角色 - AWS CDK - Create role conditionally in Lambda Function 如何从在 AWS Lambda 函数中运行的 Docker 映像获取日志? - How to get logs from Docker image running in AWS Lambda function? 使用 Python Lambda 函数的 AWS ECR 图像标签 - AWS ECR Image Tags using Python Lambda Function 使用Python Docker API从AWS ECR获取Image哈希 - Using the Python Docker API to get an Image hash from an AWS ECR 触发 AWS Lambda function 扫描 ECR 存储库 - Trigger AWS Lambda function to scan ECR Repositores 有没有办法使用 AWS-CDK 将新的 lambda function 连接到现有的 AWS ApiGateway? (Python) - Is there a way to connect a new lambda function an existing AWS ApiGateway using AWS-CDK? (Python) 如何在 AWS CDK 创建的 Python Lambda Function 中安装外部模块? - How to install external modules in a Python Lambda Function created by AWS CDK? 如何在 AWS CDK 中为 Python 覆盖 aws_cdk.core.LegacyStackSynthesizer.add_docker_image_asset - How to override aws_cdk.core.LegacyStackSynthesizer.add_docker_image_asset in AWS CDK for Python Lambda 从 S3 检索数据时挂起的容器映像 (Docker ECR) 构建 - Lambda built from container image (Docker ECR) hanging when retrieving data from S3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM