简体   繁体   中英

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. I have also used it to create lambda function from a container image through AWS console, and it worked successfully.

Now, I want to create the function through AWS CDK.

Inside the __init__ function of the lambda stack class, I have added:

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.

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

The first attribute of Repository object should be self to refer to the same scope of the stack.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM