This is a sample template for short-url-aws-lambda - Below is a brief explanation of what we have generated for you:
.
├── Makefile <-- Make to automate build
├── README.md <-- This instructions file
├── shorten <-- Source code for a lambda function
│ ├── main.go <-- Lambda function code
│ └── main_test.go <-- Unit tests
│ └── Dockerfile <-- Dockerfile
├── redirect <-- Source code for a lambda function
│ ├── main.go <-- Lambda function code
│ └── main_test.go <-- Unit tests
│ └── Dockerfile <-- Dockerfile
└── template.yaml
- AWS CLI already configured with Administrator permission
- Docker installed
- SAM CLI - Install the SAM CLI
You may need the following for local testing.
aws ecr create-repository --repository-name lambda-shorten \
--image-tag-mutability IMMUTABLE --image-scanning-configuration scanOnPush=true
aws ecr create-repository --repository-name lambda-redirect \
--image-tag-mutability IMMUTABLE --image-scanning-configuration scanOnPush=true
aws s3 mb s3://${your_bucket_name}
sam build
- Use AWS CLI with Parameters
sam deploy \
--stack-name ${your_stack_name} \
--s3-bucket ${your_bucket_name} \
--s3-prefix ${prefix_text} \
--region ${lambda_region} \
--confirm-changeset \
capabilities CAPABILITY_IAM \
--parameter-overrides TableName=${dynamodb_table_name} \
--image-repositories ShortenFunction=${your_aws_account_id}.dkr.ecr.${lambda_region}.amazonaws.com/lambda-shorten \
--image-repositories RedirectFunction=${your_aws_account_id}.dkr.ecr.${lambda_region}.amazonaws.com/lambda-redirect
- Use AWS CLI with
samconfig.toml
samconfig.toml
version = 0.1
[default]
[default.deploy]
[default.deploy.parameters]
stack_name = "${your_stack_name}"
s3_bucket = "${your_bucket_name}"
s3_prefix = "${prefix_text}"
region = "${lambda_region}"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
parameter_overrides = "TableName=${dynamodb_table_name}"
image_repositories = [
"ShortenFunction=${your_aws_account_id}.dkr.ecr.${lambda_region}.amazonaws.com/sample-shorten",
"RedirectFunction=${your_aws_account_id}.dkr.ecr.${lambda_region}.amazonaws.com/sample-redirect"
]
sam deploy
sam deploy
- Test
ShortenFunction
Request
curl -X POST -H 'Content-Type: application/json' -d '{"url": "${test_url}"}' \
https://${random_string}.execute-api.${lambda_region}.amazonaws.com/Prod/shorten
Response
{"short_url": "HAlqGWK7R"}
- Test
RedirectFunction
Open the browser and enter the Short URL
https://${random_string}.execute-api.${lambda_region}.amazonaws.com/Prod/HAlqGWK7R
sam delete --stack-name ${your_stack_name}