From 6d4995c2498d15e78baa78f4a80f85a512ea4061 Mon Sep 17 00:00:00 2001 From: Kyle A Anderson Date: Thu, 25 Jan 2024 21:59:41 -0800 Subject: [PATCH 1/4] Use ExAws.InstanceMeta --- lib/libcluster_ec2.ex | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/libcluster_ec2.ex b/lib/libcluster_ec2.ex index 41d8661..21c6f60 100644 --- a/lib/libcluster_ec2.ex +++ b/lib/libcluster_ec2.ex @@ -1,29 +1,24 @@ defmodule ClusterEC2 do - use Tesla @moduledoc File.read!("#{__DIR__}/../README.md") - plug(Tesla.Middleware.BaseUrl, "http://169.254.169.254/latest/meta-data") - @doc """ Queries the local EC2 instance metadata API to determine the instance ID of the current instance. """ @spec local_instance_id() :: binary() - def local_instance_id do - case get("/instance-id/") do - {:ok, %{status: 200, body: body}} -> body - _ -> "" - end - end + def local_instance_id, do: get_metadata("/instance-id/") @doc """ Queries the local EC2 instance metadata API to determine the aws resource region of the current instance. """ @spec instance_region() :: binary() def instance_region do - case get("/placement/availability-zone/") do - {:ok, %{status: 200, body: body}} -> String.slice(body, 0..-2//1) - _ -> "" - end + get_metadata("/placement/availability-zone/") + |> String.slice(0..-2//1) + end + + def get_metadata(path) do + ExAws.Config.new(:ec2) + |> ExAws.InstanceMeta.request("http://169.254.169.254/latest/meta-data#{path}") end end From 3bade328a43954b72e2e53277be7b246be45d4eb Mon Sep 17 00:00:00 2001 From: Kyle A Anderson Date: Thu, 25 Jan 2024 22:19:52 -0800 Subject: [PATCH 2/4] =?UTF-8?q?Remove=20tests=20that=20aren=E2=80=99t=20ac?= =?UTF-8?q?tually=20testing=20anything?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Tags test just confirm the GenServer default state. The Error test was identical to the Non-Error test. With the leveraging of ExAws.InstanceMeta, the valid tests cases in libcluster_ec2_test are mostly passthrough functions. --- test/libcluster_ec2_test.exs | 24 ------------------- test/strategy/tags_error_test.exs | 40 ------------------------------- test/strategy/tags_test.exs | 40 ------------------------------- 3 files changed, 104 deletions(-) delete mode 100644 test/libcluster_ec2_test.exs delete mode 100644 test/strategy/tags_error_test.exs delete mode 100644 test/strategy/tags_test.exs diff --git a/test/libcluster_ec2_test.exs b/test/libcluster_ec2_test.exs deleted file mode 100644 index c156a1a..0000000 --- a/test/libcluster_ec2_test.exs +++ /dev/null @@ -1,24 +0,0 @@ -defmodule ClusterEC2Test do - use ExUnit.Case - doctest ClusterEC2 - - setup do - Tesla.Mock.mock(fn - %{method: :get, url: "http://169.254.169.254/latest/meta-data/instance-id/"} -> - %Tesla.Env{status: 200, body: "i-0fdde7ca9faef9751"} - - %{method: :get, url: "http://169.254.169.254/latest/meta-data/placement/availability-zone/"} -> - %Tesla.Env{status: 200, body: "eu-central-1b"} - end) - - :ok - end - - test "return local_instance_id" do - assert "i-0fdde7ca9faef9751" == ClusterEC2.local_instance_id() - end - - test "return instance_region" do - assert "eu-central-1" == ClusterEC2.instance_region() - end -end diff --git a/test/strategy/tags_error_test.exs b/test/strategy/tags_error_test.exs deleted file mode 100644 index 835f85b..0000000 --- a/test/strategy/tags_error_test.exs +++ /dev/null @@ -1,40 +0,0 @@ -defmodule Strategy.TagsErrorTest do - use ExUnit.Case, async: false - doctest ClusterEC2 - - setup do - Tesla.Mock.mock_global(fn - %{method: :get, url: "http://169.254.169.254/latest/meta-data/instance-id/"} -> - %Tesla.Env{status: 200, body: ""} - - %{method: :get, url: "http://169.254.169.254/latest/meta-data/placement/availability-zone/"} -> - %Tesla.Env{status: 200, body: ""} - end) - - ops = [ - topology: ClusterEC2.Strategy.Tags, - connect: {:net_kernel, :connect, []}, - disconnect: {:net_kernel, :disconnect, []}, - list_nodes: {:erlang, :nodes, [:connected]}, - config: [ - ec2_tagname: "elasticbeanstalk:environment-name" - ] - ] - - {:ok, server_pid} = ClusterEC2.Strategy.Tags.start_link(ops) - {:ok, server: server_pid} - end - - test "test info call :load", %{server: pid} do - assert :load == send(pid, :load) - - assert %Cluster.Strategy.State{ - config: [ec2_tagname: "elasticbeanstalk:environment-name"], - connect: {:net_kernel, :connect, []}, - disconnect: {:net_kernel, :disconnect, []}, - list_nodes: {:erlang, :nodes, [:connected]}, - meta: MapSet.new([]), - topology: ClusterEC2.Strategy.Tags - } == :sys.get_state(pid) - end -end diff --git a/test/strategy/tags_test.exs b/test/strategy/tags_test.exs deleted file mode 100644 index 564ec9a..0000000 --- a/test/strategy/tags_test.exs +++ /dev/null @@ -1,40 +0,0 @@ -defmodule Strategy.TagsTest do - use ExUnit.Case, async: false - doctest ClusterEC2 - - setup do - Tesla.Mock.mock_global(fn - %{method: :get, url: "http://169.254.169.254/latest/meta-data/instance-id/"} -> - %Tesla.Env{status: 200, body: "i-0fdde7ca9faef9751"} - - %{method: :get, url: "http://169.254.169.254/latest/meta-data/placement/availability-zone/"} -> - %Tesla.Env{status: 200, body: "eu-central-1b"} - end) - - ops = [ - topology: ClusterEC2.Strategy.Tags, - connect: {:net_kernel, :connect, []}, - disconnect: {:net_kernel, :disconnect, []}, - list_nodes: {:erlang, :nodes, [:connected]}, - config: [ - ec2_tagname: "elasticbeanstalk:environment-name" - ] - ] - - {:ok, server_pid} = ClusterEC2.Strategy.Tags.start_link(ops) - {:ok, server: server_pid} - end - - test "test info call :load", %{server: pid} do - assert :load == send(pid, :load) - - assert %Cluster.Strategy.State{ - config: [ec2_tagname: "elasticbeanstalk:environment-name"], - connect: {:net_kernel, :connect, []}, - disconnect: {:net_kernel, :disconnect, []}, - list_nodes: {:erlang, :nodes, [:connected]}, - meta: MapSet.new([]), - topology: ClusterEC2.Strategy.Tags - } == :sys.get_state(pid) - end -end From 81bc3cd8f9cc4386d5f55a0fdaa30ac6d769715a Mon Sep 17 00:00:00 2001 From: Kyle A Anderson Date: Thu, 25 Jan 2024 22:20:58 -0800 Subject: [PATCH 3/4] Remove Tesla --- config/config.exs | 4 +--- lib/strategy/tags.ex | 1 - mix.exs | 5 ++--- mix.lock | 18 +++++++++--------- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/config/config.exs b/config/config.exs index eb27d71..908d7eb 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,6 +1,6 @@ # This file is responsible for configuring your application # and its dependencies with the aid of the Mix.Config module. -use Mix.Config +import Mix.Config # This configuration is loaded before any dependency and is restricted # to this project. If another project depends on this project, this @@ -33,6 +33,4 @@ if Mix.env() == :test do config :ex_aws, access_key_id: "xxx", secret_access_key: "xxx" - - config :tesla, adapter: Tesla.Mock end diff --git a/lib/strategy/tags.ex b/lib/strategy/tags.ex index 8065186..7c02d7f 100644 --- a/lib/strategy/tags.ex +++ b/lib/strategy/tags.ex @@ -42,7 +42,6 @@ defmodule ClusterEC2.Strategy.Tags do @default_polling_interval 5_000 def start_link(opts) do - Application.ensure_all_started(:tesla) Application.ensure_all_started(:ex_aws) GenServer.start_link(__MODULE__, opts) end diff --git a/mix.exs b/mix.exs index b775993..7d0ac17 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule ClusterEC2.Mixfile do def project do [ app: :libcluster_ec2, - version: "0.7.0", + version: "0.8.0", elixir: "~> 1.4", name: "libcluster_ec2", source_url: "https://github.com/kyleaa/libcluster_ec2", @@ -28,11 +28,10 @@ defmodule ClusterEC2.Mixfile do defp deps do [ {:libcluster, "~> 2.0 or ~> 3.0"}, - {:ex_aws, "~> 2.0"}, + {:ex_aws, "~> 2.3.2"}, {:ex_aws_ec2, "~> 2.0"}, {:sweet_xml, "~> 0.6"}, {:hackney, "~> 1.8"}, - {:tesla, "~> 1.0"}, {:ex_doc, ">= 0.0.0", only: :dev} ] end diff --git a/mix.lock b/mix.lock index e77911b..d14860a 100644 --- a/mix.lock +++ b/mix.lock @@ -1,25 +1,25 @@ %{ - "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, + "certifi": {:hex, :certifi, "2.12.0", "2d1cca2ec95f59643862af91f001478c9863c2ac9cb6e2f89780bfd8de987329", [:rebar3], [], "hexpm", "ee68d85df22e554040cdb4be100f33873ac6051387baf6a8f6ce82272340ff1c"}, "earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm", "000aaeff08919e95e7aea13e4af7b2b9734577b3e6a7c50ee31ee88cab6ec4fb"}, "earmark_parser": {:hex, :earmark_parser, "1.4.20", "89970db71b11b6b89759ce16807e857df154f8df3e807b2920a8c39834a9e5cf", [:mix], [], "hexpm", "1eb0d2dabeeeff200e0d17dc3048a6045aab271f73ebb82e416464832eb57bdd"}, - "ex_aws": {:hex, :ex_aws, "2.2.10", "064139724335b00b6665af7277189afc9ed507791b1ccf2698dadc7c8ad892e8", [:mix], [{:configparser_ex, "~> 4.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8 or ~> 3.0", [hex: :jsx, repo: "hexpm", optional: true]}, {:mime, "~> 1.2 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "98acb63f74b2f0822be219c5c2f0e8d243c2390f5325ad0557b014d3360da47e"}, + "ex_aws": {:hex, :ex_aws, "2.3.4", "f22c9a9a8f5afdcf44a824df586954240f71d157cecb1ecfd5665d219f782322", [:mix], [{:configparser_ex, "~> 4.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8 or ~> 3.0", [hex: :jsx, repo: "hexpm", optional: true]}, {:mime, "~> 1.2 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "849b77125af5c405d38675de75cdddf5b37a7a6ea3982929a516586e338e0ce8"}, "ex_aws_ec2": {:hex, :ex_aws_ec2, "2.0.1", "1558c9bf4044048fb2b3b1ca3d7a62c65666959432ae29ef1d584258926d8fc0", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}], "hexpm", "adb86c51c2bcf630a739261bfff9293d2bf96946bf1a2d40d4f78e56a349a52b"}, "ex_doc": {:hex, :ex_doc, "0.28.2", "e031c7d1a9fc40959da7bf89e2dc269ddc5de631f9bd0e326cbddf7d8085a9da", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "51ee866993ffbd0e41c084a7677c570d0fc50cb85c6b5e76f8d936d9587fa719"}, - "hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"}, + "hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, - "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, + "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, "libcluster": {:hex, :libcluster, "3.3.1", "e7a4875cd1290cee7a693d6bd46076863e9e433708b01339783de6eff5b7f0aa", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "b575ca63c1cd84e01f3fa0fc45e6eb945c1ee7ae8d441d33def999075e9e5398"}, "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"}, "makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"}, "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, - "mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"}, + "mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, - "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, - "sweet_xml": {:hex, :sweet_xml, "0.7.2", "4729f997286811fabdd8288f8474e0840a76573051062f066c4b597e76f14f9f", [:mix], [], "hexpm", "6894e68a120f454534d99045ea3325f7740ea71260bc315f82e29731d570a6e8"}, - "telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"}, + "parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, + "sweet_xml": {:hex, :sweet_xml, "0.7.4", "a8b7e1ce7ecd775c7e8a65d501bc2cd933bff3a9c41ab763f5105688ef485d08", [:mix], [], "hexpm", "e7c4b0bdbf460c928234951def54fe87edf1a170f6896675443279e2dbeba167"}, + "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, "tesla": {:hex, :tesla, "1.4.4", "bb89aa0c9745190930366f6a2ac612cdf2d0e4d7fff449861baa7875afd797b2", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.3", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "d5503a49f9dec1b287567ea8712d085947e247cb11b06bc54adb05bfde466457"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, } From d5742fcf703844cc3ad9605829edb21ab894b6ae Mon Sep 17 00:00:00 2001 From: Kyle A Anderson Date: Fri, 26 Jan 2024 20:08:28 -0800 Subject: [PATCH 4/4] Docs, Version --- CHANGELOG.md | 3 +++ README.md | 23 +++++++++++++++++++++++ lib/libcluster_ec2.ex | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c30b58..9a2450c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +v0.8.0 +- Enhancement: Support IMDSv2 (Requires ExAws 2.3.2 minimum) https://github.com/kyleaa/libcluster_ec2/pull/33 + v0.7.0 - Enhancement: auto reconnect when disconnected by some reason https://github.com/kyleaa/libcluster_ec2/pull/27 - Dependency Cleanup: Remove Poison dependency https://github.com/kyleaa/libcluster_ec2/pull/23 diff --git a/README.md b/README.md index 1508dfc..9bbc2aa 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,26 @@ def deps do [{:libcluster_ec2, "~> 0.5"}] end ``` + +## AWS IAM Requirements + +Instances must have an instance role attached. There are two permissions required: +* `ec2:DescribeInstances` - Required to determine tag values of the current running instance. Can be restricted by Resource to the current instance running the application +* `ec2:DescribeTags` - Required to identify other instances with the same tags + +``` +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "VisualEditor0", + "Effect": "Allow", + "Action": [ + "ec2:DescribeInstances", + "ec2:DescribeTags" + ], + "Resource": "*" + } + ] +} +``` \ No newline at end of file diff --git a/lib/libcluster_ec2.ex b/lib/libcluster_ec2.ex index 21c6f60..84e7369 100644 --- a/lib/libcluster_ec2.ex +++ b/lib/libcluster_ec2.ex @@ -17,7 +17,7 @@ defmodule ClusterEC2 do |> String.slice(0..-2//1) end - def get_metadata(path) do + defp get_metadata(path) do ExAws.Config.new(:ec2) |> ExAws.InstanceMeta.request("http://169.254.169.254/latest/meta-data#{path}") end