This pattern creates an Amazon API Gateway with access logging to Amazon CloudWatch configured.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-cloudwatch
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage – please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
Requirements
- 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
- AWS Serverless Application Model (AWS SAM) installed
Deployment Instructions
-
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-cloudwatch
-
From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
sam deploy --guided
-
During the prompts:
- Enter a stack name
- Enter the desired AWS Region
- Allow SAM CLI to create IAM roles with the required permissions.
Once you have run
sam deploy --guided
mode once and saved arguments to a configuration file (samconfig.toml), you can usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
How it works
The application will only accept requests from countries that are in the country code array for the WAF rule.
Testing
Deploy the application and then go to the oputput URL. Refresh the screen multiple times. Then go to Amazon CloudWatch and check the log group that is created. The name will be in the outputs. You will see something like:
{
"requestId": "d14676c9-a4a3-46e0-9168-87ed1a429eb3",
"waf-error": "-",
"waf-status": "-",
"waf-latency": "-",
"waf-response": "-",
"authenticate-error": "-",
"authenticate-status": "-",
"authenticate-latency": "-",
"authorize-error": "-",
"authorize-status": "-",
"authorize-latency": "-",
"integration-error": "-",
"integration-status": "200",
"integration-latency": "15",
"integration-requestId": "55c51049-49c2-42f6-bb23-48e9595094da",
"integration-integrationStatus": "200",
"response-latency": "17",
"status": "200"
}
You can add authorization and a WAF to see the other data filled in.
Cleanup
- Delete the stack
sam delete --stack-name STACK_NAME
- Confirm the stack has been deleted
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
Nguồn: viblo.asia