CataSolv mobile menu

Step-by-Step Guide: Hosting Your Angular Application on AWS S3

CataSolv logo by CataSolv
May 17, 2024

In this step-by-step guide, we’ll delve into the process of installing our Angular application on AWS S3. Hosting your Angular application on an AWS S3 bucket provides an efficient and cost-effective solution for delivering static web content to users worldwide. Follow along to learn how to host your Angular application on AWS S3.

Step 1: Build Your Angular Application

Firstly, make sure you have Angular CLI installed.

If not, you can install it globally by running the following command:

npm install -g @angular/cli

Next, navigate to the root directory of your Angular application in your terminal. Then, execute the command provided below to build the production-ready version of your application:

ng build — prod

Step 2: Set up AWS Account

If you haven’t already, sign up for an AWS account at https://aws.amazon.com/. Once you’ve created your account, log in to the AWS Management Console.

Step 3: Create an S3 Bucket

  • Access the AWS Management Console and go to the S3 service.
  • Click on the “Create bucket” button.
  • Select a unique bucket name and choose the region for your bucket.
  • Keep the other settings as default and proceed by clicking “Create bucket”.
  • Select a distinctive name, and region, and configure the settings according to your preferences.

Step 4: Configure Bucket Properties

  • Locate the recently created bucket in the list.
  • Go to the “Properties” tab.
  • Activate static website hosting by selecting “Static website hosting”.
  • Opt for “Use this bucket to host a website”.
  • Input the index document name (typically “index.html”).
  • Optionally, specify the error document name.
  • Save your changes by clicking “Save”.

Step 5: Upload Your Angular Application

  • Go to the “Overview” tab of your bucket.
  • Click the “Upload” button.
  • Select the files and folders belonging to your Angular application.
  • Proceed by clicking “Upload”.

Step 6: Set Permissions

  • Navigate to the “Permissions” tab within the bucket properties.
  • Click “Edit” located under “Bucket Policy.”
  • Include a bucket policy to allow public readability for your content. Here’s an example policy:
{   "Version": "2012-10-17",
    "Statement": [
      {"Sid": "PublicReadGetObject",
       "Effect": "Allow",
       "Principal": "*",
       "Action": "s3:GetObject",
       "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"}
    ]
}

Step 7: Configure CORS (Cross-Origin Resource Sharing)

  • Proceed to the “Permissions” tab.
  • Select “Cross-origin resource sharing (CORS)”.
  • Add the provided CORS configuration below.
[
 {
 "AllowedHeaders": ["*"],
 "AllowedMethods": ["GET"],
 "AllowedOrigins": ["*"],
 "ExposeHeaders": []
 }
]

Step 8: Configure DNS (Optional)

If you possess a custom domain, access your domain registrar’s settings. Establish a CNAME record directing to your S3 bucket’s website endpoint. This process facilitates the utilization of your custom domain for your Angular application hosted on S3.

Step 9: Test Your Website

Once your files are uploaded and permissions are set, you can access your website using the provided endpoint from the “Static website hosting” section in the bucket properties.

Step 10: Optional — Set up Custom Domain

If you want to use a custom domain for your website:

  • Obtain a domain or utilize an existing one.
  • Access the Route 53 service within the AWS Management Console.
  • Establish a hosted zone for your domain.
  • Update the DNS records to direct to your S3 bucket endpoint.

Conclusion:

Hosting your Angular application on AWS S3 ensures scalable and dependable delivery of static content to visitors. By adhering to these comprehensive instructions, you can effortlessly and affordably make your Angular app accessible worldwide. I hope this guide has provided you with a clear understanding. Stay tuned for more valuable insights.