1
1
import * as aws from "@pulumi/aws" ;
2
- import { createPulumiApp , type PulumiAppParam , type PulumiAppParamCallback } from "@webiny/pulumi" ;
2
+ import { createPulumiApp , isResourceOfType } from "@webiny/pulumi" ;
3
3
import {
4
4
ApiApwScheduler ,
5
5
ApiBackgroundTask ,
@@ -11,96 +11,52 @@ import {
11
11
ApiPageBuilder ,
12
12
ApiWebsocket ,
13
13
CoreOutput ,
14
- type CreateCorePulumiAppParams ,
14
+ CorePulumiApp ,
15
15
VpcConfig
16
16
} from "~/apps/index.js" ;
17
- import { applyCustomDomain , type CustomDomainParams } from "../customDomain.js" ;
18
17
import {
19
18
addDomainsUrlsOutputs ,
20
- tagResources ,
21
19
withCommonLambdaEnvVariables ,
22
20
withServiceManifest
23
21
} 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" ;
28
22
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" ;
29
31
30
32
export type ApiPulumiApp = ReturnType < typeof createApiPulumiApp > ;
31
33
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 = ( ) => {
86
35
const baseApp = createPulumiApp ( {
87
36
name : "api" ,
88
37
path : "apps/api" ,
89
- config : projectAppParams ,
90
38
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
+
91
47
let searchEngineParams :
92
- | CreateCorePulumiAppParams [ "openSearch" ]
93
- | CreateCorePulumiAppParams [ "elasticSearch" ]
48
+ | typeof openSearchExtensionConfig
49
+ | typeof elasticSearchExtensionConfig
94
50
| null = null ;
95
51
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 ;
100
56
}
101
57
102
58
if ( searchEngineParams ) {
103
- const params = app . getParam ( searchEngineParams ) ;
59
+ const params = searchEngineParams ;
104
60
if ( typeof params === "object" ) {
105
61
if ( params . domainName ) {
106
62
process . env . AWS_ELASTIC_SEARCH_DOMAIN_NAME = params . domainName ;
@@ -116,9 +72,6 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
116
72
}
117
73
}
118
74
119
- const pulumiResourceNamePrefix = app . getParam (
120
- projectAppParams . pulumiResourceNamePrefix
121
- ) ;
122
75
if ( pulumiResourceNamePrefix ) {
123
76
app . onResource ( resource => {
124
77
if ( ! resource . name . startsWith ( pulumiResourceNamePrefix ) ) {
@@ -127,23 +80,75 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
127
80
} ) ;
128
81
}
129
82
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
+
130
136
// 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 ) ;
137
139
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 ;
141
145
142
146
// Register core output as a module available to all the other modules
143
147
const core = app . addModule ( CoreOutput ) ;
144
148
145
149
// Register VPC config module to be available to other modules.
146
- const vpcEnabled = app . getParam ( projectAppParams ?. vpc ) ?? isProduction ;
150
+ const vpcEnabled = ! ! vpcExtensionsConfig ?? isProduction ;
151
+
147
152
app . addModule ( VpcConfig , { enabled : vpcEnabled } ) ;
148
153
149
154
const pageBuilder = app . addModule ( ApiPageBuilder , {
@@ -254,10 +259,10 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
254
259
const backgroundTask = app . addModule ( ApiBackgroundTask ) ;
255
260
const migration = app . addModule ( ApiMigration ) ;
256
261
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
+ // }
261
266
262
267
app . addOutputs ( {
263
268
region : aws . config . region ,
@@ -298,11 +303,8 @@ export const createApiPulumiApp = (projectAppParams: CreateApiPulumiAppParams =
298
303
} ) ;
299
304
} ) ;
300
305
301
- tagResources ( {
302
- WbyProjectName : getEnvVariableWebinyProjectName ( ) ,
303
- WbyEnvironment : getEnvVariableWebinyEnv ( ) ,
304
- WbyEnvironmentVariant : getEnvVariableWebinyVariant ( )
305
- } ) ;
306
+ // Applies internal and user-defined AWS tags.
307
+ await applyAwsResourceTags ( "api" ) ;
306
308
307
309
return {
308
310
fileManager,
0 commit comments