Deployment using Terraform
The Terraform template creates AWS resources to enable the monitoring of AWS API gateway logs. The template creates an EC2 instance where Traceable services are running. Traceable fetches the cloudwatch logs every 5 minutes, parses the data, and sends it to the Traceable platform. The processed data is displayed in the Traceable UI. The following flow diagram displays how the traffic flows through:

Before you begin
Make a note of the following points before proceeding with configurations:
Keep Traceable's access token handy. It will be used when configuring the variables in the tfvars (
*.tfvars)file. You can copy the access token by logging into your Traceable account and then navigating to Settings ()→Account → Agent Token.Make sure that Terraform is already installed. For more information on installing Terraform, see Download Terraform.
REST API gateway
To monitor the REST API gateway, complete the following:
Navigate to the console. aws.amazon.com/apigateway/.
Select the API and then select the stage.
Navigate to Logs/tracing.
Select Full Request and Response Logs from Cloudwatch settings → Cloudwatch Logs drop-down list.
Mark Enable Access Logging as
trueunder Custom Access Logging and append the following JSON in Log Format:
HTTP API gateway
To monitor the HTTP API gateway, enable access logging and append the JSON mentioned above in the Log Format.
Configure AWS
Configure AWS in your shell and verify that the region is set correctly. Enter the following command to set up your AWS CLI installation:
The following example shows sample values. Replace them with your values to configure the credentials correctly.
For more information on the credentials file, see Configuration and credential file settings.
If you have configured named AWS profiles, export the environment variable AWS_PROFILE=myprofile where the profile named myprofile has the credentials that you wish to use to deploy the Traceable mirroring resources.
Finally, run the following command and verify that the AWS region is set to the region where you wish to install Traceable:
Alternatively, you can configure the following environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION or AWS_DEFAULT_REGION
AWS_DEFAULT_OUTPUT
For more configuration information, see AWS documentation.
Multiple accounts
A single AWS API Gateway account can support multiple accounts across different regions. However, each region within an account must be configured separately. This setup requires establishing cross-account IAM roles in each account, allowing the host account to authenticate and access the necessary resources.
Cross account IAM role
An IAM role is required to authenticate the host account to monitor the API gateways from another account. The following are the required permissions:
Alternatively, you can use the following Terraform template to create cross account role. Override the account_id variable with the host account’s ID.
Download
Traceable provides AWS API gateway traffic mirroring tarball. Complete the following steps to download and untar the tarball:
Enter the following command to download the tarball:
Untar the tarball. Enter the following command:
Change directory. Enter the following command:
Create tfvars file
Create a terraform.tfvars file with terraform variables as shown below:
Configuration variables
The following tables describe the various terraform variables.
accounts
Mandatory
-
Refer to the next accounts section for the description.
subnet_id
Mandatory
""
The subnet ID where the Traceable instance is created.
instance_type
Optional
m4.xlarge
The type of Traceable instance.
traceable_agent_endpoint
Optional
http://localhost:5442
The Traceable Platform agent reporting endpoint for the agent. Platform agent deployment will also be done on the same VM if not overridden. For more information, see the Traceable Platform agent.
traceable_agent_max_batch_size
Optional
500
The maximum number of spans that you wish to bundle in a single request to the Traceable Platform agent.
root_directory
Optional
/var/traceable/aws-api-gateway
Root directory path for traceable files.
log_directory
Optional
/var/traceable/log/aws-api-gateway
Log directory path for traceable log files.
api_gateway_stages_arns
Optional
*
Enter a comma-separated list of ARNs of stages of API Gateways for creating the IAM policy.
For Example, arn:aws:apigateway:us-east-1::/restapis/api_id/stages/stage_name
log_group_arns
Optional
*
The ARNs of log groups used by the API gateways in this account. This is used to create the IAM policy used by the agent instance.
tags
Optional
Tags that should be attached to resources created.
cluster_arn
Mandatory if using ECS
-
ECS Fargate Cluster ARN for running the tasks
deployment_mode
ec2
This configuration controls whether to make an EC2-based deployment or an ECS-based one. Allowed values are ecs and ec2
enable_logging
Optional
false
On ECS, the logs can be saved to cloudwatch. Allowed values are true and false.
agent_image
Mandatory
traceableai/awsapigw:<TEMPLATE_VERSION>
AWS API Gateway Agent image is to be deployed.
allow_images_from_ecr
Optional
true
Configure IAM role to allow pulling images from ECR.
cron_interval_mins
Optional
5 minutes
Time interval (in minutes) at which the cron job is triggered.
IAM roles for ECS deployment
If you choose ECS deployment, the following IAM roles are created:
1. IAM Role for the ECS Task to Call AWS SDK APIs
Key Permissions:
API Gateway Access (
apigateway:GET): This allows the ECS task to perform GET operations on API Gateway. This could be for retrieving API configurations or monitoring API statuses.CloudWatch Logs Access (
logs:describeLogGroups,logs:FilterLogEvents): These permissions allow the task to describe CloudWatch log groups and filter log events. This is useful for the ECS task to monitor logs if needed.Cross-Account Role Assumption (
sts:AssumeRole): This permission allows the task to assume a cross-account role. This is important when the ECS task needs to access resources from a different AWS account.
2. IAM Role for Task Execution
Key Permissions:
Pulling Docker Images from ECR (
ecr:GetDownloadUrlForLayer,ecr:GetAuthorizationToken,ecr:BatchGetImage,ecr:BatchCheckLayerAvailability): These permissions allow the ECS service to pull container images from AWS ECR, which is required to run the task.CloudWatch Logs Access (
logs:PutLogEvents,logs:CreateLogStream): These permissions allow the ECS task to write logs to CloudWatch by creating log streams and putting log events into them.
3. IAM Role for EventBridge Scheduler
Key Permissions:
ECS Task Execution (
ecs:RunTask): This permission allows EventBridge to trigger the execution of ECS tasks. The resources specified here are the specific ECS task definitions that can be run.IAM Role Passing (
iam:PassRole): This permission allows EventBridge to pass the necessary IAM roles (the task execution role and instance role) to the ECS tasks when they are triggered. Without this permission, EventBridge would not be able to assign the correct roles to the tasks it runs.
accounts
The accounts object has the following values:
region
Mandatory
The AWS region from where you wish to capture the API traffic.
exlude
Mandatory
When set to true, the traffic is captured from all the API gateways except the ones provided in the api_list. When set to false, the traffic is captured only from the API listed in the api_list.
api_list
Mandatory
List of API IDs to consider for the above action.
API_ID— Ifexclude=false, then monitor the API gateway that has ID = API_ID. However, ifexclude=true, then do not monitor this API gateway.API_ID/STAGE_NAME— Ifexclude=false, then monitor the API gateway stage with name =STAGE_NAMEand ID =API_ID. Ifexclude=true, then do not monitor this stage.
cross_account_role_arn
Mandatory
The ARN of the IAM role that authenticates this account for accessing the required resources from another account. Provide an empty string if the listed API gateways are in the same account.
Example
The following example explains the configuration. The configuration is for 3 regions across 2 accounts.
The first object is for the region
us-east-1in Account 1. It will capture traffic only from the API gateway instance with the ID625vetvy0fin this account. It is assumed that Account 1 is the host account (where the agent instance is deployed).The second object is for the region
us-east-2in Account 1. It will capture traffic from all the API gateway instances in this region in this account.The third object is for the region
us-east-1in Account 2. It will capture traffic from all the API gateway instances in this region in this account. Thecross_account_role_arnin this case, it is the ARN of the IAM Role authenticating Account 1 to access required objects in Account 2.The fourth object is for region
us-west-1in Account 2. It will capture traffic only from the stageexample-stageof the API gateway instance with the IDexample-apiin this account.
Traceable Platform Agent
Configure these parameters only when you wish to deploy the Traceable Platform agent alongside the tracing agent instance. The Traceable Platform Agent will be deployed alongside the agent instance only if the parameter traceable_agent_endpoint of above is configured as http://localhost:5442.
traceable_refresh_token
Traceable Platform token. This is the token you have generated as part of a step in the Before you begin section.
-
traceable_environment
Defines the Traceable environment, for example, dev, QE, staging, etc.
aws-api-gateway
traceable_api_endpoint
Traceable Platform API endpoint
api.traceable.ai
service_version
Traceable Platform Agent version to be installed.
-
Apply terraform
As a last step to configure, run the following command to apply the terraform changes:
Verification
Log into your Traceable Platform account and navigate to API Catalog → Services to view the service name (traceable_service_name) that you configured earlier.
Uninstall
To uninstall, run the following command from the same directory to destroy all the resources created in the installation step:
Troubleshooting
Loss of traffic
If your setup is experiencing a loss of traffic or observing less amount of traffic, you may try doing the following:
Login to the Traceable EC2 instance you created using the Terraform template earlier. The instance's name should be with the prefix
traceable-instance-.After you log in, check the service status of the running service. Enter the following command:
You may also view the logs generated by Traceable by navigating to the logs directory:
Last updated
Was this helpful?