Home AWS CLI for EventBridge - send event to EventBridge
Post
Cancel

AWS CLI for EventBridge - send event to EventBridge

Send an event to the default EventBus

I’m assuming that permissions are properly setup.

The hardest part was getting the correct Json structure.

  • The Detail object contains a mixture of Json (the curly brackets are Json) and escaped strings (the keys and values).
  • I escaped all the objects, such as Source, but that led to an an invalid Json error.

So save the contents below in a Json file (e.g., myEventForDefaultEventBus.json)

1
2
3
4
5
6
7
8
[
  {
    "DetailType": "My Awesome Event Name",
    "Detail": "{ \"ec2InstanceId\": \"testInstanceId2\", \"exampleJsonObj\": { \"computerName\": \"fromCLI\" } }",
    "Resources": [ ],
    "Source": "my-service-as-source"
  }
]

Then go into the directory where this file is saved and run the aws cli with

1
aws events put-events --entries file://myEventForDefaultEventBus.json

The output looks like:

1
2
3
4
5
6
7
8
{
  "FailedEntryCont": 0,
  "Entries": [
    {
      "EventId": "<some-generated-unique-identifier>"
    }
  ]
}

Send an event to custom EventBus

The event above went to the default event bus because no element EventBusName is in the Json structure.

To send an event to a custom event bus, I needed to create the event bus, setup the right permissions, and add EventBusName element to the event’s Json structure (according to PutEvents syntax).

1
2
3
4
5
6
7
8
9
[
  {
    "DetailType": "My Awesome Event Name",
    "Detail": "{ \"ec2InstanceId\": \"testInstanceId2\", \"exampleJsonObj\": { \"computerName\": \"fromCLI\" } }",
    "EventBusName": "my-custom-event-bus"
    "Resources": [ ],
    "Source": "my-service-as-source"
  }
]

Then go into the directory where this file is saved and run the aws cli with

1
aws events put-events --entries file://myEventForCustomEventBus.json

The output also looks like:

1
2
3
4
5
6
7
8
{
  "FailedEntryCont": 0,
  "Entries": [
    {
      "EventId": "<some-generated-unique-identifier>"
    }
  ]
}

Create event bus using CloudFormation

The Cloudformation template below does the following:

  • Creates a custom event bus and routes all events to CloudWatch.
  • Creates policies that allow this accountId to send events to the custom event bus.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
...
Resources:
  MyCustomEventBus:
    Type: 'AWS::Events::EventBus'
    Properties:
      Name: !Sub "${AWS::StackName}-event-bus"
  
  MyCustomEventBusLogGroup:
    Type: 'AWS::Logs::LogGroup'
    UpdateReplacePolicy: Delete
    DeletionPolicy: Delete
    Properties:
      LogGroupName: !Sub '/aws/events/${AWS::StackName}-events'
      RetentionInDays: 7

  MyCustomEventBusEventRule:
    Type: 'AWS::Events::Rule'
    Properties:
      Description: 'Send all events to CloudWatch'
      State: 'ENABLED'
      EventBusName: !Ref 'MyCustomEventBus'
      EventPattern:
        source:
          - my-service-as-source
      Targets:
        - Arn: !GetAtt
            - MyCustomEventBusLogGroup
            - Arn
          Id: MyCustomEventBusEventRuleTrigger

References

This post is licensed under CC BY 4.0 by the author.

Powershell - Grant user access to Docker Pipe without Admin rights

Packer - setup with WinRM