4

I'm trying to configure this website routing rule for my static s3 bucket website using serverless yaml which uses cloudformation.

<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals/>
      <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
      <Protocol>https</Protocol>
      <HostName>foo.amazonaws.com</HostName>
      <ReplaceKeyPrefixWith>prod/photos/resize?key=</ReplaceKeyPrefixWith>
      <HttpRedirectCode>307</HttpRedirectCode>
    </Redirect>
  </RoutingRule>
</RoutingRules>

How do I translate that to my yaml below?

resources:
  Resources:
    UploadBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${file(./serverless.env.yml):${opt:stage}.BucketName}
        AccessControl: PublicRead
        WebsiteConfiguration:
          IndexDocument: index.html
          RoutingRule: //What's the format that goes here?
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

2 Answers2

4

RoutingRules is a list of Amazon S3 Website Configuration Routing Rules Property.

E.g.

RoutingRules:
  - RedirectRule:
      HostName: foo.amazonaws.com
      HttpRedirectCode: "307"
      Protocol: https
      ReplaceKeyPrefixWith: prod/photos/resize?key=
    RoutingRuleCondition:
      HttpErrorCodeReturnedEquals: "404"
spg
  • 9,309
  • 4
  • 36
  • 41
  • should values like HostName be in "quotes", e.g. "foo.amazonaws.com" rather than in just plain text? – MonkeyBonkey Mar 22 '17 at 22:11
  • You are not required. This answer explains it better:: http://stackoverflow.com/questions/19109912/do-i-need-quotes-for-strings-in-yaml – spg Mar 22 '17 at 23:37
2

If anyone interested, here is the JSON representation of the same settings

"WebsiteConfiguration" : {
 "IndexDocument": "index.html",
 "ErrorDocument": "error.html",
 "RoutingRules": [{
   "RedirectRule": {
      "HostName": {
        "foo.amazonaws.com"
      },
      "HttpRedirectCode": "307",
      "ReplaceKeyPrefixWith": "prod/photos/resize?key="
    },
    "RoutingRuleCondition": {
      "HttpErrorCodeReturnedEquals": "404"
    }
  }]
}
kimerseen
  • 2,511
  • 2
  • 16
  • 5