要使用AWS EventBridge监控ECR,您可以使用以下步骤进行设置:
ECRRepository
的CloudFormation堆栈来创建ECR存储库。您可以使用以下CloudFormation模板作为参考:AWSTemplateFormatVersion: '2010-09-09'
Resources:
ECRRepository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: MyECRRepository
EventRule
的CloudFormation堆栈来定义EventBridge规则。以下是一个示例CloudFormation模板:AWSTemplateFormatVersion: '2010-09-09'
Resources:
EventRule:
Type: AWS::Events::Rule
Properties:
Description: ECR Event Rule
EventPattern:
source:
- aws.ecr
detail-type:
- ECR Image Action
detail:
eventSource:
- ecr.amazonaws.com
eventName:
- PutImage
repository-name:
- MyECRRepository
State: ENABLED
Targets:
- Arn:
Id:
确保将
和
替换为您的目标ARN和ID。
部署CloudFormation堆栈并创建ECR存储库和EventBridge规则。
您可以使用以下Python示例代码来创建EventBridge的规则目标:
import boto3
client = boto3.client('events')
response = client.put_targets(
Rule='',
Targets=[
{
'Id': '',
'Arn': '',
'InputTransformer': {
'InputPathsMap': {
'detail': '$.detail'
},
'InputTemplate': '{"eventDetail": }'
}
}
]
)
确保将
,
和
替换为您的EventBridge规则名称,目标ID和ARN。
现在,当ECR存储库中发生PutImage事件时,EventBridge规则将触发并将事件发送到指定的目标。您可以根据需要自定义目标处理逻辑。