|
| 1 | +import { existsSync } from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { Aws, Duration, RemovalPolicy } from 'aws-cdk-lib'; |
| 4 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 5 | +import * as lambda from 'aws-cdk-lib/aws-lambda'; |
| 6 | +import * as lambdaNodejs from 'aws-cdk-lib/aws-lambda-nodejs'; |
| 7 | +import * as logs from 'aws-cdk-lib/aws-logs'; |
| 8 | +import { Construct } from 'constructs'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Properties to initialize an instance of `MicroAppsChildDeployer`. |
| 12 | + */ |
| 13 | +export interface MicroAppsChildDeployerProps { |
| 14 | + /** |
| 15 | + * ARN of the parent Deployer Lambda Function |
| 16 | + */ |
| 17 | + readonly parentDeployerLambdaARN: string; |
| 18 | + |
| 19 | + /** |
| 20 | + * RemovalPolicy override for child resources |
| 21 | + * |
| 22 | + * Note: if set to DESTROY the S3 buckes will have `autoDeleteObjects` set to `true` |
| 23 | + * |
| 24 | + * @default - per resource default |
| 25 | + */ |
| 26 | + readonly removalPolicy?: RemovalPolicy; |
| 27 | + |
| 28 | + /** |
| 29 | + * Application environment, passed as `NODE_ENV` |
| 30 | + * to the Router and Deployer Lambda functions |
| 31 | + */ |
| 32 | + readonly appEnv: string; |
| 33 | + |
| 34 | + /** |
| 35 | + * Optional asset name root |
| 36 | + * |
| 37 | + * @example microapps |
| 38 | + * @default - resource names auto assigned |
| 39 | + */ |
| 40 | + readonly assetNameRoot?: string; |
| 41 | + |
| 42 | + /** |
| 43 | + * Optional asset name suffix |
| 44 | + * |
| 45 | + * @example -dev-pr-12 |
| 46 | + * @default none |
| 47 | + */ |
| 48 | + readonly assetNameSuffix?: string; |
| 49 | + |
| 50 | + /** |
| 51 | + * Deployer timeout |
| 52 | + * |
| 53 | + * For larger applications this needs to be set up to 2-5 minutes for the S3 copy |
| 54 | + * |
| 55 | + * @default 2 minutes |
| 56 | + */ |
| 57 | + readonly deployerTimeout?: Duration; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Represents a MicroApps Child Deployer |
| 62 | + */ |
| 63 | +export interface IMicroAppsChildDeployer { |
| 64 | + /** |
| 65 | + * Lambda function for the Deployer |
| 66 | + */ |
| 67 | + readonly deployerFunc: lambda.IFunction; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Create a new MicroApps Child Deployer construct. |
| 72 | + */ |
| 73 | +export class MicroAppsChildDeployer extends Construct implements IMicroAppsChildDeployer { |
| 74 | + private _deployerFunc: lambda.Function; |
| 75 | + public get deployerFunc(): lambda.IFunction { |
| 76 | + return this._deployerFunc; |
| 77 | + } |
| 78 | + |
| 79 | + constructor(scope: Construct, id: string, props?: MicroAppsChildDeployerProps) { |
| 80 | + super(scope, id); |
| 81 | + |
| 82 | + if (props === undefined) { |
| 83 | + throw new Error('props cannot be undefined'); |
| 84 | + } |
| 85 | + |
| 86 | + const { |
| 87 | + appEnv, |
| 88 | + deployerTimeout = Duration.minutes(2), |
| 89 | + assetNameRoot, |
| 90 | + assetNameSuffix, |
| 91 | + removalPolicy, |
| 92 | + parentDeployerLambdaARN, |
| 93 | + } = props; |
| 94 | + |
| 95 | + // |
| 96 | + // Deployer Lambda Function |
| 97 | + // |
| 98 | + |
| 99 | + // Create Deployer Lambda Function |
| 100 | + const deployerFuncName = assetNameRoot |
| 101 | + ? `${assetNameRoot}-deployer${assetNameSuffix}` |
| 102 | + : undefined; |
| 103 | + const deployerFuncProps: Omit<lambda.FunctionProps, 'handler' | 'code'> = { |
| 104 | + functionName: deployerFuncName, |
| 105 | + memorySize: 1769, |
| 106 | + logRetention: logs.RetentionDays.ONE_MONTH, |
| 107 | + runtime: lambda.Runtime.NODEJS_16_X, |
| 108 | + timeout: deployerTimeout, |
| 109 | + environment: { |
| 110 | + NODE_ENV: appEnv, |
| 111 | + AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1', |
| 112 | + PARENT_DEPLOYER_LAMBDA_ARN: parentDeployerLambdaARN, |
| 113 | + }, |
| 114 | + }; |
| 115 | + if ( |
| 116 | + process.env.NODE_ENV === 'test' && |
| 117 | + existsSync(path.join(__dirname, '..', '..', 'microapps-deployer', 'dist', 'index.js')) |
| 118 | + ) { |
| 119 | + // This is for local dev |
| 120 | + this._deployerFunc = new lambda.Function(this, 'deployer-func', { |
| 121 | + code: lambda.Code.fromAsset(path.join(__dirname, '..', '..', 'microapps-deployer', 'dist')), |
| 122 | + handler: 'index.handler', |
| 123 | + ...deployerFuncProps, |
| 124 | + }); |
| 125 | + } else if (existsSync(path.join(__dirname, 'microapps-deployer', 'index.js'))) { |
| 126 | + // This is for built apps packaged with the CDK construct |
| 127 | + this._deployerFunc = new lambda.Function(this, 'deployer-func', { |
| 128 | + code: lambda.Code.fromAsset(path.join(__dirname, 'microapps-deployer')), |
| 129 | + handler: 'index.handler', |
| 130 | + ...deployerFuncProps, |
| 131 | + }); |
| 132 | + } else { |
| 133 | + this._deployerFunc = new lambdaNodejs.NodejsFunction(this, 'deployer-func', { |
| 134 | + entry: path.join(__dirname, '..', '..', 'microapps-deployer', 'src', 'index.ts'), |
| 135 | + handler: 'handler', |
| 136 | + bundling: { |
| 137 | + minify: true, |
| 138 | + sourceMap: true, |
| 139 | + }, |
| 140 | + ...deployerFuncProps, |
| 141 | + }); |
| 142 | + } |
| 143 | + if (removalPolicy !== undefined) { |
| 144 | + this._deployerFunc.applyRemovalPolicy(removalPolicy); |
| 145 | + } |
| 146 | + |
| 147 | + // Grant full control over lambdas that indicate they are microapps |
| 148 | + const policyAPIManageLambdas = new iam.PolicyStatement({ |
| 149 | + effect: iam.Effect.ALLOW, |
| 150 | + actions: ['lambda:*'], |
| 151 | + resources: [ |
| 152 | + `arn:aws:lambda:${Aws.REGION}:${Aws.ACCOUNT_ID}:function:*`, |
| 153 | + `arn:aws:lambda:${Aws.REGION}:${Aws.ACCOUNT_ID}:function:*:*`, |
| 154 | + ], |
| 155 | + conditions: { |
| 156 | + StringEquals: { 'aws:ResourceTag/microapp-managed': 'true' }, |
| 157 | + }, |
| 158 | + }); |
| 159 | + this._deployerFunc.addToRolePolicy(policyAPIManageLambdas); |
| 160 | + } |
| 161 | +} |
0 commit comments