[英]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.