Skip to content

Commit a67f3f7

Browse files
committed
wip
1 parent 3957181 commit a67f3f7

File tree

1 file changed

+94
-92
lines changed

1 file changed

+94
-92
lines changed

packages/pulumi-aws/src/apps/api/createApiPulumiApp.ts

Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as aws from "@pulumi/aws";
2-
import { createPulumiApp, type PulumiAppParam, type PulumiAppParamCallback } from "@webiny/pulumi";
2+
import { createPulumiApp, isResourceOfType } from "@webiny/pulumi";
33
import {
44
ApiApwScheduler,
55
ApiBackgroundTask,
@@ -11,96 +11,52 @@ import {
1111
ApiPageBuilder,
1212
ApiWebsocket,
1313
CoreOutput,
14-
type CreateCorePulumiAppParams,
14+
CorePulumiApp,
1515
VpcConfig
1616
} from "~/apps/index.js";
17-
import { applyCustomDomain, type CustomDomainParams } from "../customDomain.js";
1817
import {
1918
addDomainsUrlsOutputs,
20-
tagResources,
2119
withCommonLambdaEnvVariables,
2220
withServiceManifest
2321
} from "~/utils/index.js";
24-
import { DEFAULT_PROD_ENV_NAMES } from "~/constants.js";
25-
import { getEnvVariableWebinyVariant } from "~/env/variant.js";
26-
import { getEnvVariableWebinyEnv } from "~/env/env.js";
27-
import { getEnvVariableWebinyProjectName } from "~/env/projectName.js";
2822
import { getEnvVariableAwsRegion } from "~/env/awsRegion.js";
23+
import { getProjectSdk } from "@webiny/project";
24+
import { getVpcConfigFromExtension } from "~/apps/extensions/getVpcConfigFromExtension";
25+
import { getOsConfigFromExtension } from "~/apps/extensions/getOsConfigFromExtension";
26+
import { getEsConfigFromExtension } from "~/apps/extensions/getEsConfigFromExtension";
27+
import { ApiPulumi } from "@webiny/project/abstractions";
28+
import { applyAwsResourceTags } from "~/apps/awsUtils";
29+
import { License } from "@webiny/wcp";
30+
import { handleGuardDutyEvents } from "./handleGuardDutyEvents";
2931

3032
export type ApiPulumiApp = ReturnType<typeof createApiPulumiApp>;
3133

32-
export interface ApiElasticsearchConfig {
33-
domainName: string;
34-
indexPrefix: string;
35-
sharedIndexes: boolean;
36-
}
37-
38-
export interface ApiOpenSearchConfig {
39-
domainName: string;
40-
indexPrefix: string;
41-
sharedIndexes: boolean;
42-
}
43-
44-
export interface CreateApiPulumiAppParams {
45-
/**
46-
* Enables ElasticSearch infrastructure.
47-
* Note that it requires also changes in application code.
48-
*/
49-
elasticSearch?: PulumiAppParam<boolean | Partial<ApiElasticsearchConfig>>;
50-
51-
/**
52-
* Enables OpenSearch infrastructure.
53-
* Note that it requires also changes in application code.
54-
*/
55-
openSearch?: PulumiAppParam<boolean | Partial<ApiOpenSearchConfig>>;
56-
57-
/**
58-
* Enables or disables VPC for the API.
59-
* For VPC to work you also have to enable it in the Core application.
60-
*/
61-
vpc?: PulumiAppParam<boolean>;
62-
63-
/** Custom domain configuration */
64-
domains?: PulumiAppParamCallback<CustomDomainParams>;
65-
66-
/**
67-
* Provides a way to adjust existing Pulumi code (cloud infrastructure resources)
68-
* or add additional ones into the mix.
69-
*/
70-
pulumi?: (app: ApiPulumiApp) => void | Promise<void>;
71-
72-
/**
73-
* Prefixes names of all Pulumi cloud infrastructure resource with given prefix.
74-
*/
75-
pulumiResourceNamePrefix?: PulumiAppParam<string>;
76-
77-
/**
78-
* Treats provided environments as production environments, which
79-
* are deployed in production deployment mode.
80-
* https://www.webiny.com/docs/architecture/deployment-modes/production
81-
*/
82-
productionEnvironments?: PulumiAppParam<string[]>;
83-
}
84-
85-
export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams = {}) => {
34+
export const createApiPulumiApp = () => {
8635
const baseApp = createPulumiApp({
8736
name: "api",
8837
path: "apps/api",
89-
config: projectAppParams,
9038
program: async app => {
39+
const sdk = await getProjectSdk();
40+
const projectConfig = await sdk.getProjectConfig();
41+
42+
const pulumiResourceNamePrefix = await sdk.getPulumiResourceNamePrefix();
43+
const vpcExtensionsConfig = getVpcConfigFromExtension(projectConfig);
44+
const openSearchExtensionConfig = getOsConfigFromExtension(projectConfig);
45+
const elasticSearchExtensionConfig = getEsConfigFromExtension(projectConfig);
46+
9147
let searchEngineParams:
92-
| CreateCorePulumiAppParams["openSearch"]
93-
| CreateCorePulumiAppParams["elasticSearch"]
48+
| typeof openSearchExtensionConfig
49+
| typeof elasticSearchExtensionConfig
9450
| null = null;
9551

96-
if (projectAppParams.openSearch) {
97-
searchEngineParams = app.getParam(projectAppParams.openSearch);
98-
} else if (projectAppParams.elasticSearch) {
99-
searchEngineParams = app.getParam(projectAppParams.elasticSearch);
52+
if (openSearchExtensionConfig) {
53+
searchEngineParams = openSearchExtensionConfig;
54+
} else if (elasticSearchExtensionConfig) {
55+
searchEngineParams = elasticSearchExtensionConfig;
10056
}
10157

10258
if (searchEngineParams) {
103-
const params = app.getParam(searchEngineParams);
59+
const params = searchEngineParams;
10460
if (typeof params === "object") {
10561
if (params.domainName) {
10662
process.env.AWS_ELASTIC_SEARCH_DOMAIN_NAME = params.domainName;
@@ -116,9 +72,6 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
11672
}
11773
}
11874

119-
const pulumiResourceNamePrefix = app.getParam(
120-
projectAppParams.pulumiResourceNamePrefix
121-
);
12275
if (pulumiResourceNamePrefix) {
12376
app.onResource(resource => {
12477
if (!resource.name.startsWith(pulumiResourceNamePrefix)) {
@@ -127,23 +80,75 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
12780
});
12881
}
12982

83+
// <-------------------- Enterprise start -------------------->
84+
app.addHandler(async () => {
85+
const license = await License.fromEnvironment();
86+
87+
const usingAdvancedVpcParams =
88+
vpcExtensionsConfig && typeof vpcExtensionsConfig !== "boolean";
89+
90+
if (license.canUseFileManagerThreatDetection()) {
91+
handleGuardDutyEvents(app as ApiPulumiApp);
92+
}
93+
94+
// Not using advanced VPC params? Then immediately exit.
95+
if (usingAdvancedVpcParams) {
96+
const { onResource, addResource } = app;
97+
const { useExistingVpc } = vpcExtensionsConfig;
98+
99+
// 1. We first deal with "existing VPC" setup.
100+
if (useExistingVpc) {
101+
if (!useExistingVpc.lambdaFunctionsVpcConfig) {
102+
throw new Error(
103+
"Cannot specify `useExistingVpc` parameter because the `lambdaFunctionsVpcConfig` parameter wasn't provided."
104+
);
105+
}
106+
107+
onResource(resource => {
108+
if (isResourceOfType(resource, aws.lambda.Function)) {
109+
const canUseVpc = resource.meta.canUseVpc !== false;
110+
if (canUseVpc) {
111+
resource.config.vpcConfig(
112+
useExistingVpc!.lambdaFunctionsVpcConfig
113+
);
114+
}
115+
}
116+
117+
if (isResourceOfType(resource, aws.iam.Role)) {
118+
if (resource.meta.isLambdaFunctionRole) {
119+
addResource(aws.iam.RolePolicyAttachment, {
120+
name: `${resource.name}-vpc-access-execution-role`,
121+
config: {
122+
role: resource.output.name,
123+
policyArn:
124+
aws.iam.ManagedPolicy
125+
.AWSLambdaVPCAccessExecutionRole
126+
}
127+
});
128+
}
129+
}
130+
});
131+
}
132+
}
133+
});
134+
// <-------------------- Enterprise end -------------------->
135+
130136
// Overrides must be applied via a handler, registered at the very start of the program.
131-
// By doing this, we're ensuring user's adjustments are not applied too late.
132-
if (projectAppParams.pulumi) {
133-
app.addHandler(() => {
134-
return projectAppParams.pulumi!(app as ApiPulumiApp);
135-
});
136-
}
137+
// By doing this, we're ensuring user's adjustments are not applied to late.
138+
const pulumiHandlers = sdk.getContainer().resolve(ApiPulumi);
137139

138-
const productionEnvironments =
139-
app.params.create.productionEnvironments || DEFAULT_PROD_ENV_NAMES;
140-
const isProduction = productionEnvironments.includes(app.params.run.env);
140+
app.addHandler(() => {
141+
return pulumiHandlers.execute(app as unknown as CorePulumiApp);
142+
});
143+
144+
const isProduction = app.env.isProduction;
141145

142146
// Register core output as a module available to all the other modules
143147
const core = app.addModule(CoreOutput);
144148

145149
// Register VPC config module to be available to other modules.
146-
const vpcEnabled = app.getParam(projectAppParams?.vpc) ?? isProduction;
150+
const vpcEnabled = !!vpcExtensionsConfig ?? isProduction;
151+
147152
app.addModule(VpcConfig, { enabled: vpcEnabled });
148153

149154
const pageBuilder = app.addModule(ApiPageBuilder, {
@@ -254,10 +259,10 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
254259
const backgroundTask = app.addModule(ApiBackgroundTask);
255260
const migration = app.addModule(ApiMigration);
256261

257-
const domains = app.getParam(projectAppParams.domains);
258-
if (domains) {
259-
applyCustomDomain(cloudfront, domains);
260-
}
262+
// const domains = app.getParam(projectAppParams.domains);
263+
// if (domains) {
264+
// applyCustomDomain(cloudfront, domains);
265+
// }
261266

262267
app.addOutputs({
263268
region: aws.config.region,
@@ -298,11 +303,8 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
298303
});
299304
});
300305

301-
tagResources({
302-
WbyProjectName: getEnvVariableWebinyProjectName(),
303-
WbyEnvironment: getEnvVariableWebinyEnv(),
304-
WbyEnvironmentVariant: getEnvVariableWebinyVariant()
305-
});
306+
// Applies internal and user-defined AWS tags.
307+
await applyAwsResourceTags("api");
306308

307309
return {
308310
fileManager,

0 commit comments

Comments
 (0)