This Lambda function acts as a proxy API that validates incoming requests, forwards them to the specified URL, and returns the response. It allows for custom headers, methods, parameters, and timeout settings, and supports both JSON and URL-encoded content types. The function is secured by an API key.
- Install dependencies:
npm install- Create a
.envfile in the root of the project with the following environment variables:
USER_AGENT=YourUserAgent
API_KEY=YourApiKey
- Deploy the Lambda function:
npm run deployIt's actually updating the existing function, so you can run this command once it's created for the first time.
You can use Axios to make a sample request to the Lambda function.
const axios = require("axios");
(async () => {
try {
const response = await axios.post("https://your-lambda-endpoint.amazonaws.com", {
url: "https://jsonplaceholder.typicode.com/posts",
method: "GET",
headers: { "x-api-key": "YourApiKey" },
timeout: 5000
});
console.log("Response:", response.data);
} catch (error) {
console.error("Error:", error.response ? error.response.data : error.message);
}
})();url(string, required): The URL to forward the request to.method(string, optional): HTTP method, default isGET. Supported values areGET,POST,PUT,DELETE,PATCH.headers(object, optional): Custom headers for the request. Default includes a customUser-Agent.params(object, optional): Query parameters.body(object/string, optional): Body content for methods likePOST.timeout(number, optional): Request timeout in milliseconds, default is5000.
Note: Ensure you include the correct API key in the x-api-key header to authenticate requests.
- Returns the HTTP status code and response from the forwarded request.
- In case of errors, returns an error message and relevant status code.