"Insufficient Logging & Monitoring" is one of the Top 10 Application Security Risks ranked by OWASP in 2017.
AWS X-Ray gives you visibility into the data flow of your microservices architecture and a map of how your application’s underlying components are connected. It's a great tool to troubleshoot performance and debug errors. However, given the ephemeral nature of the infrastructure in a serverless application, this visibility into your application is also critical for the purpose of security:
- It helps you understand the "norm" of the data flow, interdependencies, and performance characteristics of your distributed serverless components. Knowing that is a prerequisite of recognoizing when things are not normal.
- During an security incident or post analysis, X-Ray can give you insights into what your code is doing at runtime, what downstream dependency it's making calls to, where the code is spending its time
In the Cloud9 IDE environment, go to the SAM template (template.yaml
), find the Globals section, which contains settings that all resources in the SAM template share unless explicitly overwritten.
Globals:
Function:
Timeout: 30
...
Add Tracing: Active
to the configuration section for lambda functions:
Globals:
Function:
Timeout: 30
Tracing: Active
...
When our applications makes calls to AWS services such as Secrets Manager, DynamoDB, S3 etc., the X-Ray SDK can help tracks the calls downstream and record the request timing, status, etc. about the AWS Service call.
To enable this, you can instrument all AWS SDK clients by wrapping your aws-sdk
require statement in a call to AWSXRay.captureAWS
The Lambda authorizer you added in Module 1: Auth uses the AWS SDK to look up values from a DynamoDB table. We can instrument the AWS SDK with X-Ray:
-
Install the XRay SDK in the
authorizer/
folder by running in a terminalcd ~/environment/aws-serverless-security-workshop/src/authorizer npm install aws-xray-sdk-core --save
-
In
authorizer/index.js
, find the line where the AWS SDK is imported:const AWS = require('aws-sdk');
And replace it with:
const AWSXRay = require('aws-xray-sdk-core'); const AWS = AWSXRay.captureAWS(require('aws-sdk'));
If you haven't gone through Module 2: Secrets
The backend lambda functions currently doesn't use the AWS SDK, so no additional action needed!
If you have gone through Module 2: Secrets
If you have gone through Module 2: Secrets, you would have added the AWS SDK to dbUtils.js
so the code would retrieve the database username and password from AWS Secrets Manager
-
Install the XRay SDK in the
app/
folder by running in a terminalcd ~/environment/aws-serverless-security-workshop/src/app npm install aws-xray-sdk-core --save
-
In
app/dbUtils.js
, find the line where the AWS SDK is imported:const AWS = require('aws-sdk');
And replace it with:
const AWSXRay = require('aws-xray-sdk-core'); const AWS = AWSXRay.captureAWS(require('aws-sdk'));
-
In the terminal, validate the SAM template:
cd ~/environment/aws-serverless-security-workshop/src/ sam validate -t template.yaml
-
Deploy the updates:
aws cloudformation package --output-template-file packaged.yaml --template-file template.yaml --s3-bucket $BUCKET --s3-prefix securityworkshop --region $REGION && aws cloudformation deploy --template-file packaged.yaml --stack-name CustomizeUnicorns --region $REGION --parameter-overrides InitResourceStack=Secure-Serverless --capabilities CAPABILITY_IAM
-
Once the deployment finishes, test making API requests again with postman.
-
Go to the X-Ray console, go to the Service map tab and refresh. You should start seeing some lambda requests getting captured!
-
Go to API Gateway Console, and go to the
CustomizeUnicorns
API -
Go to the Stages tab, click on the
dev
stage -
Find the Logs/Tracing tab, check the box for Enable X-Ray Tracing, and Save changes
-
Redeploy the API by clicking on the Resources tab on the left hand side --> Actions --> Deploy API -> Pick the
dev
stage --> deploy. -
Test making a few making API requests with postman.
-
Go to the X-Ray console, go to the Service map tab and refresh
-
Explore the service map. Click on various components, and use View traces to see a list of request traces captured by X-Ray
-
Explore the individual traces by clicking into individual rquests
Return to the workshop landing page to pick another module.