To Devs,
I am looking to deploy a lambda with an image from 1 of 2 possible ECR's. First check ECR-1 and if it has an image hook up the lambda to that. If ECR-1 is empty then use the image from ECR-2.
What is the best way to do this?
Thanks,
Marc

- 548
- 8
- 22
-
3Can you show your lambda code? What errors do you get? Why it does not work or what you want to change in it? – Marcin Oct 15 '21 at 02:17
-
The lambda code is an image and is referenced to a specific ECR. My problem is the ECR is being created in the Terraform script that the lambda is being created. I can use a dummy ECR with an image to create the lambda. A lambda that is based on an image can not be created without referencing an image. Thanks – user1154422 Oct 15 '21 at 09:25
1 Answers
I don't know of any way that you can determine this from within your Terraform configuration. If you try to use one of the ECR data sources, I would expect it to throw an error if the repository doesn't already exist. Nor is there any way to push an image when creating the repository.
So one solution is to run your Terraform with a variable that identifies the repository, and override this variable on first run.
Or you could use a local-exec provisioner and a shell script to upload a dummy revision if one doesn't already exist. Or use a third-party Docker provider.
A better solution is to split the build and deployment configurations. If you look at the process of deploying Lambdas, it looks like this:
- Verify that the ECR repository exists, and create it if it doesn't.
- Build the Docker image and upload to the repository.
- Run the deployment.
Since you don't talk about integrating step 2 into your Terraform config, you've already got a split process. So formalize that. You can either rerun step 1 with every build, and rely on Terraform not overwriting existing infrastructure, or your build script can perform step 1 using shell commands.

- 3,928
- 5
- 9