Reputation: 1480
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.
Upvotes: 6
Views: 7764
Reputation: 1480
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.
Upvotes: 8