/

October 27, 2023

Deploy Angular App from GitHub to AWS with CodeBuild & CloudFront

Deploy Angular App from GitHub to AWS with CodeBuild & CloudFront

Introduction

This blog will go into the art of AWS-smooth Angular application deployment. We’ll demonstrate the potential of automation by using AWS CodeBuild to build the app and AWS S3 to host it. Not only that but we’ll up your deployment game by distributing your software over AWS CloudFront. With this configuration, developers may concentrate entirely on code while the infrastructure handles deployment seamlessly. Let’s get started on this quest to make Angular app deployment on AWS easier.

Configuration steps: –

  1. Setup a GitHub repository for your angular application with all the necessary files
jWAyM2op3MAgJqifWcHPhVYeVfQYjpiV8X2rv8aeRi1cTzwpHy7dfMb4P2sG
  1. Now create a S3 bucket on which the artifacts of the code will be stored after being built by the AWS Codebuild and then further get distributed via the Cloudfront URL.
  2. Open Your AWS Management Console after logging into your Amazon Web Services account with your IAM user.
q8w4oJy5rf1bAF8AdWKkrhB0arEKtVosJvcIGpAF dLI0E2R GS5 1mzo9Nkdf62B qcnfengjyAg2tHTI
  1. Go to the search bar and type S3. If you click on it you’ll be redirected to the S3 dashboard.
AkRGGp45cPJy7pg4zJC0UeDdo6m4My94fxADvV0PFvnm2IFDmKIkAfpKHn9ot ZbfUFELS8JL4tB1OF5y4SScH23 auXyID RUaT604psnPB0K0ub swk8EWWicvyV7uNFQ0pDITcJZRz5uR48wBeDK W6vMdv4
  1. Click on Create bucket button. And in the general configuration, give an a unique name to your bucket and select the AWS region for your bucket which in our case will be ap-south-1 (Mumbai).
y223S4vqqHYMmhBSjceCRcNn2EHWL26V26DSQclSF4eI9XIVc0QSsAs413E78vcJxXHPGZPN9RkxQvqsvJjzTuoMAMamifxpG2gzRbNw98En01HQLGd9rqQz2 Lf lGpgA99WBNSzlc6QI68sEf aiRJveWtOuCQ
  1. In the next steps keep the rest options for ACL, Block Public access as default and only enable the Bucket versioning for your bucket.
ejq3DACFAHbZaeRmOzLNW2zjgso iAtT1gZX TIgKmqE1Ki9eN OmZno31f1gUKKkdI9 xJHzvDqYQvvl0EMTucJSCrIh k XvZZw6tAT
  1. Keep the encryption of the object same as the bucket encryption. By default the bucket is encrypted with Amazon SSE-S3 encryption. 
z1q8eGZzQAqdr0tX4jzZi6SsKiAl3oiyFkO5CvBhS5a35uVICuUmXeHndqkR3
  1. Click on the Create bucket icon.
areYNaoYJYopfl1AppRfN X5n8HFHy15gotcL X g1TeomCgUPEQ1POIhGIjB3RneMOtIMDiOIftHZ3lNYSd7S8q8IPnrHktWkEKvFS n3Q6FvppRsfBtWxFlRoCFnhXgppIsUP2VbnC3d6aNW iSbT68pgBwsrL
  1. Once the bucket is created we need to enable the static web hosting. For that go to the properties of your bucket , enable the static website hosting and, add the index.html at the start and error pages.
Khng0SIfQ0RudMxXBaeV6IZvmXxoaV27u rzkBMbh5VjDUKIU DW9rIZkkzWNrf1d 7JkV6WfNoGge0PtSrN3sA49tBo2LGWkeBYVKrJIQpH1BqHMe vsq954SAD89vz0Ai6fFKNswGYBTiqL64oW4U7P1GbFA
  1. Now create a Codebuild project to setup connection between our GitHub repository and Amazon S3 bucket for storing the artifacts.
  2. Go to the search box of your Amazon console, type Codebuild and click on it.
KZulzKAaFdmS1Vd5u9 vK7Yu7iUrqBPxZJzEI5QyYka6I l03LP8MJ9VjyLCVvKvT99IAenGpSyUHkmEgsZ4HSM3BiWdgUwmzp1aAntTAKBu8SQ2a7j9xU6z4sPXQYF6jhi6LE0XySdSrGcGfwCg1aYSrEuM9 d
  1. Once clicked it will redirect you to the Codebuild dashboard. And click on Create build project. 
  1. In the Project Configuration block add a name and description to your project.
rEHMdSU8qD6tdYwsmlLRRnU6zmVJpBGP77PhZBQx UtiEN59o ESQNZsYPiBvbVOcOCEOPLu9RN52 i5iy0GxIpKYiQlksko7NIIISZ0XCLhRDKBcxSPSJH ULO3CTjudWuj4EtwSya12AP7lCJKz YRldYNISw
  1. In the Source block, select provider and authenticate, and then choose your repository in which your angular application and necessary files are present.
qH41rk7Rx3kssln2EG71 uSWg3livUAv545YMF9 aYLXtV0Ki5t1EWMsZOBaEBKZSRkVy1R84m41a
  1. In the Primary source webhook events block, check the Rebuild everytime a code is pushed in to the repository option. Select Build type as Single Build  and Webhook event type as PUSH
uRTUd4JO5 WHW9bHEVGPGy8PmcVvQl2Jwb WeHIvXYs6RcIuEV1e4as0yCuDjmj9etGdC5sDUJusIpsXd6tCUgAFG14Szf5j43qTzdZPINDUzMkduZgyjw05d 2BORdW5HRIceQYBi1LENBvgAdr2YTd rETA5RK
  1. In the Environment block, we will be defining the variables and values that our code will require and the image or OS it will use to build the application. A Codebuild service role will be created at this block in order to configure the IAM access for these operations.
becG3ekBkJ69HHrwOIJbPU1a19f6QcdV68EHfRkJuXS ODSgvf6lziAgvGlHLeRyjimeXw3
  1. Next is the buildspec block. In this block you have to mention a buildspec.yml file that should be created in your root repository.
0Dp6tU 0 r9pkse4Qd2f r4BvGfrkVSwqcUkEOTVv3WzxBTw60wR2uTFoBLVDmsn UQWz8oE 97DVPnKyR7ofjFWwm2WyHiFG3xTSvcblFMyO37bHSbnPUteV2VRinfHoFzrfKG 9qZ2kQmhiDFdIFm 2ue2DzwL
  1. Here is an example of the buildspec.yml file for an angular application 

version: 0.2

phases:

  install:

    runtime-versions:

      nodejs: 16

    commands:

      – echo Installing source NPM dependencies…

      – npm install -g @angular/cli@15.2.5

      – npm install

  build:

    commands:

      – echo Build started for $BUILD_ENV on `date`

      – npm run build

      – echo Build completed…

  post build:

    commands:

      – echo Pre-Build started on `date`

      – aws s3 sync ./dist s3://$BUCKET_NAME

      – echo Pre-Build completed on `date`

artifacts:

  files:

    – dist/**/*

  discard-paths: yes

  1. Now in the Artifacts block, select the location and bucket where you want to store the artifacts once they are built using the Codebuild.
WqieFHbbqDmAa3RYoTWKfRUtFN WXYeRZIqJeKTjYNKha6H4cUe4h7CluPMzRXS2AhUG9hRqpAcoFD0o1 Bf FY60QMfRm2k1AXdp2quKW57Esje
  1. Click on Create build project.
XPr6hWUnwxGA0owB5Kl tvcegW24D5EspW8AdIDnRw7oujmkgcznsfxyJz0j8zGK4GcYzpsAJvVA6G4iFmOM0My6 3PWUnNq7WWcqtDGqpGV a2DDMcXYcZgqxYqDXxSkqa6 5Ek AH VRHlaD27SfMGLG 8WYME
  1. Once the build project is created click on start build and observe the workings of the build process through logs and phase details.
UcgfIiKaVjB LZ8fd3B9OyZhbNElEQYHZXOIc2U67Sbv pv5byPDhoC7vzXZruWp238IyQQX j4bTgaS01RRA7hPIADfVOHVI cH8cRybIilYcn vJD9NB4IMSfTLcmgJxkPPBy2onYKDZ20BPZ7

The above image shows that the build was successful, and now the artifacts are copied to your S3 bucket.

  1. Creating a Cloudfront distribution for my S3 bucket.
  1. Go to the Search box on your AWS management console, type Cloudfront, and click on it it will redirect you to Cloudfront console.
EMMW1RV7QovaXuYtfjkzpzRqw cLWlFFgmpXVD VNdTR WNqEBjrUh8lshU4nyVCBEukvxM 0aTnsWqwN5Ulit0q4DBkgdz5W0z7TJQig6XhJ fW5PzGssrHhKlHfIDp7z1Jva4me6wEkucIpSgJmiU 2o0qWkSg
  1. Now click on Create a Cloudfront distribution.
  1. Now in the Origin block, enter the AWS origin, which in our case will be our S3 bucket
BJq8WXRA1XPpzuiEgKFVnb wk7aWfB5y4OaFTUIA1 kz CbsSpARs17FiYFtk8CcagU7uZAus92HSahzH87LlbxRaWy9jUsk1k7uziURMdXA8681YTjIu9oEgvW nKkDQtSuXOWlCeIGYj5tOj49Q7s3cYGf4zX
  1. Now in the Default cache behavior, select path pattern as default, compress objects as Yes, viewer protocol policy as Redirect Http to Https, allowed head for HTTP as GET HEAD, restrict viewer access as No and Cache policy as CacheOptimized
w KOvGaocSJinEggyvRgPpq5YkwSIP7f8Iltg9J2WAxUpVRmDPp SZdeuHrvk 5eIUb 3B6ullxR SU1pHerCJCAWavO
  1. In the Web application firewall click on do not enable security options for now.
  1. In the Settings block, select the use all edge locations option for price class for best performances, and add CNAME if an alternate domain and SSL certificate are available. HTTP/2 and HTTP/3 can both be used and now click on Create distribution.
  1. It will generate an IAM policy for the bucket which can be copied to the policies.
mnCGht49IA tJLbP8iiH0VovuelFj6U5sXZUoayosFllBKeBOurQRN9HaJMOJ0OOtjfwcUDMkSfP9u K bNAdEiLwedud3j9ZDqF YVjBClIJGLcIKEW5 pNy40rBVmyKEWvkIL9oSBVhN6fVLibmoCtvuoMgedj
  1. Now hit the Cloudfront URL and you’ll be able to see your angular application.
Cj3JddRK6NplxZ4tWeWsG s heeR5tdoCb84cw8wnKRzCsKA4N48uDDGM pjEwgHxsJtxGGN0l K3fKUNME I01nmfNy0tNBNqAE2uBHID0AAYH2u1SvL0Rm2CGxaPeSOSUPyvJwGUGR 899bv05oTduAqhtBdMU