How to trigger lambda function when image is pushed to ECR?

0

I am trying to invoke a lambda function as soon as a new image is pushed to ECR. For this, I have created a lambda function and successfully tested its attached code snippet.

As a trigger, I am using an EventBridge rule with the event pattern:

{
  "source": ["aws.ecr"],
  "detail-type": ["ECR Image Action"],
  "detail": {
    "action-type": ["PUSH"],
    "result": ["SUCCESS"],
    "repository-name": ["myrepo1", "myrepo2"],
    "image-tag": ["latest"]
  }
}

I can see the cross reference of the rule to the lambda and vice versa. But when a new image is pushed to ECR, the rule is not triggered. According to this post this should be enough, as ECR events should be published automatically.

Best regards,

Sebastian

2 Answers
0

Hi there,

It sounds like you have correctly set up the EventBridge rule and Lambda function to trigger when a new image is pushed to your Amazon Elastic Container Registry (ECR) repositories. However, it appears that the rule is not being triggered when a new image is pushed to ECR.

One possible reason for this is that the event pattern in your EventBridge rule may not be correctly configured to match the specific events that are generated by ECR when a new image is pushed. The event pattern in your rule specifies that the source of the event must be "aws.ecr", the detail type must be "ECR Image Action", and the action type and result must be "PUSH" and "SUCCESS", respectively. However, you may need to specify the registry and repository where the image was pushed to the event pattern in order for the rule to match the events that are generated by ECR when a new image is pushed.

Another possible reason is that the ECR repository where the image is pushed may not be configured to publish events to EventBridge. By default, ECR repositories do not publish events to EventBridge, so you will need to enable this feature in the repository settings in order for the events to be sent to EventBridge and trigger your rule.

I would recommend checking the event pattern in your EventBridge rule and ensuring that it is properly configured to match the events that are generated by ECR when a new image is pushed. You should also check the settings for the ECR repository and ensure that the repository is configured to publish events to EventBridge. If these are not the issue, you may need to check for other potential causes or contact Amazon customer support for assistance.

AWS
answered a year ago
  • Thanks for the lightning-fast reply. Upon investigation of my ECR repo, I was unable to find the option to enable event creation. I have directed this at my admin for further investigation. Regarding the second suggestion, could you point me to an example where the registry is specified? I created this rule in the browser GUI, so I am completely oblivious what to edit.

  • @ajmarcin I have tried out your suggestions, e.g. I added the total repository path including the registry to the rule like so: "repository-name":["<userID>.dkr.ecr.eu-central-1.amazonaws.com/myrepo1"] , but that didn't do the trick, unfortunately. And neither my admin nor myself were able to find the configuration option of the repository where the event publishing is managed. Editing the repositry only provides me with options regarding visibility, image scanning, and encryption. Could you provide further information, please?

0

You need to filter this event

    "eventSource": "ecr.amazonaws.com",
    "eventName": "PutImage",
answered a year ago
  • Could you elaborate on where to put this snippet? I tried adding it under "detail", but without success. Also, isn't this quite redundant with the existing fields?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions