Skip to main content

AWS Request V4

Latest Version: 1.0.2

This class can be used to generate correctly structured requests intended for Amazon Web Services (AWS) endpoints, sign the requests using Amazon’s “Signature Version 4”, and send them. It is intended to be used internally by wrapper classes for specific AWS services.

To use the class yourself — for example, if there is no corresponding wrapper class for the service you’re working with — you’ll need the following info:

  • Service name (eg. "iam" or "firehose")
  • Region (eg. "us-east-1")
  • Access Key ID and Secret Access Key (from IAM)
  • Knowledge of the required headers and request body contents and formatting

See also the AWSKinesisFirehose class

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To add this library to your project, add #require "AWSRequestV4.class.nut:1.0.2" to the top of your agent code

Class Usage

Constructor: AWSRequestV4(service, region, accessKeyID, secretAccessKey)

All parameters are strings.

Currently, the constructor auto-generates the endpoint URL in the form https://..amazonaws.com/ though that may change if it turns out not to be that generalizable.

Class Methods

request(method, path, queryString, headers, body, callback)

Parameter Type Description
method String "POST", "GET", etc.
path String This is frequently just "/" (the actual URL for the request is generated from the service and region)
queryString String Everything after the ? in the URL (if applicable, otherwise just pass "")
headers Table Any additional headers necessary for the request. General headers, like X-Amz-Date, are included automatically, but service-specific headers, such as X-Amz-Target, must be added here
body String The request body. (Hint: create a table and then pass it through http.jsonencode())
callback Function A callback function that will be called when the request completes. It should take one argument: a response table

post(path, headers, body, callback)

Wrapper for request() (see above) where method is set to "POST" and queryString is passed "".

Example

#require "AWSRequestV4.class.nut:1.0.2"

const ACCESS_KEY_ID = "<YOUR_KEY_ID>";
const SECRET_ACCESS_KEY = "<YOUR_SECRET_ACCESS_KEY>";

aws <- AWSRequestV4("firehose", "us-east-1", ACCESS_KEY_ID, SECRET_ACCESS_KEY);

local headers = {
    "X-Amz-Target": "Firehose_20150804.PutRecord"
};

local body = {
    "DeliveryStreamName": "myDeliveryStream",
    "Record": {
        "Data": http.base64encode("my super important data string")
    }
};

aws.post("/", headers, http.jsonencode(body), function(response) {
    server.log(response.statuscode + ": " + response.body);
});

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release
1.0.1 GitHub Minor code improvements
1.0.2 GitHub Minor code improvements

License

The AWSRequestV4 library is licensed under the MIT License.