How to deploy SPA as a static website using AWS S3
Tags: aws, react, s3, spa
Published: 2021-02-05 21:16:05

Amazon web services allow you to store different files, using amazon S3 buckets. Everyone can store images or libraries, use it like CDN or similar to it. Also it allows us to deploy frontend web application, connect any domain you want (and you have bought previously).
At first, let's create our frontend application. I will use Create React App template, but you can use the same way Angular, Vue or just static html file. It depends on you.
![image](https://dev-to-uploads.s3.amazonaws.com/i/jypbiv7s9182po97c0vp.png)
   
As you have created and developed your app, you have to create a minified bundle.In Create React App for its deploying you have to run command npm run build, after folder build will be created, where minified bundle is located (I believe so).

![image](https://dev-to-uploads.s3.amazonaws.com/i/0mqgpttccwih7j4lmw2s.png)

AWS S3
_______
![image](https://dev-to-uploads.s3.amazonaws.com/i/3nlgna095jonzanmge7w.png)
 
Let's create s3 bucket, that supposes to be our static website's hosting. For this purposes we have to create S3 bucket with a public access.
![image](https://dev-to-uploads.s3.amazonaws.com/i/pnjnt4tgc5nab853qhnt.png)

After the bucket was created we should go into that bucket.
![image](https://dev-to-uploads.s3.amazonaws.com/i/rzlknikxfmyhxog4kdd4.png)
 
 In "Properties" tab at the same bottom of the page you can find "Static website hosting" block. 
![image](https://dev-to-uploads.s3.amazonaws.com/i/d2wfowd2a8j3e1b54m78.png)
  
You have to enable it and specify default index file. If you use frontend routing system without hash separating, you should specify index file like an error file.

![image](https://dev-to-uploads.s3.amazonaws.com/i/0qnn7ae5romj3xv95vbx.png)

Please, don't forget to upload your files into the bucket.
![image](https://dev-to-uploads.s3.amazonaws.com/i/8gzrx28ts4vmi0q97ac4.png)


The last step of our deployment will be specifying bucket's policy in the "Permissions" tab. 
![image](https://dev-to-uploads.s3.amazonaws.com/i/wp5of79gh53r1m4er4nl.png)
  
You have to edit bucket's policy in this way, don't forget to save the policy: 

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your-backet-name/*"
        }
    ]
}
```

Finally, in the "Properties" tab , where we previoulsy enabled static website hosting we can find the url, that allows to visit our frontend web application: 

![image](https://dev-to-uploads.s3.amazonaws.com/i/5apwshkpy0bgz4ilrgp3.png)


Let's check the link:

![image](https://dev-to-uploads.s3.amazonaws.com/i/c5nd51h350ejcjj6rb7b.png)

We deployed our frontend!

P.S. In next articles I assume to create backend microservices using AWS Lambda for our frontend application. And for now, you can find pretty good manual how to deploy frontend application in Azure   https://dev.to/magervalentine/deploy-spa-with-azure-blob-storage-in-minutes-7c9 


_____________________________________________________

Image wa taken from aws.amazon.com