Skip to content

Instantly share code, notes, and snippets.

@formido
Created May 22, 2010 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save formido/409600 to your computer and use it in GitHub Desktop.
Save formido/409600 to your computer and use it in GitHub Desktop.
%% process for signing ec2 api requests:
%% http://docs.amazonwebservices.com/AWSEC2/latest/DeveloperGuide/index.html?using-query-api.html
s_query(PLparams) ->
{ok,SaccKeyId} = application:get_env(monitor,amazon_access_key_id),
{{Iyr,Imth,Iday},{Ihr,Imin,Isec}} = erlang:universaltime(),
Sdatetime0 = io_lib:format("~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ",
[Iyr,Imth,Iday,Ihr,Imin,Isec]),
Sdatetime1 = lists:flatten(Sdatetime0),
PL0 = [{"SignatureMethod","HmacSHA1"},
{"SignatureVersion","2"},
{"AWSAccessKeyId",SaccKeyId},
{"Version","2009-11-30"},
{"Timestamp",Sdatetime1}
| PLparams],
PL1 = lists:keysort(1, PL0),
PL2 = [{url_encode(Skey),
url_encode(Sval)}
|| {Skey,Sval} <- PL1],
Sparams = string:join([Skey++"="++Sval
|| {Skey,Sval} <- PL2], "&"),
Ssignature0 = lists:flatten(io_lib:format("GET~nec2.amazonaws.com~n/~n~s", [Sparams])),
{ok,SaccKey} = application:get_env(monitor,amazon_secret_access_key),
Ssignature1 = binary_to_list(crypto:sha_mac(SaccKey,Ssignature0)),
Ssignature2 = ibrowse_lib:encode_base64(Ssignature1),
Ssignature3 = url_encode(Ssignature2),
"http://ec2.amazonaws.com/?" ++ Sparams ++ "&Signature=" ++ Ssignature3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment