Skip to content

Commit

Permalink
Decomposed storage and API apps into modules (#2360)
Browse files Browse the repository at this point in the history
* feat: disable public access to S3 buckets (cloudfront only allowed)

* fix: changes after review

* fix: added logging for page builder setting update

* fix: compilation fix

* feat: poc on how to simplify pulumi codebase

* feat: getting rid of pulumi code files

* feat: experimenting on new pulumi architecture

* feat: admin app simplification

* feat: option to add multiple app outputs

* feat: migrated website app to the new approach

* fix: fixed typings

* fix: path separator for linux/mac

* feat: moved pulumi yaml files to pulumi folder

* fix: getStackOutput now works with new architecture

* feat: extracted admin after deploy hook to pulumi-sdk package

* feat: migrated website app hooks to new architecture

* fix: minor eslint issue

* feat: updated templates for admin and website

* fix: getting tests to fully work

* Revert "feat: updated templates for admin and website"

This reverts commit 8461221.

* feat: more flexibility and simplified setup

* fix: fixed pulumi outputs and running pulumi commands

* fix: rolled back making S3 bucket private

* feat: resource name customization and output formatting

* feat: pulumi update and added coloring to pulumi output logs

* feat: better configurability for nested resource configuration

* chore: removing some old not used code

* feat: added easier setup for custom domains

* feat: extracted storage app and migrated API to new arch

* fix: updating pulumi-sdk-v6

* fix: nasty fix for pulumi login issue

* fix: compatibility fix

* fix: fixes after review

* fix: some fixes after review

* feat: migrated APW to new pulumi arch

* fix: renaming hooks

* fix: fixes after review

* fix: removed todos

* fix: minor change to force running tests on CI

* fix: added some comments to storage migration scrip[t

* feat: rewritten storage and API to use modules

* fix: fixed package references

* fix: fix build after changing names previously

* fix: review fixes

* feat: added check on adding modules twice
  • Loading branch information
kedrzu authored May 10, 2022
1 parent 9b6904f commit e297c80
Show file tree
Hide file tree
Showing 20 changed files with 673 additions and 573 deletions.
3 changes: 3 additions & 0 deletions packages/pulumi-aws/src/apps/admin/AdminApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ export function createAdminApp(config?: AdminAppConfig & ApplicationConfig<Admin
}
},
async app(ctx) {
// Create the app instance.
const app = new AdminApp(ctx);
// Run the default application setup.
await app.setup(config || {});
// Run the custom user config.
await config?.config?.(app, ctx);
return app;
},
Expand Down
33 changes: 17 additions & 16 deletions packages/pulumi-aws/src/apps/api/ApiApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import {
ApplicationConfig
} from "@webiny/pulumi-sdk";

import { createGraphql } from "./ApiGraphql";
import { ApiGraphql } from "./ApiGraphql";
import { createVpc, Vpc } from "./ApiVpc";
import { createPrerenderingService } from "./ApiPrerendering";
import { createFileManager } from "./ApiFileManager";
import { createPageBuilder } from "./ApiPageBuilder";
import { createHeadlessCms } from "./ApiHeadlessCMS";
import { createApiGateway } from "./ApiGateway";
import { createCloudfront } from "./ApiCloudfront";
import { ApiFileManager } from "./ApiFileManager";
import { ApiPageBuilder } from "./ApiPageBuilder";
import { ApiHeadlessCMS } from "./ApiHeadlessCMS";
import { ApiGateway } from "./ApiGateway";
import { ApiCloudfront } from "./ApiCloudfront";
import { getStorageOutput } from "../getStorageOutput";
import { getAwsAccountId, getAwsRegion } from "../awsUtils";
import { createApwScheduler } from "./ApiApwScheduler";
import { ApiApwScheduler } from "./ApiApwScheduler";

export interface ApiAppConfig {
vpc?(app: PulumiApp, ctx: ApplicationContext): boolean | Vpc;
Expand All @@ -41,7 +41,7 @@ export const ApiApp = defineApp({
const awsAccountId = getAwsAccountId(app);
const awsRegion = getAwsRegion(app);

const pageBuilder = createPageBuilder(app, {
const pageBuilder = app.addModule(ApiPageBuilder, {
env: {
COGNITO_REGION: String(process.env.AWS_REGION),
COGNITO_USER_POOL_ID: storage.cognitoUserPoolId,
Expand All @@ -58,7 +58,7 @@ export const ApiApp = defineApp({
vpc
});

const fileManager = createFileManager(app, {
const fileManager = app.addModule(ApiFileManager, {
fileManagerBucketId: storage.fileManagerBucketId,
vpc
});
Expand All @@ -76,7 +76,7 @@ export const ApiApp = defineApp({
vpc
});

const apwScheduler = createApwScheduler(app, {
const apwScheduler = app.addModule(ApiApwScheduler, {
primaryDynamodbTableArn: storage.primaryDynamodbTableArn,
env: {
COGNITO_REGION: String(process.env.AWS_REGION),
Expand All @@ -88,7 +88,7 @@ export const ApiApp = defineApp({
}
});

const graphql = createGraphql(app, {
const graphql = app.addModule(ApiGraphql, {
env: {
COGNITO_REGION: String(process.env.AWS_REGION),
COGNITO_USER_POOL_ID: storage.cognitoUserPoolId,
Expand Down Expand Up @@ -120,7 +120,7 @@ export const ApiApp = defineApp({
vpc
});

const headlessCms = createHeadlessCms(app, {
const headlessCms = app.addModule(ApiHeadlessCMS, {
env: {
COGNITO_REGION: String(process.env.AWS_REGION),
COGNITO_USER_POOL_ID: storage.cognitoUserPoolId,
Expand All @@ -135,7 +135,7 @@ export const ApiApp = defineApp({
vpc
});

const apiGateway = createApiGateway(app, {
const apiGateway = app.addModule(ApiGateway, {
"graphql-post": {
path: "/graphql",
method: "POST",
Expand Down Expand Up @@ -163,9 +163,7 @@ export const ApiApp = defineApp({
}
});

const cloudfront = createCloudfront(app, {
apiGateway
});
const cloudfront = app.addModule(ApiCloudfront);

app.addOutputs({
region: process.env.AWS_REGION,
Expand Down Expand Up @@ -211,8 +209,11 @@ export function createApiApp(config?: ApiAppConfig & ApplicationConfig<ApiApp>)
}
},
async app(ctx) {
// Create the app instance.
const app = new ApiApp(ctx);
// Run the default application setup.
await app.setup(config || {});
// Run the custom user config.
await config?.config?.(app, ctx);
return app;
},
Expand Down
81 changes: 43 additions & 38 deletions packages/pulumi-aws/src/apps/api/ApiApwScheduler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import { PulumiApp } from "@webiny/pulumi-sdk";
import { defineAppModule, PulumiApp, PulumiAppModule } from "@webiny/pulumi-sdk";

interface ScheduleActionParams {
env: Record<string, any>;
Expand All @@ -14,47 +14,52 @@ const EXECUTE_ACTION_LAMBDA = `${LAMBDA_NAME_PREFIX}-execute-action-lambda`;
const EVENT_RULE_NAME = `${LAMBDA_NAME_PREFIX}-event-rule`;
const EVENT_RULE_TARGET = `${LAMBDA_NAME_PREFIX}-event-rule-target`;

export function createApwScheduler(app: PulumiApp, params: ScheduleActionParams) {
const executeAction = createExecuteActionLambda(app, params);
const scheduleAction = createScheduleActionLambda(app, executeAction.lambda.output, params);
export type ApiApwScheduler = PulumiAppModule<typeof ApiApwScheduler>;

// Create event rule.
const eventRule = app.addResource(aws.cloudwatch.EventRule, {
name: EVENT_RULE_NAME,
config: {
description: `Enable us to schedule an action in publishing workflow at a particular datetime`,
scheduleExpression: "cron(* * * * ? 2000)",
isEnabled: true
}
});
export const ApiApwScheduler = defineAppModule({
name: "ApiApwScheduler",
config(app: PulumiApp, params: ScheduleActionParams) {
const executeAction = createExecuteActionLambda(app, params);
const scheduleAction = createScheduleActionLambda(app, executeAction.lambda.output, params);

// Add required permission to the target lambda.
app.addResource(aws.lambda.Permission, {
name: "eventTargetPermission",
config: {
action: "lambda:InvokeFunction",
function: scheduleAction.lambda.output.arn,
principal: "events.amazonaws.com",
statementId: "allow-rule-invoke-" + EVENT_RULE_NAME
}
});
// Create event rule.
const eventRule = app.addResource(aws.cloudwatch.EventRule, {
name: EVENT_RULE_NAME,
config: {
description: `Enable us to schedule an action in publishing workflow at a particular datetime`,
scheduleExpression: "cron(* * * * ? 2000)",
isEnabled: true
}
});

// Add lambda as target to the event rule.
const eventTarget = app.addResource(aws.cloudwatch.EventTarget, {
name: EVENT_RULE_TARGET,
config: {
rule: eventRule.output.name,
arn: scheduleAction.lambda.output.arn
}
});
// Add required permission to the target lambda.
app.addResource(aws.lambda.Permission, {
name: "eventTargetPermission",
config: {
action: "lambda:InvokeFunction",
function: scheduleAction.lambda.output.arn,
principal: "events.amazonaws.com",
statementId: "allow-rule-invoke-" + EVENT_RULE_NAME
}
});

return {
executeAction,
scheduleAction,
eventRule,
eventTarget
};
}
// Add lambda as target to the event rule.
const eventTarget = app.addResource(aws.cloudwatch.EventTarget, {
name: EVENT_RULE_TARGET,
config: {
rule: eventRule.output.name,
arn: scheduleAction.lambda.output.arn
}
});

return {
executeAction,
scheduleAction,
eventRule,
eventTarget
};
}
});

function createExecuteActionLambda(app: PulumiApp, params: ScheduleActionParams) {
const role = app.addResource(aws.iam.Role, {
Expand Down
Loading

0 comments on commit e297c80

Please sign in to comment.