Skip to content

Commit

Permalink
Feature/s3 alias multiple region (#28)
Browse files Browse the repository at this point in the history
* feat: support multiple s3 region in which the stack is deployed in

* chore: update website alias test spec
  • Loading branch information
raykrishardi authored May 16, 2024
1 parent aaacf18 commit 1e0362b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
12 changes: 2 additions & 10 deletions s3.cfndsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,14 @@
dns_format = website['alias']['dns_format']
subdomain = website['alias']['subdomain']
fqdn = subdomain + "." + dns_format + "."
region = website['alias']['region']

# Reference: https://docs.aws.amazon.com/general/latest/gr/s3.html#s3_website_region_endpoints
s3RegionMapping = {
"ap-southeast-2" => {
"hosted_zone_id" => "Z1WCIGYICN2BYD"
}
}

Route53_RecordSet("#{safe_bucket_name}S3AliasRecord") do
HostedZoneName FnSub("#{dns_format}.")
Name FnSub(fqdn)
Type 'A'
AliasTarget ({
DNSName: "s3-website-#{region}.amazonaws.com.",
HostedZoneId: s3RegionMapping[region]['hosted_zone_id']
DNSName: FnSub("s3-website-${AWS::Region}.amazonaws.com."),
HostedZoneId: FnFindInMap('S3AliasHostedZone', Ref('AWS::Region'), 'HostedZoneId')
})
end
end
Expand Down
6 changes: 6 additions & 0 deletions s3.mappings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Reference: https://docs.aws.amazon.com/general/latest/gr/s3.html#s3_website_region_endpoints
S3AliasHostedZone:
ap-southeast-2:
HostedZoneId: Z1WCIGYICN2BYD
us-west-2:
HostedZoneId: Z3BJ6K6RIION7M
2 changes: 1 addition & 1 deletion spec/website_alias_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
end

it "to have property AliasTarget" do
expect(resource["Properties"]["AliasTarget"]).to eq({"DNSName"=>"s3-website-ap-southeast-2.amazonaws.com.", "HostedZoneId"=>"Z1WCIGYICN2BYD"})
expect(resource["Properties"]["AliasTarget"]).to eq({"DNSName"=>{"Fn::Sub"=>"s3-website-${AWS::Region}.amazonaws.com."}, "HostedZoneId"=>{"Fn::FindInMap"=>["S3AliasHostedZone", {"Ref"=>"AWS::Region"}, "HostedZoneId"]}})
end

end
Expand Down
1 change: 0 additions & 1 deletion tests/website_alias.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ buckets:
alias:
dns_format: example.com
subdomain: mybucket
region: ap-southeast-2
routing_rules:
- redirect_rule:
hostname: "test1"
Expand Down

0 comments on commit 1e0362b

Please sign in to comment.