url-shortener-go is a service to shorten URL using AWS Lambda and DynamoDB
You have to prepare credentials with proper policies.
And,
- install aws-cli
- install aws-sam-cli. Docker is also required. Follow the instruction here.
- install direnv
- install saw
- you can watch CloudWatch logs on your terminal
- set environment variables to .envrc.sample and remove .sample.
- LINK_TABLE is DynamoDB table name where maps of your original URLs and shortend resource are gonna be saved.
- REGION is where your project is gonna be deployed.
- STACK_BUCKET is S3 bucket name for artifacts of SAM and should be unique globally.
$ dep ensure # to resolve dependency
$ aws s3 mb "s3://${STACK_BUCKET}" # for artifacts of SAM
$ make deploy
# Your endpoint is gonna be pirinted.
# You can change url for your own.
$ curl -X POST https://yyyyyyyyyy.execute-api.[your region].amazonaws.com/Prod/links -d '{"url":"http://toshi0607.com/"}'
xxxxxxxxx
then you can access https://yyyyyyyyyy.execute-api.[your region].amazonaws.com/Prod/links/xxxxxxxxx and it'll be redirected to the original URL.
Deploy is required before checking behavior.
$ saw groups
/aws/lambda/url-shortener-lambda-go-Redirect-XXXXXXXXXXXX
/aws/lambda/url-shortener-lambda-go-Shorten-XXXXXXXXXXXX
$ saw watch /aws/lambda/url-shortener-lambda-go-Redirect-XXXXXXXXXXXX &
$ saw watch /aws/lambda/url-shortener-lambda-go-Shorten-XXXXXXXXXXXX &
# open another window
$ curl -X POST https://yyyyyyyyyy.execute-api.[your region].amazonaws.com/Prod/links -d '{"url":"http://toshi0607.com/"}'
You can test AWS Lambda & API Gateway locally with AWS SAM (Serverless Application Model).
$ sam local start-api # you have to use DynamoDB
# You can change url for your own.
$ curl -X POST http://127.0.0.1:3000/links -d '{"url":"http://toshi0607.com/"'}
xxx
then you can access http://127.0.0.1:3000/links/xxxxxxxxx and it'll be redirected to the original URL.
DynamoDB local is useful as well.
You can try to use the DynamoDB local in test as below.
$ docker pull amazon/dynamodb-local
$ docker run -p 8000:8000 amazon/dynamodb-local
$ make test
- Go で学ぶ AWS Lambda(PDF、ePub セット版)
- This architecture is explained in detail in this book.
- 技術書典 5 で『Go で学ぶ AWS Lambda』を出展します #技術書典
- 技術書典 5 の『Go で学ぶ AWS Lambda』の振り返りとフィードバックのお願い #技術書典