You are trying to get your new shiny app deployed to AWS either to save cost, try out a new option or any other reason you have in mind. You figure it’s not as straight forward as you think, this tutorial will try and lend a helping hand.
This tutorial assumes knowledge of these services and won’t be explaining some terms.
Create and Configure S3
-
Navigate to your AWS console, and create an S3 bucket.
The guide below will help with that
https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html
It’s important that you uncheck the Block all public access
box while creating your bucket.
Uncheck the Block all public access
option.
- Next, we will configure our bucket for static website hosting.
Configure Bucket.
As shown above;
- Select your created bucket from your list that would be displayed at S3 Home
- Navigate to the properties tab and select the Static website hosting option from the list of options.
- Check the
Use this bucket to host a website
option - Change the
Error Document
toindex.html
(This is because our application is a SPA, and the index page does all the error handling). - Save
We are done with S3.
Create and Configure Cloudfront (Optional)
This step is is optional, but I recommend you do it.
The tutorial below will help with setting up CloudFront and configuring it for our S3 bucket above
https://aws.amazon.com/premiumsupport/knowledge-center/cloudfront-serve-static-website/.
Setup Project Scripts
Create a new deploy.sh
file in your project directory, and copy the contents below into it.
- deploy.sh
- Your
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
can be gotten from the AWS console, the guide below will show you how if you don’t already know or have them AWS Docs. AWS_BUCKET_NAME
is the S3 bucket name we created above e.g mine istest-bucket-damiloju
as shown in picture 2.AWS_CLOUDFRONT
is our CloudFront distribution ID. It can be found here CloudFront
CloudFront distribution ID
- Important that you change the mode option of your project in your
nuxt.config.js
tospa
Make deploy.sh
executable and not check into gitignore.
- Make deploy.sh executable
If you are on Mac or Linux, you can copy the code above, then paste it in your terminal to make deploy.sh
executable.
Next, add gulp to our project and install it globally. Run the commands below.
npm install —save-dev gulp gulp-awspublish gulp-cloudfront-invalidate-aws-publish concurrent-transform
npm install -g gulp
Now create a gulpfile.js
file at the root of your project directory and add the contents from the script below to it.
- gulpfile.js
Now we are done.
All you need to do now is execute your deploy script.
Go to your terminal and type ./deploy.sh
You should get something similar to the pictures below as output.
Terminal Output
Terminal Output 2
We are good to go.