Skip to content

Commit

Permalink
feat: add a script and ci workflow to build all examples (#733)
Browse files Browse the repository at this point in the history
* feat: add a script and ci workflow to build all examples
  • Loading branch information
pratik151192 authored Aug 17, 2023
1 parent 170885f commit 59fe10f
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 37 deletions.
3 changes: 2 additions & 1 deletion examples/cloudflare-workers/http-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"start": "wrangler dev"
"start": "wrangler dev",
"build": "tsc"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230419.0",
Expand Down
3 changes: 2 additions & 1 deletion examples/cloudflare-workers/web-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"start": "wrangler dev"
"start": "wrangler dev",
"build": "tsc"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230419.0",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"license": "ISC",
"devDependencies": {
"@types/aws-lambda": "8.10.118",
"@types/google-protobuf": "^3.15.6",
"@types/node": "^16.11.4",
"@typescript-eslint/eslint-plugin": "5.30.5",
"@typescript-eslint/parser": "^5.0.0",
Expand Down
6 changes: 6 additions & 0 deletions examples/nodejs/lambda-examples/simple-get/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as apig from 'aws-cdk-lib/aws-apigateway';
import * as secrets from 'aws-cdk-lib/aws-secretsmanager';
import * as config from '../../lambda/token-vending-machine/config';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import {AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId} from 'aws-cdk-lib/custom-resources';

export class TokenVendingMachineStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
Expand Down Expand Up @@ -61,20 +61,20 @@ export class TokenVendingMachineStack extends cdk.Stack {
timeout: cdk.Duration.seconds(30),
memorySize: 128,
});

const authorizer = new apig.RequestAuthorizer(this, 'MomentoTokenVendingMachineTokenAuthorizer', {
handler: authLambda,
identitySources: [apig.IdentitySource.header('username'), apig.IdentitySource.header('password')],
resultsCacheTtl: cdk.Duration.seconds(5),
});

api.root.addMethod('GET', tvmIntegration, {
authorizer: authorizer,
});

api.root.addCorsPreflight({
allowOrigins: ['*'],
allowHeaders: ['username', 'password']
allowHeaders: ['username', 'password'],
});

break;
Expand All @@ -83,25 +83,25 @@ export class TokenVendingMachineStack extends cdk.Stack {
const userPool = new cognito.UserPool(this, 'MomentoTokenVendingMachineUserPool', {
userPoolName: 'MomentoTokenVendingMachineUserPool',
signInAliases: {
username: true
username: true,
},
passwordPolicy: {
minLength: 8,
requireSymbols: true,
}
},
});
new cdk.CfnOutput(this, "UserPoolId", {
new cdk.CfnOutput(this, 'UserPoolId', {
value: userPool.userPoolId,
});

const userPoolClient = new cognito.UserPoolClient(this, 'MomentoTokenVendingMachineUserPoolClient', {
userPool,
generateSecret: false,
authFlows: {
userPassword: true
}
userPassword: true,
},
});
new cdk.CfnOutput(this, "UserPoolClientId", {
new cdk.CfnOutput(this, 'UserPoolClientId', {
value: userPoolClient.userPoolClientId,
});

Expand All @@ -120,42 +120,44 @@ export class TokenVendingMachineStack extends cdk.Stack {
const authorizer = new apig.CognitoUserPoolsAuthorizer(this, 'MomentoTokenVendingMachineTokenAuthorizer', {
cognitoUserPools: [userPool],
});

api.root.addMethod('GET', tvmIntegration, {
authorizationType: apig.AuthorizationType.COGNITO,
authorizer: authorizer,
});

api.root.addCorsPreflight({
allowOrigins: ['*'],
allowHeaders: ['usergroup', 'cachename', 'authorization']
allowHeaders: ['usergroup', 'cachename', 'authorization'],
});

this.createCognitoUser(this, userPool, "momento", "$erverless", userPoolReadWriteGroup.groupName);
this.createCognitoUser(this, userPool, "serverless", "momento!", userPoolReadOnlyGroup.groupName);
this.createCognitoUser(this, userPool, 'momento', '$erverless', userPoolReadWriteGroup.groupName);
this.createCognitoUser(this, userPool, 'serverless', 'momento!', userPoolReadOnlyGroup.groupName);

break;
}
case config.AuthenticationMethod.Open: {
console.log("Warning: no authentication method provided, the Token Vending Machine URL will be publicly accessible");
console.log(
'Warning: no authentication method provided, the Token Vending Machine URL will be publicly accessible'
);
api.root.addMethod('GET', tvmIntegration);
api.root.addCorsPreflight({
allowOrigins: ['*'],
});
break;
}
default: {
throw new Error("Unrecognized authentication method");
throw new Error('Unrecognized authentication method');
}
}
}

// reference: https://github.com/awesome-cdk/cdk-userpool-user/blob/master/lib/UserPoolUser.ts
// reference: https://github.com/awesome-cdk/cdk-userpool-user/blob/master/lib/UserPoolUser.ts
private createCognitoUser(
scope: Construct,
userPool: cognito.IUserPool,
username: string,
password: string,
userPool: cognito.IUserPool,
username: string,
password: string,
group?: string
) {
// Basically use the AWS SDK to fill in this gap in the CDK for creating a user with a password
Expand All @@ -167,7 +169,7 @@ export class TokenVendingMachineStack extends cdk.Stack {
UserPoolId: userPool.userPoolId,
Username: username,
MessageAction: 'SUPPRESS',
TemporaryPassword: password
TemporaryPassword: password,
},
physicalResourceId: PhysicalResourceId.of(`AwsCustomResource-CreateCognitoUser-${username}`),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
"description": "Momento token vending machine API Gateway Lambda authorizer for the Lambda function",
"main": "index.ts",
"scripts": {
"prebuild": "eslint . --ext .ts",
"build": "tsc",
"test": "jest",
"lint": "eslint . --ext .ts",
"format": "eslint . --ext .ts --fix"
"test": "jest"
},
"author": "",
"license": "ISC",
Expand Down
Loading

0 comments on commit 59fe10f

Please sign in to comment.