Am I able to save a Canaries "text/xml" responseBody to S3

0

I have Canaries set that monitor feeds and retain 24h of the responses; When performing this for feeds with the mime type "text/xml", the responseBody log in the HttpRequestReport states "Content Type is not supported"

Please advise how I may retain the XML data from a Canary run where save resposneBody is enabled

Mike D
已提問 8 個月前檢視次數 557 次
1 個回答
1
已接受的答案

Hi Mike,

Let's see if I can help you!

The error "Content Type is not supported" suggests that while your Canary is trying to log the response body, the built-in CloudWatch Synthetics functionality does not natively support the "text/xml" content type for storing response data.

To retain the XML data from a Canary run, you can consider the following approach:

  1. Modify your script to manually capture the XML response.
  2. Within the same script, use the AWS SDK to store the XML data to an S3 bucket.

Here is a Python code:

import boto3
import os

# Initialize the S3 client
s3_client = boto3.client('s3')

# Set the bucket name from an environment variable.
# Make sure to set the 'BUCKET_NAME' environment variable in the AWS Lambda console.
BUCKET_NAME = os.environ['BUCKET_NAME']

def lambda_handler(event, context):
    # Assume the XML data comes as a string in the 'xml_data' field of the event.
    xml_data = event['xml_data']
    
    # Define the path where the XML file should be saved in S3. Replace 'path/to/your' with your desired path.
    object_name = 'path/to/your/xmlfile.xml'  

    try:
        # Upload the XML data to the specified S3 bucket and path.
        response = s3_client.put_object(Bucket=BUCKET_NAME, Key=object_name, Body=xml_data, ContentType='text/xml')
        return {
            'statusCode': 200,
            'message': f"XML saved successfully in {BUCKET_NAME}/{object_name}"
        }
    except Exception as e:
        print(e)
        return {
            'statusCode': 500,
            'message': 'Error saving XML to S3'
        }

Ensure the necessary IAM permissions are granted to the role associated with the Canary so that it can read and write to the desired S3 bucket.

I'll keep tracking your post to see if you've found a resolution.

profile picture
已回答 8 個月前
profile pictureAWS
專家
已審閱 8 個月前
  • Thanks Victor! step 2 is a bit unnecessary as you can simply direct the response to the logger as I have done, but your response got me there; thanks again!

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南