はじめに
前回構築したWordpress環境を再現するRoute53とCloudFrontのCFnテンプレートを書いてみました。
Wordpress環境を進化させたい(構成編)
Wordpress環境を進化させたい(構築編)
ALBまでのリソースは過去の記事で書いているので参照してみてください。
構成図
Route 53 Hosted ZoneとAレコードを作成するテンプレート
AレコードでALBに対してエイリアスを指定しています。
ALBのホストゾーンIDはリージョンによって固定値となっています。
東京リージョン(ap-northeast-1):Z14GRHDCWA56QT
- テンプレート
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFormation template to create a Route 53 Hosted Zone and an A record.
Parameters:
DomainName:
Type: String
Default: "example.com" # Route53に登録済みのドメイン
Description: "The domain name for the hosted zone and record."
Resources:
HostedZone:
Type: AWS::Route53::HostedZone
Properties:
Name: !Ref DomainName
ARecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZone
Name: !Ref DomainName
Type: A
AliasTarget:
HostedZoneId: Z14GRHDCWA56QT # ap-northeast-1のALBホストゾーンID
DNSName: "MyLoadBalancer-xxxxxxxxx.ap-northeast-1.elb.amazonaws.com" # ALBのDNS名
Outputs:
HostedZoneID:
Value: !Ref HostedZone
Description: "The ID of the created Route 53 Hosted Zone."
CloudFormationディストリビューションのテンプレート
もしキャッシュポリシーやオリジンリクエストを変更したい場合のIDは以下を参照してください
AWSTemplateFormatVersion: "2010-09-09"
Resources:
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: example.com # Route53でエイリアスをALBに向けているためドメインを直接指定
Id: example.com
CustomOriginConfig:
HTTPSPort: 443
OriginProtocolPolicy: https-only
Enabled: true
DefaultCacheBehavior:
TargetOriginId: example.com
ViewerProtocolPolicy: redirect-to-https
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- POST
- PATCH
- DELETE
CachedMethods:
- GET
- HEAD
- OPTIONS
CachePolicyId: "4135ea2d-6df8-44a3-9df3-4b5a84be39ad" # CatchingDisabled
OriginRequestPolicyId: "b689b0a8-53d0-40ab-baf2-68738e2966ac" # AllViewerExceptHostHeader
ForwardedValues:
QueryString: false
Cookies:
Forward: none
PriceClass: PriceClass_All
Restrictions:
GeoRestriction:
RestrictionType: none
IPV6Enabled: false
Outputs:
CloudFrontURL:
Description: "CloudFront URL"
Value: !Sub "https://${CloudFrontDistribution.DomainName}"
CloudFrontに独自ドメインでアクセスしたいとき
以下をPrice Classの下に書き加えてください
Aliases:
- "example.com"
ViewerCertificate:
AcmCertificateArn: "arn:aws:acm:us-east-1:xxxxxxxxxxxxxxxxxxxxx" #us-east-1のACM証明書
SslSupportMethod: "sni-only"
MinimumProtocolVersion: "TLSv1.2_2021"
結果
Route53
CloudFront
最後に
コンソールをぽちぽちしなくていいので便利です。