Serverless URL shortener powered by AWS services
- Go compiler (
brew install go
) - AWS CLI (
brew install awscli
or check https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) - AWS SAM CLI (
brew tap aws/tap; brew install aws-sam-cli
or check https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
Using the AWS CLI create a new bucket where the deployment packages will be uploaded:
$ aws s3 mb s3://my-deploy-bucket
The deployment can be executed in a single step running:
$ make install S3_BUCKET=my-deploy-bucket STACK_NAME=my-stack-name
That command will:
- Compile lambda handlers written in Go
- Build a AWS SAM deployment package (CloudFormation package) and upload to the S3 bucket
- Deploy or upgrade the CloudFormation stack and wait until it's completed
In order to use the service at least one [API Key] and Usage Plan must be created to access the API. This can be done from the AWS Console. For details check: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-usage-plans.html
Steps to expose the API with a hostname are out of the scope of this document, but in short you need:
- Registered domain name
- The zone for the domain name registered in Route53
- Certificate for the domain generated by the AWS Certificate Manager
- A custom domain declared within Amazon API Gateway pointing to the "Prod" stage of the API
- An entry within the domain zone aliasing the target domain name
To create new short URL entries in the service use the POST /api/v1/links
endpoint passing the desired target url
in the query string. For example:
$ curl -XPOST https://nnn.dd/api/v1/links?url=https://google.com -H "X-Api-Key: Mq1lLPeXe99bfjUc4hWnn7R0iqY2CeWNza292yw0"
{"key":"YdhnqV","url":"https://google.com"}
Once the entry is created the short url can be used
$ curl -v https://nnn.dd/YdhnqV
...
HTTP/1.1 302
Location: https://google.com
...