aws-cloudformation - 如何在模板文件中设置 CloudFront 函数的 FunctionCode 属性?

标签 aws-cloudformation amazon-cloudfront

我正在尝试使用云形成(YAML 模板文件)在我的堆栈中部署 CloudFront 函数。如何为我的 CloudFront Function 指定 FunctionCode ( AWS docs ) 属性?

AddHTMLPostfixFunction:
    Type: AWS::CloudFront::Function
    Properties:
      Name: !Sub "${ApplicationName}-add-html-postfix"
      AutoPublish: true
      FunctionCode: "---> Path to my .js-file? <---"
      FunctionConfig:
        Comment: "Adds .html postfix to viewer requests"
        Runtime: cloudfront-js-1.0

除了CLI commands that has been documented from AWS之外,我找不到任何这样的例子.

最佳答案

可以使用内联代码!

AddHTMLPostfixFunction:
    Type: AWS::CloudFront::Function
    Properties:
      Name: !Sub "${ApplicationName}-add-html-postfix"
      AutoPublish: true
      FunctionCode: |
        function handler(event) {
          var hasExtension = /(.+)\.[a-zA-Z0-9]{2,5}$/
          var request = event.request
          var uri = request.uri

          // If the request does not have any extension, add '.html'
          if (uri && !uri.match(hasExtension)) {
            request.uri = `${uri}.html`
          }

          return request
        }
      FunctionConfig:
        Comment: "Adds .html postfix to viewer requests"
        Runtime: cloudfront-js-1.0

关于aws-cloudformation - 如何在模板文件中设置 CloudFront 函数的 FunctionCode 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67608477/

相关文章:

amazon-web-services - AWS Cloudfront 重定向 TOO_MANY 次

amazon-web-services - 将 CloudFront 与外部托管的 DNS 结合使用,指向 EC2 实例

amazon-web-services - S3 和 CloudFront 网站托管用于多个 SPA 根据 url 路径重定向到不同的 index.html

amazon-web-services - Terraform - 模板插值无效

amazon-web-services - CloudFormation 可选参数

aws-lambda - 为什么sam包每次都会上传工件?

angularjs - 通过 AWS CloudFormation 配置资源以部署 React(MERN)/Angular(MEAN) 应用程序

amazon-web-services - 无法在同一 cloudformation 堆栈中创建带有死信队列的 FIFO 队列

caching - Amazon Cloudfront Cache-Control:24小时后,无缓存 header 无效

apache - 允许 Amazon CDN 绕过 HTTP 基本身份验证