Skip to content

Allowing MediaTailor Access Through CloudFront Function Security Rules (401 Errors)

0

I recently integrated AWS Elemental MediaTailor (server-side ad insertion) in my stage environment, and everything worked correctly.

However, when moving the same configuration to production, playback fails with:

  • 504 Gateway Timeout in the browser
  • MediaTailor logs showing MANIFEST_ERROR → httpCode: 401
  • CloudFront logs showing FunctionGeneratedResponse 401 for .m3u8 manifest requests

After investigation, I found that the issue occurs only when a CloudFront Function is attached to the distribution.

This CloudFront Function validates the Origin and User-Agent headers to block unauthorized access. Because MediaTailor internally fetches the origin manifest (e.g., .m3u8) using its own AWS backend User-Agent and often without an Origin header, the CloudFront Function incorrectly returns 401, which causes MediaTailor to fail when fetching the content.

Result

MANIFEST_ERROR cause: "Error obtaining template manifest" httpCode: 401

And in CloudFront logs:

x-edge-result-type: FunctionGeneratedResponse
sc-status: 401
cs-uri-stem: /test/ACCUSSED_Stereo.m3u8

If I remove the CloudFront Function, MediaTailor works perfectly in production.

What I need help with

How do I correctly configure CloudFront so that MediaTailor is allowed to fetch:

  • manifests (.m3u8, .mpd)
  • ad segments (.ts, .m4s)
  • tracking paths (/segment/, /tm/)
  • personalized manifests (/v1/*)

…while still keeping my CloudFront Function logic for blocking unauthorized browsers?

I want to allow MediaTailor traffic without weakening security for public clients.

Any guidance or a best-practice CloudFront Function example for this scenario would be appreciated.

1 Answer
0

The issue you're experiencing is due to your CloudFront Function blocking MediaTailor's requests because they don't include the expected Origin header or User-Agent values that your security rules require.

To solve this problem while maintaining security, you need to modify your CloudFront Function to specifically allow requests from MediaTailor's service. Here's how you can approach this:

  1. Identify MediaTailor requests: AWS Elemental MediaTailor makes requests to your origin using its own service domain. You can identify these requests by checking for the MediaTailor service domain in the request headers.

  2. Modify your CloudFront Function: Update your function to include an exception for MediaTailor by checking for its service identifier. For example:

function handler(event) {
var request = event.request;
var headers = request.headers;

// Check if the request is from MediaTailor
if (headers['user-agent'] && headers['user-agent'].value.includes('mediatailor.amazonaws.com')) {
// Allow MediaTailor requests
return request;
}

// Your existing security checks for other clients
if (!headers['origin'] || !headers['user-agent']) {
return {
statusCode: 401,
statusDescription: 'Unauthorized'
};
}

// Rest of your validation logic
// ...

return request;
}
  1. Consider using a condition based on the request path: If MediaTailor only accesses specific paths, you can also add path-based conditions to your function:
// Allow requests to manifest and segment paths that might be accessed by MediaTailor
if (request.uri.endsWith('.m3u8') || request.uri.endsWith('.mpd') || 
request.uri.endsWith('.ts') || request.uri.endsWith('.m4s') ||
request.uri.includes('/segment/') || request.uri.includes('/tm/') ||
request.uri.includes('/v1/')) {

// Additional checks specific to MediaTailor requests
// ...
}
  1. Consider using Origin Access Control (OAC) or Origin Access Identity (OAI) for your S3 bucket if that's your origin, rather than relying solely on header validation.

  2. Use separate cache behaviors: You could create separate cache behaviors in your CloudFront distribution - one for paths accessed by MediaTailor without the function attached, and another for public access with your security function.

By implementing these changes, you can maintain your security controls for public clients while allowing MediaTailor to access the content it needs for ad insertion.
Sources
Securing AWS Elemental MediaTailor origin interactions with SigV4 - AWS Elemental MediaTailor
Cloudfront CDN is not working with Videos | AWS re:Post
Troubleshoot common CloudFormation deployment issues for CDN and MediaTailor integrations - AWS Elemental MediaTailor

answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.