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

Add support for EC2 #43

Closed
philipgiuliani opened this issue Oct 19, 2020 · 4 comments
Closed

Add support for EC2 #43

philipgiuliani opened this issue Oct 19, 2020 · 4 comments

Comments

@philipgiuliani
Copy link

philipgiuliani commented Oct 19, 2020

The library currently does not support the EC2 API.

Related: aws-beam/aws-codegen#24

@philipgiuliani
Copy link
Author

In the meanwhile I am using this to get my required API call.

defmodule AWS.EC2 do
  def describe_network_interfaces(client, input, options \\ []) do
    request(client, "DescribeNetworkInterfaces", input, options)
  end

  @spec request(AWS.Client.t(), binary(), map(), list()) ::
          {:ok, Poison.Parser.t() | nil, Poison.Response.t()}
          | {:error, Poison.Parser.t()}
          | {:error, HTTPoison.Error.t()}
  defp request(client, action, input, options) do
    client = %{client | service: "ec2"}
    host = build_host("ec2", client)
    url = build_url(host, client)

    headers = [
      {"Host", host},
      {"Content-Type", "application/x-www-form-urlencoded"}
    ]

    input = Map.merge(input, %{"Action" => action, "Version" => "2016-11-15"})
    payload = AWS.Util.encode_query(input)
    headers = AWS.Request.sign_v4(client, "POST", url, headers, payload)

    case HTTPoison.post(url, payload, headers, options) do
      {:ok, %HTTPoison.Response{status_code: 200, body: ""} = response} ->
        {:ok, nil, response}

      {:ok, %HTTPoison.Response{status_code: 200, body: body} = response} ->
        {:ok, AWS.Util.decode_xml(body), response}

      {:ok, %HTTPoison.Response{body: body}} ->
        error = AWS.Util.decode_xml(body)
        {:error, error}

      {:error, %HTTPoison.Error{reason: reason}} ->
        {:error, %HTTPoison.Error{reason: reason}}
    end
  end

  defp build_host(_endpoint_prefix, %{region: "local"}) do
    "localhost"
  end

  defp build_host(endpoint_prefix, %{region: region, endpoint: endpoint}) do
    "#{endpoint_prefix}.#{region}.#{endpoint}"
  end

  defp build_url(host, %{:proto => proto, :port => port}) do
    "#{proto}://#{host}:#{port}/"
  end
end

@dynnamitt
Copy link

Just curious (and I know it's more layers) but did you try https://hexdocs.pm/aws/AWS.AutoScaling.html
I also wish the ec2 api would be support tho

@ruslandoga
Copy link

ruslandoga commented Apr 6, 2022

defmodule AWS.EC2 do
  alias AWS.Request

  def metadata do
    %AWS.ServiceMetadata{
      api_version: "2016-11-15",
      content_type: "text/xml",
      endpoint_prefix: "ec2",
      global?: false,
      protocol: "query",
      signing_name: "ec2"
    }
  end

  def describe_instances(client, input, options \\ []) do
    Request.request_post(client, metadata(), "DescribeInstances", input, options)
  end
end

seems to work for me:

> iex
Erlang/OTP 25 [RELEASE CANDIDATE 2] [erts-13.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Interactive Elixir (1.14.0-dev) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> Mix.install [:aws, :jason, :hackney]
iex(2)> defmodule AWS.EC2 do
...(2)>   alias AWS.Request
...(2)>
...(2)>   def metadata do
...(2)>     %AWS.ServiceMetadata{
...(2)>       api_version: "2016-11-15",
...(2)>       content_type: "text/xml",
...(2)>       endpoint_prefix: "ec2",
...(2)>       global?: false,
...(2)>       protocol: "query",
...(2)>       signing_name: "ec2"
...(2)>     }
...(2)>   end
...(2)>
...(2)>   def describe_instances(client, input, options \\ []) do
...(2)>     Request.request_post(client, metadata(), "DescribeInstances", input, options)
...(2)>   end
...(2)> end

iex(3)> client = AWS.Client.create("eu-north-1")
iex(4)> AWS.EC2.describe_instances(client, _input = %{})
{:ok,
 %{
   "DescribeInstancesResponse" => %{
     "requestId" => "57587f0e-31f9-4d70-8f27-87b738f39f2b",
     "reservationSet" => %{
       "item" => [
         %{
           "groupSet" => :none,
           "instancesSet" => %{
             "item" => %{
               "tagSet" => %{
               # ...

@cheese-head
Copy link
Contributor

I've created two pull requests that would add support for ec2.
aws-beam/aws-codegen#100
#170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants