CloudFront Function

1

Hi,
I'm trying to create a CloudFormation stack that deploys a CloudFront Function.
The specific code is:

AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFront Test Function
Resources:
  ResponseFunction:
    Type: 'AWS::CloudFront::Function'
    Properties:
      Name: !Sub '${AWS::StackName}-response'
      AutoPublish: true
      FunctionCode: |
        function handler(event) {
            var response = event.response;
            var headers = response.headers;
            headers\['strict-transport-security'] = {value: 'max-age=63072000; includeSubDomains; preload'};
            headers\['content-security-policy'] = {value: "default-src 'none';"};
            headers\['x-content-type-options'] = {value: 'nosniff'};
            headers\['x-frame-options'] = {value: 'DENY'};
            headers\['x-xss-protection'] = {value: '1; mode=block'};
            headers\['referrer-policy'] = {value: 'same-origin'};
            return response;
        }

Trying to deploy it fails with a not-useful error:
Resource handler returned message: "null" (RequestToken: 278d458f-5016-4b68-f156-2eee41d00f94, HandlerErrorCode: InternalFailure)

Has anyone any idea on the issue or how to fix it?

Thanks a lot,
Fale

Edited by: Fale on Jun 4, 2021 2:40 AM

Fale
질문됨 3년 전676회 조회
2개 답변
2

Just got of the lie with AWS support

if you specify the function code you also need to specify the function config

[pre]
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFront Test Function
Resources:
ResponseFunction:
Type: 'AWS::CloudFront::Function'
Properties:
Name: !Sub '${AWS::StackName}-response'
AutoPublish: true
FunctionConfig:
Comment: !Sub '${AWS::StackName} Security Headers'
Runtime: cloudfront-js-1.0
FunctionCode: |
function handler(event) {
var response = event.response;
var headers = response.headers;
headers['strict-transport-security'] = {value: 'max-age=63072000; includeSubDomains; preload'};
headers['content-security-policy'] = {value: "default-src 'none';"};
headers['x-content-type-options'] = {value: 'nosniff'};
headers['x-frame-options'] = {value: 'DENY'};
headers['x-xss-protection'] = {value: '1; mode=block'};
headers['referrer-policy'] = {value: 'same-origin'};
return response;
}
[/pre]

The error message is less than helpful!

Edited by: ryan-bennett on Jun 23, 2021 6:26 PM

답변함 3년 전
0

Thanks a lot, it worked!

Fale
답변함 3년 전

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

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

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

관련 콘텐츠