LoginSignup
1
0

More than 3 years have passed since last update.

静的ページを作成するためのCloudFormationテンプレート(カスタムドメイン無し)

Posted at

S3をオリジンにして静的ページを作成したい場合のCloudFormationテンプレート。

カスタムドメインは設定しないのでxxxxx.cloudfront.netのようなURLのアクセスのみ可能。
本番利用は想定していないので一時的に動作確認したいときとかに使う用です。

S3のバケットポリシーはCloudFront経由のアクセスを許可するための設定になります。

S3バケット名とCloudFrontのTTLは必要に応じて変更したいためパラメータ化しています。
AWS::WAF::IPSetに関しては許可したいIPが都度異なるためベタ書き。

注意点

こちらのサイトに書かれていますが、作成後は気長に1時間くらい待たないといけないデス:innocent:

テンプレート

AWSTemplateFormatVersion: 2010-09-09
Description: Web Site Hosting Stack

Parameters:
  BucketName:
    Type: String
  CloudFrontTTL:
    Type: Number
    Default: 0

Resources:
  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: Private
      BucketName: !Ref BucketName
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: error.html

  S3BucketPolicy:
    Type: AWS::S3::BucketPolicy
    DependsOn:
      - S3Bucket
      - CloudFrontOriginAccessIdentity
    Properties:
      Bucket: !Ref S3Bucket
      PolicyDocument:
        Version: 2008-10-17
        Statement:
          - Action:
              - s3:GetObject
            Effect: Allow
            Resource: !Join ["/", [!GetAtt S3Bucket.Arn, "*"]]
            Principal:
              AWS: !Sub "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOriginAccessIdentity}"

  CloudFrontOriginAccessIdentity:
    Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: !Sub "${BucketName} access identity"

  CloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    DependsOn:
      - S3Bucket
      - CloudFrontOriginAccessIdentity
      - WebACL
    Properties:
      DistributionConfig:
        Enabled: true
        DefaultCacheBehavior:
          AllowedMethods:
            - HEAD
            - GET
          CachedMethods:
            - HEAD
            - GET
          DefaultTTL: !Ref CloudFrontTTL
          MaxTTL: !Ref CloudFrontTTL
          MinTTL: !Ref CloudFrontTTL
          TargetOriginId: !Sub "${BucketName}-Origin"
          ViewerProtocolPolicy: redirect-to-https
          ForwardedValues:
            QueryString: false
        IPV6Enabled: true
        HttpVersion: http2
        DefaultRootObject: index.html
        WebACLId: !Ref WebACL
        Origins:
          - Id: !Sub "${BucketName}-Origin"
            DomainName: !Sub "${BucketName}.s3.amazonaws.com"
            S3OriginConfig:
              OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}"

  WebACL:
    Type: AWS::WAF::WebACL
    DependsOn:
      - IPSetRule
    Properties:
      DefaultAction:
        Type: BLOCK
      MetricName: WebACLMetric
      Name: !Sub "${BucketName} WebACL"
      Rules:
        - Action:
            Type: ALLOW
          Priority: 0
          RuleId: !Ref IPSetRule

  IPSetRule:
    Type: AWS::WAF::Rule
    Properties:
      Name: !Sub "${BucketName} IPSetRule"
      MetricName: IPSetRuleMetric
      Predicates:
        - DataId: !Ref IPSet
          Negated: false
          Type: IPMatch

  IPSet:
    Type: AWS::WAF::IPSet
    Properties:
      IPSetDescriptors:
        - Type: IPV4
          # ここは書き換える
          Value: aaa.bbb.ccc.ddd/32
      Name: !Sub "${BucketName} IPSet"
1
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
1
0