LambaEdge function gives 503 error

I have an s3 + CloudFront setup. So I've pdf files inside a folder, and I want those pdf files to deindexed from google search. But blocking them with robots.txt didn't help. So I read somewhere that I've to add response headers X-Robots-Tags: noindex to those pdf files. So we created a dummy s3 bucket with few pdf files and made another CloudFront distribution and with route53 and AWS certificate manager and HTTPS enabled subdomain with few pdf files.
Then we added ```x-amz-meta-X-Robots-Tag:noindex``` to those pdf files.

We followed this "" We created a new Lambda IAM role that has the inline and trust relationship policies below. (for CloudFront trigger).

{ "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRole", "Principal": { "Service": [ "", "" ] }, "Effect": "Allow" } ] } 

We used python to write the function. Here is the code snippet.

def lambda_handler(event, context): response = event['Records'][0]['cf']['response'] headers = response['headers'] custom_header_prefix = 'x-amz-meta-header-' for old_header in list(headers): if old_header.lower().startswith(custom_header_prefix): new_header = old_header.lower().replace(custom_header_prefix, '') headers[new_header] = [ {'key': new_header.title(), 'value': headers[old_header][0] ['value']} ] del headers[old_header] print('Header %s was replaced with %s.' % (old_header, new_header)) return response 

After deploying, we invalidated the cache in the CloudFront distribution. Then we save this error when opened the website. 503 ERROR The request could not be satisfied. The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner. If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.

Any idea how to solve this issue?

Thanks in advance.

submitted by /u/the_nerd_designer
[link] [comments]