Skip to content

adding s3 website endpoint as alias

0

cannot add s3 website endpoint as alias in hosted record creation. not getting the name of s3 website endpoint in pull down menu. tried diffrent regions. all direct links of s3 website endpoints works without any issue. there are two buckets in canada region, but nothing is listed in pulldown list. tried manually adding. added with an error. and nothing works

Enter image description here

Enter image description here

result in browser after manually adding the link

asked 3 years ago1K views
2 Answers
2
Accepted Answer

It is hard to tell since some of the key information is obfuscated, but I would say you can start by checking the name of the bucket in S3 matches the hostname in Route 53. If your site name is "my.example.com" then S3 required that the content be stored in a bucket called "my.example.com". It cannot be a bucket called "my" or "example.com" or any other name. It has to exactly match the hostname, and it must be lower case.

Also to support requests from both the root domain and subdomain, you must create two buckets. For example "my.example.com" and "www.my.example.com". You can view instructions on how to setup here.

AWS
answered 3 years ago
AWS
EXPERT
reviewed 3 years ago
0

The documentation above explains well if you do all changes from AWS Console; I think the question is how can you do it in CloudFormation. As you need a AWS Route53 RecordSet, A Record using Alias and create a CNAME record for the www and points to the Root A Record

See below an example of a CloudFormation Stack which creates the following resources

  1. S3 Bucket for a website with a domain yourdomain.com
  2. Create bucket policy for public access (this is desirable but it will flagged if you have a security scanner),
  3. Create an alias A record to point to the S3 website endpoint
  4. Create a CNAME record to point to the root A record Note: Assumes you have an already Route53 Hosted Zone with the same domain yourdomain.com and the bucket and Route53 Zone are on the same AWS Account
AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation template to create an S3 bucket for static website hosting with IAM roles and lifecycle rules'
Parameters:
  DomainName:
    Type: String
    Description: 'The domain name for the website'
    Default: 'yourdomain.com'
  HostedZoneId:
    Type: String
    Description: 'Hosted Zone ID for the domain'
    Default: 'Z1234567ADFFBB' # Use your own Route53 Hosted Zone Id

Resources:
*** Create any another resources ***

  WebsiteBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref DomainName
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        IgnorePublicAcls: true
        BlockPublicPolicy: false
        RestrictPublicBuckets: false
      LifecycleConfiguration:
        Rules:
          - Id: DeleteOldVersions
            Status: Enabled
            NoncurrentVersionExpirationInDays: 30
          - Id: DeleteOldLogs
            Status: Enabled
            ExpirationInDays: 90
            Prefix: logs/
      VersioningConfiguration:
        Status: Enabled
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: error.html

  WebsiteBucketPolicy:
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket: !Ref WebsiteBucket
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: PublicReadGetObject
            Effect: Allow
            Principal: '*'
            Action:
              - 's3:GetObject'
            Resource:
              - !Sub 'arn:aws:s3:::${DomainName}/*'

  WebsiteRootRecord:
    Type: 'AWS::Route53::RecordSet'
    Properties:
      HostedZoneId: !Ref HostedZoneId
      Name: !Ref DomainName
      Type: A
      AliasTarget:
        # DNSName: !GetAtt WebsiteBucket.WebsiteURL
        DNSName: !Sub 's3-website-${AWS::Region}.amazonaws.com'
        EvaluateTargetHealth: true
        HostedZoneId: Z3AQBSTGFYJSTF # Hosted Zone ID for S3 website endpoints check this to 
 https://docs.aws.amazon.com/general/latest/gr/s3.html#s3_website_region_endpoints

  WebsiteWWWRecord:
    Type: 'AWS::Route53::RecordSet'
    Properties:
      HostedZoneId: !Ref HostedZoneId
      Name: !Sub 'www.${DomainName}.'
      Type: CNAME
      TTL: 300
      ResourceRecords:
        - !Ref DomainName

Outputs:
  WebsiteBucketArn:
    Value: !GetAtt WebsiteBucket.Arn
    Description: ARN of S3 bucket for website hosting
  WebsiteURL:
    Value: !GetAtt WebsiteBucket.WebsiteURL
    Description: URL for website hosted on S3
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.