Skip to content

Instantly share code, notes, and snippets.

View dhavaln's full-sized avatar
💭
I may be slow to respond.

Dhaval Nagar dhavaln

💭
I may be slow to respond.
View GitHub Profile
@dhavaln
dhavaln / es-del.sh
Created September 5, 2022 05:07
Delete Records from Elastic Index
curl -k -X POST "https://<URL>/<index>/_delete_by_query?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"lt": "now-180d"
}
@dhavaln
dhavaln / handler.js
Created August 15, 2022 13:02
NodeJS Lambda function execution flow
const doSomething = () => {
console.log('Do Something Executed');
}
// no callback invoked
module.exports.handler = function () {
// Lambda finishes AFTER `doSomething()` is invoked
setTimeout(() => doSomething(), 1000);
};
@dhavaln
dhavaln / handler.js
Created July 7, 2022 04:41
Lambda function to get storage cost for the S3 Bucket Folder
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
let totalObjects = 0;
let dataMap = {};
const formatBytes = (bytes) => {
const marker = 1024;
const gigaBytes = marker * marker * marker;
const decimal = 2;
@dhavaln
dhavaln / scan-blocks.js
Last active May 24, 2022 10:00
Scan Chain blocks to query Event Logs
const ethers = require('ethers');
const ECO = require('../contracts/ECOContract.json');
const { CONTRACT_ADDRESS, DEPLOYER_KEY, NETWORK_URL } = process.env;
const provider = new ethers.providers.JsonRpcProvider(NETWORK_URL);
const deployerWallet = new ethers.Wallet(DEPLOYER_KEY);
// This block will come from DynamoDB last checkpoint block
const originBlock = 10730324;
@dhavaln
dhavaln / index.js
Last active August 17, 2021 05:44
Generate AWS Credentials using STS to limit S3 access
const AWS = require('aws-sdk')
const STS = new AWS.STS();
const bucketName = 'appgambittest' //bucket where the data files are stored
const userId = 'user123' //userId who is requesting temporary access
const appId = 'userdir-user123' //directory name where the user data is available on s3
const roleArn = 'arn:aws:iam::XXXXXXXXXX:role/CustomS3AccessRole' //role with trust relationship and full bucket access
//generate inline session policy, with restricted access to the specific directory only
const generateInlinePolicy = (bucketName, folderName) => {
@dhavaln
dhavaln / .env
Last active August 6, 2022 07:03
MongoDB Atlas Serverless with NodeJS 4.1 Drivers
MONGO_URL=mongodb+srv://<USER>:<PASSWORD>@<LB>.mongodb.net/?retryWrites=true&w=majority
DB=
COLLECTION=
@dhavaln
dhavaln / id-pool-client.yml
Created December 30, 2020 07:13
Cognito Identity Pool Client Config
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref cognitoUserPool
AllowedOAuthFlows:
- 'implicit'
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthScopes:
- 'openid'
- 'aws.cognito.signin.user.admin'
CallbackURLs:
@dhavaln
dhavaln / app-config.js
Created December 29, 2020 09:40
ServerlessDocs Frontend App Config
//this file is in git ignore
//you need to create app-config.js, exact similar to this sample file, you can go to cloudformation stack output section to get all values
const userPoolId = '';
const normalClientId = '';
const region = 'us-west-2';
const identityPoolId = '';
const S3FilesBucket = '';
//=============== Cognito Group Name ===================
const adminGroupName = 'Admin'
//===============AWS Cognito Hosted Ui References==================
@dhavaln
dhavaln / identity-pool-client.yml
Last active December 29, 2020 07:21
Cognito Identity Pool client for Cognito User Pool Auth
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref cognitoUserPool
AllowedOAuthFlows:
- 'implicit'
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthScopes:
- 'openid'
- 'aws.cognito.signin.user.admin'
CallbackURLs:
@dhavaln
dhavaln / user-pool.yml
Created December 29, 2020 07:03
Amazon Cognito User Pool with Pre and Post triggers
Type: AWS::Cognito::UserPool
Properties:
AccountRecoverySetting:
RecoveryMechanisms:
- Name: 'verified_email'
Priority: 2
AutoVerifiedAttributes:
- email
Policies:
PasswordPolicy: