Amazon API Gateway, AWS Lambda and Amazon DynamoDB with Lambda SnapStart for Java deployed with Terraform
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- Terraform installed
- Maven
- Artillery CLI
- Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/serverless-patterns
- Change directory to the pattern directory:
cd apigw-lambda-snapstart-terraform
- Build the function:
cd UnicornStockLambda mvn package cd ..
- From the command line, initialize terraform to download and install the providers defined in the configuration:
terraform init
- Deploy the infrastructure
terraform apply
- Confirm that it was deployed correctly
terraform show
To test the Lambda function you can run the artillery load test with the following commands
export API_GW_URL=$(terraform output -raw apigw-url)
artillery run -t $API_GW_URL -v '{ "url": "/transactions" }' UnicornStockLambda/misc/loadtest.yaml
You can use the following AWS CloudWatch Insights query to measure the duration your SnapStart Lambda function.
filter @type = "REPORT"
| parse @log /\d+:\/aws\/lambda\/(?<function>.*)/
| parse @message /Restore Duration: (?<restoreDuration>.*?) ms/
| stats
count(*) as invocations,
pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 0) as p0,
pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 25) as p25,
pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 50) as p50,
pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 90) as p90,
pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 95) as p95,
pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 99) as p99,
pct(@duration+coalesce(@initDuration,0)+coalesce(restoreDuration,0), 100) as p100
group by function, (ispresent(@initDuration) or ispresent(restoreDuration)) as coldstart
| sort by coldstart desc
- Change directory to the pattern directory:
cd apigw-lambda-snapstart-terraform
- Delete all created resources
terraform destroy
- During the prompts:
- Enter yes
- Confirm all created resources has been deleted
terraform show