0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Wordpress環境を進化させたい(IaC編)

Last updated at Posted at 2025-02-15

はじめに

前回構築したWordpress環境を再現するRoute53とCloudFrontのCFnテンプレートを書いてみました。
Wordpress環境を進化させたい(構成編)
Wordpress環境を進化させたい(構築編)
ALBまでのリソースは過去の記事で書いているので参照してみてください。

構成図

構成図.png

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

スクリーンショット 2025-02-15 102652.png

CloudFront

  • オリジン
    スクリーンショット 2025-02-15 093953.png
  • ビヘイビア
    スクリーンショット 2025-02-15 094021.png
  • キャッシュ
    スクリーンショット 2025-02-15 094034.png

最後に

コンソールをぽちぽちしなくていいので便利です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?