Skip to content

Honor AWS_ENDPOINT_URL_<SERVICE> env vars in generated Erlang clients#129

Open
vulkoingim wants to merge 1 commit into
aws-beam:masterfrom
vulkoingim:respect-aws_endpoint_url
Open

Honor AWS_ENDPOINT_URL_<SERVICE> env vars in generated Erlang clients#129
vulkoingim wants to merge 1 commit into
aws-beam:masterfrom
vulkoingim:respect-aws_endpoint_url

Conversation

@vulkoingim
Copy link
Copy Markdown

Depends on aws_beam_core:apply_endpoint_url_override

As a disclosure I have used a LLM to generate the code, but I have tested and verified that the generated erlang lib compiles and works against:

  • DynamoDB Local with AWS_ENDPOINT_URL_DYNAMODB
  • Real DynamoDB, no endpoint override
  • RustFS with AWS_ENDPOINT_URL_S3
  • Real S3

Generates do_request in the form:

 do_request(Client, Action, Input0, Options) ->
     Client1 = Client#{service => <<"dynamodb">>},
-    Host = build_host(<<"dynamodb">>, Client1),
-    URL = build_url(Host, Client1),
+    DefaultHost = build_host(<<"dynamodb">>, Client1),
+    {URL, Host} = aws_util:apply_endpoint_url_override(
+        build_url(DefaultHost, Client1), DefaultHost, <<"/">>,
+        <<"AWS_ENDPOINT_URL_DYNAMODB">>),
     Headers = [
         {<<"Host">>, Host},
         {<<"Content-Type">>, <<"application/x-amz-json-1.0">>},

Tested after generating the code, and patching the aws_util in aws-erlang.

cd aws-erlang
rebar3 compile
./test.escript
# and targeting local instances
export AWS_ENDPOINT_URL_S3=http://loclahost:9000
export AWS_ENDPOINT_URL_DYNAMODB=http://localhost:9001
#!/usr/bin/env escript
main(_) ->
    [code:add_pathz(E) || E <- filelib:wildcard("_build/default/lib/*/ebin")],
    {ok, _} = application:ensure_all_started(hackney),
    Env = fun(K) -> case os:getenv(K) of false -> halt(1); V -> list_to_binary(V) end end,
    Client = aws_client:make_client(Env("AWS_ACCESS_KEY_ID"), Env("AWS_SECRET_ACCESS_KEY"), Env("AWS_REGION")),
    io:format("DDB:  ~p~n", [aws_dynamodb:list_tables(Client, #{}, [])]),
    io:format("S3:   ~p~n", [aws_s3:list_buckets(Client, #{}, #{}, [])]).

Depends on aws_beam_core:apply_endpoint_url_override
Comment thread priv/post.erl.eex
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of the resulting indentation but I won't stop it 😄

    DefaultHost = build_host(<<"chatbot">>, Client1),
    URL0 = build_url(DefaultHost, Path, Client1),
    PathBin = erlang:iolist_to_binary(Path),
    {URL1, Host} = aws_util:apply_endpoint_url_override(
        URL0, DefaultHost, PathBin,
        <<"AWS_ENDPOINT_URL_CHATBOT">>),
    URL = aws_request:add_query(URL1, Query),

I would have personally preferred:

    DefaultHost = build_host(<<"chatbot">>, Client1),
    URL0 = build_url(DefaultHost, Path, Client1),
    PathBin = erlang:iolist_to_binary(Path),
    {URL1, Host} = aws_util:apply_endpoint_url_override(URL0, DefaultHost, PathBin, <<"AWS_ENDPOINT_URL_CHATBOT">>),
    URL = aws_request:add_query(URL1, Query),

Then again, I know some folks are incredibly picky about their line-length 😆

signing_name: signing_name,
signature_version: AWS.CodeGen.Util.get_signature_version(service),
service_id: AWS.CodeGen.Util.get_service_id(service),
endpoint_url_env_var: AWS.CodeGen.Util.endpoint_url_env_var(service),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use below instead and save yourself adding something to the util.ex that essentially does the same thing.

Suggested change
endpoint_url_env_var: AWS.CodeGen.Util.endpoint_url_env_var(service),
endpoint_url_env_var: "AWS_ENDPOINT_URL_" <> spec.module_name |> String.upcase(),

signing_name: signing_name,
signature_version: AWS.CodeGen.Util.get_signature_version(service),
service_id: AWS.CodeGen.Util.get_service_id(service),
endpoint_url_env_var: AWS.CodeGen.Util.endpoint_url_env_var(service),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use below instead and save yourself adding something to the util.ex that essentially does the same thing.

Suggested change
endpoint_url_env_var: AWS.CodeGen.Util.endpoint_url_env_var(service),
endpoint_url_env_var: "AWS_ENDPOINT_URL_" <> spec.module_name |> String.upcase(),

@onno-vos-dev
Copy link
Copy Markdown
Member

onno-vos-dev commented May 26, 2026

@vulkoingim I'll leave it up to you if you wanna address my comments. If I don't hear back from you by tomorrow, I'll merge it anyway but I think my comments would be cleaner. At least the obsolete util.ex code. Parsing these things is kinda brittle so the less places that deal with this sort of logic, the better.

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

Successfully merging this pull request may close these issues.

2 participants