Skip to content

Instantly share code, notes, and snippets.

@bestickley
Last active March 3, 2021 14:41
Show Gist options
  • Save bestickley/43ff142b14243a66d33b8f30c0e95443 to your computer and use it in GitHub Desktop.
Save bestickley/43ff142b14243a66d33b8f30c0e95443 to your computer and use it in GitHub Desktop.
import { SignatureV4 } from "@aws-sdk/signature-v4";
import { HttpRequest } from "@aws-sdk/protocol-http";
import { HttpRequest as IHttpRequest } from "@aws-sdk/types";
import { Sha256 } from "@aws-crypto/sha256-js";
import { defaultProvider } from "@aws-sdk/credential-provider-node";
interface CreateSignHttpRequestParams {
body?: string;
headers?: Record<string, string>;
hostname: string;
method?: string;
path?: string;
port?: number;
protocol?: string;
query?: Record<string, string>;
service: string;
}
export async function createSignedHttpRequest({
body,
headers,
hostname,
method = "GET",
path = "/",
port = 443,
protocol = "https:",
query,
service,
}: CreateSignHttpRequestParams): Promise<IHttpRequest> {
const httpRequest = new HttpRequest({
body,
headers,
hostname,
method,
path,
port,
protocol,
query,
});
const signer = new SignatureV4({
credentials: defaultProvider(),
region: process.env.AWS_DEFAULT_REGION as string,
service,
sha256: Sha256,
});
return signer.sign(httpRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment