backin

backin

I need help sending a put event on AWS Eventbridge

I’m fighting with this code

  @moduledoc false
  require Logger

  def send_event_to_eventbridge() do
    event = %{
      entries: [
        %{
          source: "car-event",
          detailType: "car-created",
          detail: (%{
            brand: "Renault",
            name: "A4",
            country: "US"
          }),
          eventBusName: "my-event-bus"
        }
      ]
    }

    event_json = Poison.encode(event)

    Logger.info("Sending: #{inspect(event_json)}")
    
    case ExAws.EventBridge.put_events(event_json) |> ExAws.request() do
      {:ok, response} ->
        Logger.info("Successfully: #{inspect(response)}")
      {:error, error} ->
        Logger.error("Error: #{inspect(error)}")
    end
  end
end

and I don’t know how to fix this error

(Protocol.UndefinedError) protocol Enumerable not implemented for {:ok, “{"entries":[{"eventBusName":"my-event-bus","detail":{"country":"US","brand":"Renault","name":"A4"},"detailType":"car-created","source":"car-event"}]}”} of type Tuple. This protocol is implemented for the following type(s): DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, List, Map, MapSet, Phoenix.LiveView.LiveStream, Postgrex.Stream, Range, Stream
(elixir 1.14.0) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir 1.14.0) lib/enum.ex:166: Enumerable.reduce/3
(elixir 1.14.0) lib/enum.ex:4307: Enum.map/2
(ex_aws_eventbridge 0.1.1) lib/ex_aws/event_bridge.ex:88: ExAws.EventBridge.put_events/2
(my_app 0.1.0) lib/my_app/eventbridge.ex:27: MyApp.Eventbridge.send_event_to_eventbridge/0

Marked As Solved

backin

backin

I was able to get it now, I’ll leave it here for other users.

defmodule MyApp.EventBridge do
  @moduledoc false
  require Logger
  alias ExAws.EventBridge

  def send_event_to_eventbridge(source, detail_type, detail, event_bus_name) do
    event = %{
      "Source": source,
      "DetailType": detail_type,
      "Detail":  Poison.encode!(detail),
      "EventBusName": event_bus_name
    }

    Logger.info("Sending: #{inspect(event)}" )

    case EventBridge.put_events([event]) |> ExAws.request() do
      {:ok, response} ->
        Logger.info("Event ID: #{inspect(response)}")

      {:error, error} ->
        Logger.error("Error #{inspect(error)}")
    end
  end
end

I insert the data through the console

MyApp.EventBridge.send_event_to_eventbridge(
   "car-event",
   "car-created",
   %{brand: "Renault", name: "A4", country: "US"},
   "my-event-bus"
  )

[info] Sending: %{Source: "car-event", DetailType: "car-created", Detail: "{\"country\":\"US\",\"brand\":\"Renault\",\"name\":\"A4\"}", EventBusName: "my-event-bus"}

[info] Event ID: %{"Entries" => [%{"EventId" => "e4accabf-b89d-0dd2-0fff-92e6de215a95"}], "FailedEntryCount" => 0}

Also Liked

msmithstubbs

msmithstubbs

This error message:

(Protocol.UndefinedError) protocol Enumerable not implemented for {:ok, “{"entries":[{"eventBusName":"my-event-bus","detail":{"country":"US","brand":"Renault","name":"A4"},"detailType":"car-created","source":"car-event"}]}”} of type Tuple. 

is telling you “I’m calling an Enumerable function on something, but it doesn’t support it because it is a tuple”

Where is the tuple coming from? Here:

  event_json = Poison.encode(event)`

Poison.encode returns a tuple. If you just want the encoded JSON, you can call Poison.encode!

iex> Poison.encode([1, 2, 3])
{:ok, "[1,2,3]"}

iex> Poison.encode!([1, 2, 3])
"[1,2,3]"

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement