Deploy an Angular application on AWS using serverless

栏目: IT技术 · 发布时间: 4年前

内容简介:A quick starter for a production ready Serverless Angular application on Amazon Web ServicesServerless is aCloud providers like AWS, Azure, or Google Cloud are responsible for executing the code by

A quick starter for a production ready Serverless Angular application on Amazon Web Services

Apr 15 ·4min read

Deploy an Angular application on AWS using serverless

What is serverless

Serverless is a cloud based architecture that allows users to write and deploy code without worrying about the underlying infrastructure .

Cloud providers like AWS, Azure, or Google Cloud are responsible for executing the code by dynamically allocating the resources , this paradigm enables you to shift more of your operational responsibilities to the providers, letting you focus on the business logic.

The serverless approach is typically used for backend applications but we can take advantages of this architecture also for quickly deploy frontend applications .

The Serverless Framework

The Serverless Framework helps you develop and deploy your cloud resources, along with the infrastructure resources they require . It’s a CLI that offers structure, automation and best practices out-of-the-box.

Deploy an Angular application on AWS using serverless

Serverless works with several cloud provider but in this tutorial we will focus on Amazon Web Services .

Reference repository

A working example of the angular base project that we will use as example is available at: https://github.com/mzuccaroli/angular-serverless-starter :, it is a simple Angular project generated with angular CLI with the command “ng new”. For more information see the quickstart of an angular2 project.

You can use this project as a quick starter for your project.

The original example

This tutorial is based on “ aws node single page app via cloudfront ” serverless example, we will adapt the serverless base example to an angular application. You can clone it for code references and custom adaptations.

Prerequisites

You will need Serverless installed globally your system:

$ npm install -g serverless

For more info see: https://serverless.com/framework/docs/getting-started/

You also need an AWS account, check the following link if you don’t have one: How to create an AWS account . Amazon offer a one year free tier that you can use and take great advantages of.

If you already have an AWS account be sure to have the right permissions for cloudfront, cloudformation and s3.

Build your application

Getting started

Create a base angular project with angular CLI if you don’t already have one by typing:

$ ng new

Follow the steps to scaffold your project with last versions on angular libs.

Once the Angular project is set up you can customize it to handle the serverless deploy.

Add the single page app plugin

go to the base example repository and start copying some useful files:

we will need the full serverless-single-page-app-plugin folder, copy it into your project root, this plugin will allow you to simplify the deploy experience. It’s not necessary to understand the plugin to deploy your Single Page Application.

Make available this plugin to your project by adding this to your package.json in the requirements section:

Install the serverless-dotenv-plugin

This will help you to handle the various environments and stages on your project

$ npm install serverless-dotenv-plugin

Add the serverless.yml file

This file is the serverless deployment core, you can copy it from the main example and edit it or use the custom one in the reference repository

the main customizations are in the plugins custom and provider sections:

you will need to customize the s3Bucket value with a custom unique name, then use the angular dist folder as serverless dist folder and finally use the serverless-dotenv-plugin to be able to deploy on multiple stages.

Customize your gitignore

Be sure to add the .serverless folder to your .gitignore file by adding:

Build and deploy

Build

Be sure that your environment is set then perform the regular Angular build process:

$ ng build

The build artifacts will be stored in the dist/ directory. Use the — prod flag for a production build.

First deploy

Warning: Whenever you making changes to CloudFront resource in serverless.yml the deployment might take a while e.g 20 minutes.

In order to deploy the Application you need to setup the infrastructure first by running

$ serverless deploy

The expected result should be similar to:

After this step your S3 bucket and CloudFront distribution is setup. Now you need to upload your static files, aka your dist folder, to S3. You can do this by running

$ serverless syncToS3

The expected result should be similar to

Now you just need to figure out the deployed URL. You can use the AWS Console UI or run

$ sls domainInfo

The expected result should be similar to

Serverless: Web App Domain: dyj5gf0txxyyzz.cloudfront.net

Visit the printed domain domain to see your application.

It should automatically redirect you to HTTPS.

Re-deploying

If you make changes to your Single Page Application you don’t need to re-deploy the full stack, you only need to upload the new files to s3 and invalidate CloudFront’s cache to make sure new files are served.

Run:

$ serverless syncToS3

To sync your files and then:

$ serverless invalidateCloudFrontCache

以上所述就是小编给大家介绍的《Deploy an Angular application on AWS using serverless》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法交易与套利交易

算法交易与套利交易

赵胜民 / 厦门大学出版社 / 2010-9 / 35.00元

《算法交易与套利交易》主要介绍算法交易和一些套利交易的策略,以便于读者对相关方面的内容进行阅读和学习。在《算法交易与套利交易》的第一部分,我们回顾了投资学一些相关的基本内容。其中,前两章介绍了证券投资的收益和风险等特征,以及马可维茨的最优资产配置模型。第3章则介绍了股票投资分析当中常用的资本资产定价模型(CAPM)、套利定价模型(APT),以及因素模型。然后,第4、5章分别讲到了金融证券估值模型、......一起来看看 《算法交易与套利交易》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具