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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement