Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sampathshivakumar/b7143cec8e257b10bd3db22dc0b97c94 to your computer and use it in GitHub Desktop.
Save sampathshivakumar/b7143cec8e257b10bd3db22dc0b97c94 to your computer and use it in GitHub Desktop.

Deploying a Static Website to AWS S3 with HTTPS using CloudFront.

static website

Prerequisites.

  • You should have a domain name already purchased to link with your Static Website.
  • Some Web content to display on your domain.

By the end of this post you will be able to:

  • Create an S3 bucket and Configure it for static website hosting.
  • Create a record in Route 53.
  • Create a CloudFront distribution and link it with your custom domain.
  • Create Certificates in AWS Certificate Manager.
  • Then Finally Link the CloudFront CDN, S3, Custom Domain and SSL Certificate via Route 53 To securly access your webpage.

I have created a simple index.html file.

1 2

index.html

<html>
<body>

<h1> "Be A Better Version of Yourself".</h1>

<img src="image/image.jpg" alt="Be-a-Better-Version-of-Yourself">

</body>
</html>

Your webpage will look like this.

3

Open Amazon S3 in your aws console and click on Create bucket.

4

Name your bucket it should be unique, so i am giving my domain name to s3 bucket.

5

In Block Public Access settings for this bucket, uncheck "Block all public access" and select the acknowledge at the end.

6

Rest of all settings are fine, click on create bucket at bottom.

7

Once done you should be able to see your bucket.

image

Now go inside the s3 bucket and upload the web content you have.

9

10

Select upload once your done. 11

Once files are uploaded you can see the status "Succeeded". 12

Now go to "Properties" tab and enable "Static website hosting" enter index.html in index document section and save changes.

13

14

15

16

Now go to "Permissions" tab and enter bucket policy and save chnages.

17

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::learnwithsampath.world/*"
            ]
        }
    ]
} 

18

This policy will grants public read access to all objects within the "learnwithsampath.world" S3 bucket.

Now you can see bucket access as "public" previously it was "object can be public".

19

Now open index.html in s3 bucket.

image

You can see your website, but it is not your custome domain url.

21

Now we have to redirect the click to learnwithsampath.world to this s3 bucket.

I have already purchased a domain name "learnwithsampath.world" with godaddy Internet domain registrar.

22

Now go to Route 53 Dashboard and click on "create hosted zone".

23

Enter your Domain name, type "public hosted zone" is fine click "create hosted zone"

24

Once your done, you should see one hosted zone created in your Route53 console.

25

26

To manage godaddy domain name using AWS Route-53 we have to copy nameservers created in Route53 to godaddy.

27

28

29

30

31

32

Now you can manage your domain from Route-53 by creating records in it.

Now lets create a simple record in Route-53.

33

Select "Alias", Route traffic to "S3 website endpoint" and select location of your s3 bucket.

34

Now you can access your website on your domain learnwithsampath.world

34

But it's still "Not secure" its http://learnwithsampath.world/ not https://learnwithsampath.world/

Now lets make it using AWS Certificate Manager, go to AWS Certificate Manager console.

Note: You have to select the AWS North Virginia region(US-East-1). As CloudFront recognizes only this region as it's ACM certificates.

35

Select Certificate type "Request a public certificate" and click on next.

36

Provide your Domain names, select a Validation method rest all are fine click on request.

37

38

Once you click request, you need to add the given record in Route 53 by clicking on Create records in Route 53.

39

40

It will take some time for records to be updated for me it took few seconds.

41

42

After the certificate is issued, we can set up a CloudFront distribution. Open Amazon CloudFront in aws console and click on "Create a CloudFront distribution"

43

Select Origin domain from drop down.

44

45

46

In Viewer protocol policy select to Redirect HTTP to HTTPS

47

Add Custom SSL certificate.

48

Select Web Application Firewall (WAF). You can select as per your requirment.

50

Mention Default root object and Click on Create distribution .

49

51

**It will take few minutes.

you can check with Distribution domain name.

52

53

Now we can point our domain to Distribution domain name in Route 53.

Previously we have created A-record in Route 53 pointing to "S3 website endpoint" you delete/edit that A-record to point to Distribution domain name and save record.

55

Wait for some time for changes to apply

Now you can access the website securely over "https".

56

Even you time http://learnwithsampath.world/ also it will redirect to https://learnwithsampath.world/

We have successfully Deployed a Static Website to AWS S3 with HTTPS using CloudFront.

Thank you for reading this post! I hope you found it helpful. If you have any feedback or questions,Please connect with me on LinkedIn at https://www.linkedin.com/in/sampathsivakumar-boddeti-1666b810b/. Your feedback is valuable to me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment