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!

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠