6

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.

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.