Serverless AWS Lambda URL shortener backed by S3. It exposes:
POST /to create a short URL. This route is protected with an API key.GET /{short_code}to issue a301redirect to the stored URL.- A default fallback redirect when a short code is missing or unknown.
- Use Node.js 20.
- Install dependencies with
yarn install. - Copy
serverless.yml.exampletoserverless.yml. - Replace the placeholder values in
serverless.yml:bucketbaseUrl(BASE_URL; this must be the deployed API Gateway URL unless you have already mapped a custom domain)defaultRedirectUrlapiKeyNameprofileregionstage
- Deploy with
yarn serverless deployornpx serverless deploy. - Set
BASE_URLto the deployed API Gateway invoke URL. Only change it after you have configured a custom domain that points to the same deployment.
yarn typecheckyarn lintyarn testyarn build
The Lambda functions receive these environment variables through Serverless:
| Variable | Required | Description |
|---|---|---|
BUCKET |
Yes | S3 bucket used to store short-code objects. |
BASE_URL |
Yes | Public base URL returned in shortUrl, for example https://sho.rt. |
DEFAULT_REDIRECT_URL |
Yes | Redirect target used when a code is missing or unknown. |
SHORT_CODE_LENGTH |
No | Length of generated codes. Default: 6. |
MAX_GENERATION_ATTEMPTS |
No | Max collision retries before returning a failure. Default: 10. |
BASE_URL is the public short-link domain returned in shortUrl. It must match the deployed API Gateway invoke URL until a custom domain is mapped to that deployment. For example, if BASE_URL=https://sho.rt, successful create requests return links such as https://sho.rt/a1B2c3.
Request:
curl --request POST \
--url "https://your-api-id.execute-api.us-east-1.amazonaws.com/production/" \
--header "Content-Type: application/json" \
--header "x-api-key: YOUR_API_KEY" \
--data '{"url":"https://www.example.com/articles/introducing-url-shortener"}'Success response:
{
"longUrl": "https://www.example.com/articles/introducing-url-shortener",
"shortUrl": "https://sho.rt/a1B2c3"
}Validation failures:
400when the request body is missing or not valid JSON.422whenurlis missing or is not a validhttp/httpsURL.500when the service cannot store the new URL.503when unique code generation exhausts the configured retry budget.
Example invalid request response:
{
"message": "url must be a valid http or https URL"
}Request:
curl --include \
--request GET \
--url "https://sho.rt/a1B2c3"https://sho.rt in this example is BASE_URL. If no custom domain is configured, BASE_URL should be the API Gateway invoke URL instead.
Response:
HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/articles/introducing-url-shortenerWhen the code is not found, the service still returns 301, but the Location header points to DEFAULT_REDIRECT_URL.
POST /is configured as a private API Gateway route, so callers need a valid API key.- Create the S3 bucket before deploying.
- A fresh
serverless deploygives you an API Gateway invoke URL, andBASE_URLshould point to that URL until a custom domain is configured. - After deployment, configure a custom domain in API Gateway and map the base path to
/if you want a branded short domain. - Keep
BASE_URLaligned with the public domain you expose to clients; otherwise generatedshortUrlvalues will be incorrect.