Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(aws-ecr): ECR EventBridge rule has incorrect event pattern #29225

Closed
Labels
@aws-cdk/aws-ecr Related to Amazon Elastic Container Registry bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@Brads3290
Copy link

Describe the bug

Using the CDK, creating an EventBridge rule on an ECR repository creates a rule with the following event pattern:

{
  "resources": ["arn:aws:ecr:ap-southeast-2:<account-id>:repository/my-repository-name"],
  "source": ["aws.ecr"]
}

However when ECR sends the event, "resources" is empty, so the rule never matches (example from https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr-eventbridge.html#ecr-eventbridge-bus):

{
    "version": "0",
    "id": "13cde686-328b-6117-af20-0e5566167482",
    "detail-type": "ECR Image Action",
    "source": "aws.ecr",
    "account": "123456789012",
    "time": "2019-11-16T01:54:34Z",
    "region": "us-west-2",
    "resources": [], // <--- empty, rule fails
    "detail": {
        "result": "SUCCESS",
        "repository-name": "my-repository-name",
        "image-digest": "sha256:7f5b2640fe6fb4f46592dfd3410c4a79dac4f89e4782432e0378abcd1234",
        "action-type": "PUSH",
        "image-tag": "latest"
    }
}

Expected Behavior

Use the detail.repository-name field as a filter instead of resources:

{
  "detail": {
    "repository-name": ["my-repository-name"]
  },
  "source": ["aws.ecr"]
}

Current Behavior

Uses resources as a filter, which is empty in the real event

Reproduction Steps

var ecrRepo = new EcrRepository(this, "CiTestEcrRepository", new EcrRepositoryProps());
ecrRepo.OnEvent("EcrRepoEventRule", new OnEventOptions() {
    Target = new LambdaFunction(myLambdaHandler),
});

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.127.0 (build 6c90efc)

Framework Version

.NET 8

Node.js Version

v18.16.1

OS

MacOS

Language

.NET

Language Version

.NET 8

Other information

No response

@Brads3290 Brads3290 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 22, 2024
@github-actions github-actions bot added the @aws-cdk/aws-ecr Related to Amazon Elastic Container Registry label Feb 22, 2024
@Brads3290
Copy link
Author

Also, CDK adds the resources filter even if you specify your own event pattern, including if you set Resources to null or an empty array:

ecrRepo.OnEvent("EcrRepoEventRule", new OnEventOptions() {
    Target = new LambdaFunction(ecrHandler),
    EventPattern = new EventPattern() {
        Source = new[] { "aws.ecr" },
        Resources = null, //Setting to `null` or `new string[0]` doesn't help
        Detail = new Dictionary<string, object>() {
            ["repository-name"] = new[] { ecrRepo.RepositoryName },
        },
    },
});

Result:

{
  "resources": ["arn:aws:ecr:ap-southeast-2:<account-id>:repository/repository-name"],
  "detail": {
    "repository-name": ["repository-name"]
  },
  "source": ["aws.ecr"]
}

@Brads3290
Copy link
Author

Workaround is to create the rule directly via the EventBridge CDK Rule construct:

new Rule(this, "EcrRepoEventRule", new RuleProps() {
    Targets = new IRuleTarget[] {
        new LambdaFunction(ecrHandler),
    },
    EventPattern = new EventPattern() {
        Source = new[] { "aws.ecr" },
        Detail = new Dictionary<string, object>() {
            ["repository-name"] = new[] { ecrRepo.RepositoryName },
        },
    },
});

@pahud
Copy link
Contributor

pahud commented Feb 23, 2024

Yes I get this when I synth:

  Type: AWS::Events::Rule
    Properties:
      EventPattern:
        source:
          - aws.ecr
        resources:
          - Fn::GetAtt:
              - CiTestEcrRepository95B2A864
              - Arn
      State: ENABLED
      Targets:
        - Arn:
            Fn::GetAtt:
              - Func217E03A4
              - Arn
          Id: Target0

And looks like the resources should be empty according to the doc. And we probably should filter the repository-name in the detail.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Feb 23, 2024
@msambol
Copy link
Contributor

msambol commented Feb 23, 2024

I'll take this.

@msambol
Copy link
Contributor

msambol commented Feb 24, 2024

@pahud I started a PR for this but please see my comment in the description. The Lambda trigger does not created on the Lambda side.

@mergify mergify bot closed this as completed in #29243 May 3, 2024
mergify bot pushed a commit that referenced this issue May 3, 2024
Closes #29225. 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

github-actions bot commented May 3, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment