Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 7 fe #331

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bd6f89a
S3 and Cloudfront task2
Jul 5, 2022
bf50ed0
Updated the url, along woth "Note"
Jul 5, 2022
747d18c
Formatted readme
Jul 5, 2022
2d291e9
Updated the links
Jul 7, 2022
3de919c
Updated the <links>
Jul 7, 2022
d018b72
Updated Readme.md with correct url
Jul 11, 2022
4c5a10b
Added serveless-single-page-app-plugin, invalidate cache is working.
Jul 13, 2022
de89c28
Updated README file
Jul 13, 2022
c790f6c
package-lock.json
Jul 14, 2022
ef8a0ab
Merge pull request #2 from Ajay-Nallanagula/task2
Ajay-Nallanagula Jul 14, 2022
b18e375
Added Two end points
Jul 15, 2022
2ab1f08
Added Local function testing,
Jul 15, 2022
ba3cb16
Added serverless.yml back
Jul 15, 2022
57ea619
Added few more changes Related to productHandler
Jul 15, 2022
3e7a9d4
Checkin constants file
Jul 15, 2022
bdf6db8
Updated Readme.md
Jul 15, 2022
f55c401
Changed the names for product to test
Jul 18, 2022
7f93d1b
Integrated Swagger related changes
Jul 18, 2022
9a7de54
Updated Swagger Urls
Jul 19, 2022
6768f7e
lambda getProducts,getProductsById integrated to aws postgres rds
Jul 21, 2022
e2b11cb
Create Product Lambda/API endpoint created
Jul 21, 2022
936f121
Added Invalid Data Http400
Jul 21, 2022
d556d9f
Added Transactions to include stocks been added
Jul 25, 2022
f4dae60
Fixed the swagger issue w.r.t POST method
Jul 26, 2022
50effb2
Added few commands w.r.t serverless functionality
Jul 26, 2022
4dead6e
Comitting package-locks, gitignore,npmignore
Jul 26, 2022
8f4832b
Code validations, Integrate with Frontend
Jul 27, 2022
6306d82
Frontend Integration Task5 Imports
Jul 29, 2022
85b3218
Updated the comment
Jul 29, 2022
45aeb42
Deleted Product-Service, created new backend branch
Jul 29, 2022
a1f2df8
Added Frontend changes
Aug 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions .serverless_plugins/serverless-single-page-app-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
'use strict';

const spawnSync = require('child_process').spawnSync;

class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.commands = {
syncToS3: {
usage: 'Deploys the `app` directory to your bucket',
lifecycleEvents: [
'sync',
],
},
domainInfo: {
usage: 'Fetches and prints out the deployed CloudFront domain names',
lifecycleEvents: [
'domainInfo',
],
},
invalidateCloudFrontCache: {
usage: 'Invalidates CloudFront cache',
lifecycleEvents: [
'invalidateCache',
],
},
};

this.hooks = {
'syncToS3:sync': this.syncDirectory.bind(this),
'domainInfo:domainInfo': this.domainInfo.bind(this),
'invalidateCloudFrontCache:invalidateCache': this.invalidateCache.bind(
this,
),
};
}

runAwsCommand(args) {
let command = 'aws';
if (this.serverless.variables.service.provider.region) {
command = `${command} --region ${this.serverless.variables.service.provider.region}`;
}
if (this.serverless.variables.service.provider.profile) {
command = `${command} --profile ${this.serverless.variables.service.provider.profile}`;
}
const result = spawnSync(command, args, { shell: true });
const stdout = result.stdout.toString();
const sterr = result.stderr.toString();
if (stdout) {
this.serverless.cli.log(stdout);
}
if (sterr) {
this.serverless.cli.log(sterr);
}

return { stdout, sterr };
}

// syncs the `app` directory to the provided bucket
syncDirectory() {
const s3Bucket = this.serverless.variables.service.custom.s3Bucket;
const buildFolder = this.serverless.variables.service.custom.client.distributionFolder;
const args = [
's3',
'sync',
`${buildFolder}/`,
`s3://${s3Bucket}/`,
'--delete',
];
const { sterr } = this.runAwsCommand(args);
if (!sterr) {
this.serverless.cli.log('Successfully synced to the S3 bucket');
} else {
throw new Error('Failed syncing to the S3 bucket');
}
}

// fetches the domain name from the CloudFront outputs and prints it out
async domainInfo() {
const provider = this.serverless.getProvider('aws');
const stackName = provider.naming.getStackName(this.options.stage);
const result = await provider.request(
'CloudFormation',
'describeStacks',
{ StackName: stackName },
this.options.stage,
this.options.region,
);

const outputs = result.Stacks[0].Outputs;
const output = outputs.find(
entry => entry.OutputKey === 'WebAppCloudFrontDistributionOutput',
);

if (output && output.OutputValue) {
this.serverless.cli.log(`Web App Domain: ${output.OutputValue}`);
return output.OutputValue;
}

this.serverless.cli.log('Web App Domain: Not Found');
const error = new Error('Could not extract Web App Domain');
throw error;
}

async invalidateCache() {
const provider = this.serverless.getProvider('aws');

const domain = await this.domainInfo();

const result = await provider.request(
'CloudFront',
'listDistributions',
{},
this.options.stage,
this.options.region,
);

const distributions = result.DistributionList.Items;
const distribution = distributions.find(
entry => entry.DomainName === domain,
);

if (distribution) {
this.serverless.cli.log(
`Invalidating CloudFront distribution with id: ${distribution.Id}`,
);
const args = [
'cloudfront',
'create-invalidation',
'--distribution-id',
distribution.Id,
'--paths',
'"/*"',
];
const { sterr } = this.runAwsCommand(args);
if (!sterr) {
this.serverless.cli.log('Successfully invalidated CloudFront cache');
} else {
throw new Error('Failed invalidating CloudFront cache');
}
} else {
const message = `Could not find distribution with domain ${domain}`;
const error = new Error(message);
this.serverless.cli.log(message);
throw error;
}
}
}

module.exports = ServerlessPlugin;
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
## AWS S3 CLOUDFRONT TASK2

## After S3 Hosting Staticwebsite is enabled

[Click S3 Bucket Deployment](http://latestnodeawsshopify.s3-website.ap-south-1.amazonaws.com) <br/>
NOTE: Public access is blocked , shows HTTP403

## After Cloudfront Enabled, Invalidate Cache Enabled

[Click CloudFront Deployment](https://d22izvif14nqas.cloudfront.net) <br/>
NOTE: Added two products ""AJTWO" and "AjOne"

## Task2.2 : Serverless

[Click here for Serverless deployment](https://d1zw82pbwmw3ax.cloudfront.net)
Accomplished creation of S3 Bucket
Accomplished creation of CloudFront without Invalidation.
Accomplished creation of CloudFront with Invalidation

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:
You can use NPM instead of YARN (Up to you)
You can use NPM instead of YARN (Up to you)

### `yarn start` OR `npm run start`

Expand Down
21 changes: 21 additions & 0 deletions dbscripts/products.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--drop table if exists products

CREATE table IF NOT EXISTS products (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
description TEXT NOT NULL,
price INTEGER
)

--seed data for products
insert into products(
title,description,price
) values
('Product_1','First Product description',100),
('Product_2','Second Product description',200),
('Product_3','Third Product description',300),
('Product_4','Fourth Product description',400),
('Product_5','Fivth Product description',500),
('Product_6','Sixth Product description',600)

select * from products
29 changes: 29 additions & 0 deletions dbscripts/stocks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
----drop table if exists stocks

CREATE TABLE IF NOT EXISTS stocks (
id serial PRIMARY KEY,
count INTEGER,
product_id uuid,
CONSTRAINT fk_products
FOREIGN KEY(product_id)
REFERENCES products(id)
)

--Fill product_ids from products table
insert into stocks(
product_id,count
) values
('f5fdcedf-9e45-456b-8e10-a7087567dec1',10),
('6a359846-bcc9-438d-beb3-f139e2998e27',20),
('094d9b41-c392-4d36-b1de-a94138ff5d21',30),
('dd0f3014-2f3e-4d82-a147-1e5aa8f44d88',40),
('c978f1f9-c2cb-424d-84a6-5e4af2139cdf',50),
('248ef0df-99e4-4e12-8887-b3305f7f22aa',60)

select * from stocks

--inner join query
select p.title,p.price,s.count from products p inner join stocks s on p.id = s.product_id

--query without join
select p.title,p.price,s.count from products p, stocks s where p.id = s.product_id
Loading