This is an extension to the Angular web application for a Hotel Booking System. This application is built using Angular 4 and would be using AWS serverless computing - AWS Lambda and Dynamo DB. We will host this application as a static website on Amazon S3. Authentication will be supported using AWS Cognito. This application will help you understand the basics of AWS services and building serverless applications on AWS.
1. Angular CLI:
Make sure you have Angular CLI installed
2. Node.js
Install Node.js version 6.x and above. To check the version, use the fol. command in your command prompt.
node -v
3. AWS Account Create an AWS account by signing up on aws.amazon.com
Follow the below steps to run the application:
- Install the dependencies
npm install
- Transpile the code
ng build
- Run the application
ng serve
- Open your browser and hit the URL: http://localhost:4200 You will see the landing page.
- Create an Angular 5 application using the angular-cli.
- Once you have the app created, build the app:
ng build --prod --aot
- This will generate a dist folder with the application bundle. You need to upload the contents of this folder to an s3 bucket.
- Assuming you already have an AWS Account, login to the console.
- Go to S3, under services, select a region.
- Create a new bucket. The name of this bucket must be globally unique.
- Now install the aws CLI if you not already have it installed.
- Run aws configure to configure the credentials of the user.
- Go to the folder of your application, and hit the command:
aws s3 cp ./dist s3://<BUCKET_NAME> --recursive --region <YOUR_REGION_FROM_STEP_5>
Make sure you put the bucket name you created in step 6. 10. All buckets are private by default. Since this is a web application, you need to make it accessible to the public. In the S3 Console, click on the bucket you created. Go to "Public Access Settings". Uncheck all the settings to enable public access. 11. Now add a bucket policy to make any object added to that bucket public. Click on the "Permissions" tab. Click on "Bucket Policy". Add the following in the editor. Replace the correct bucket name.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
}
]
}
Click "Save". 12. To serve the content via index.html, you need to enable "Static Website Hosting" for your s3 Bucket. Click on the "Properties" Tab. Select Static Website Hosting. In the index document field, enter index.html. Note the website URL, leave the other fields blank and click "Save". 13. Paste the URL noted in step 12. in your browser. You should be able to see the landing page of your website. Boom!! You've successfully launched your Angular App on S3. :)
- Go to Cognito from services in AWS Console.
- Click "Create User Pool", give a name and hit Create.
- Now, Create an App Client, Click Add App Client, give a name. Uncheck "generate client secret". Hit Create App Client. Note the App Client ID.
- You can write code in your application for connecting with AWS Cognito. Use the library amazon-cognito-identity-js.
- Enter the Pool ID and App Client ID in the Authorization service (services/authorization.service) of this repository.
- Build and deploy
- Users will now be able to login and logout from your App via Cognito. When the users register through your app, a verification code will be sent to the email address provided. When the users enter the correct code, they will be successfully registered via Cognito.
- Also make sure that the password contains at least one upper-case letter, a number, and a special character.
Yayy!!
For more details on Cognito, refer: AWS Cognito Docs
- For this application we use static data stored in DynamoDB.
- Go to Dynamo DB from services in the AWS console. Create a table for hotel data. Name the table "hotels" and enter "id" as the Partition Key. Create another table for storing the booking details. Name the table "bookings" and enter "id" as the Partition Key. Note the ARN of both the tables.
- For your AWS Lambda to access the DynamoDB tables, you need to grant access using an IAM Role. Let's set that up!
- Go to IAM from Services. Choose Roles from the Left pane. Select Create Role. Select Lambda as the service. Select AWSLambdaBasicExecutionRole as the policy. Hit Review and then Create.
- Click on the role you just created. Click "Add Inline Policy". Click "Choose a Service" and type DynamoDB. Now, add permissions to be performed on DynamoDB. Add PuItem, BatchGetItem, GetItem and Query permissions. Under Resources, select specific and add the ARNs of the tables you created. We will be adding data to the "hotels" table from the console.
- Hit Review Policy. Enter a name for the Policy and click "Create Policy".