|
| 1 | +import * as aws from '@pulumi/aws'; |
| 2 | +import * as pulumi from '@pulumi/pulumi'; |
| 3 | +import { ISetup } from '../utils/Setup'; |
| 4 | + |
| 5 | +export function createEscoApiLambdaFunctionUrl(setup: ISetup, codesetsUrl: pulumi.Output<string>) { |
| 6 | + const escoApiLambdaFunctionExecRoleConfig = setup.getResourceConfig('EscoApiLambdaFunctionExecRole'); |
| 7 | + const escoApiLambdaFunctionExecRoleRole = new aws.iam.Role(escoApiLambdaFunctionExecRoleConfig.name, { |
| 8 | + assumeRolePolicy: JSON.stringify({ |
| 9 | + Version: '2012-10-17', |
| 10 | + Statement: [ |
| 11 | + { |
| 12 | + Action: 'sts:AssumeRole', |
| 13 | + Principal: { |
| 14 | + Service: ['lambda.amazonaws.com'], |
| 15 | + }, |
| 16 | + Effect: 'Allow', |
| 17 | + }, |
| 18 | + ], |
| 19 | + }), |
| 20 | + tags: escoApiLambdaFunctionExecRoleConfig.tags, |
| 21 | + }); |
| 22 | + |
| 23 | + const escoApiLambdaFunctionExecRolePolicy = setup.getResourceConfig('EscoApiLambdaFunctionExecRolePolicy'); |
| 24 | + new aws.iam.RolePolicy(escoApiLambdaFunctionExecRolePolicy.name, { |
| 25 | + role: escoApiLambdaFunctionExecRoleRole.id, |
| 26 | + policy: JSON.stringify({ |
| 27 | + Version: '2012-10-17', |
| 28 | + Statement: [ |
| 29 | + { |
| 30 | + Action: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'], |
| 31 | + Resource: 'arn:aws:logs:*:*:*', |
| 32 | + Effect: 'Allow', |
| 33 | + }, |
| 34 | + ], |
| 35 | + }), |
| 36 | + }); |
| 37 | + |
| 38 | + const escoApiConfig = new pulumi.Config('escoApi'); |
| 39 | + const escoApiRegionConfig = setup.getResourceConfig('EscoApiRegion'); |
| 40 | + const escoApiRegion = new aws.Provider(escoApiRegionConfig.name, { |
| 41 | + region: escoApiConfig.require('awsRegion') as aws.Region, |
| 42 | + }); |
| 43 | + |
| 44 | + const escoApiLambdaFunctionConfig = setup.getResourceConfig('EscoApiLambdaFunction'); |
| 45 | + const escoApiLambdaFunction = new aws.lambda.Function( |
| 46 | + escoApiLambdaFunctionConfig.name, |
| 47 | + { |
| 48 | + role: escoApiLambdaFunctionExecRoleRole.arn, |
| 49 | + runtime: 'nodejs18.x', |
| 50 | + handler: 'index.handler', |
| 51 | + timeout: 30, |
| 52 | + memorySize: 256, |
| 53 | + code: new pulumi.asset.FileArchive('./dist/escoApi'), |
| 54 | + tags: escoApiLambdaFunctionConfig.tags, |
| 55 | + environment: { |
| 56 | + variables: { |
| 57 | + CODESETS_API_ENDPOINT: pulumi.interpolate`${codesetsUrl}`, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { provider: escoApiRegion } |
| 62 | + ); |
| 63 | + |
| 64 | + const escoApiLambdaFunctionUrlConfig = setup.getResourceConfig('EscoApiLambdaFunctionUrl'); |
| 65 | + const escoApiLambdaFunctionUrl = new aws.lambda.FunctionUrl( |
| 66 | + escoApiLambdaFunctionUrlConfig.name, |
| 67 | + { |
| 68 | + functionName: escoApiLambdaFunction.name, |
| 69 | + authorizationType: 'NONE', |
| 70 | + cors: { |
| 71 | + allowCredentials: false, |
| 72 | + allowOrigins: ['*'], |
| 73 | + allowMethods: ['POST'], |
| 74 | + }, |
| 75 | + }, |
| 76 | + { provider: escoApiRegion } |
| 77 | + ); |
| 78 | + |
| 79 | + // Warmup scheduler for lambda function |
| 80 | + if (setup.isProductionLikeEnvironment()) { |
| 81 | + setupLambdaWarmerScheduler(setup, escoApiLambdaFunction, escoApiRegion); |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + lambdaFunctionExecRoleRole: escoApiLambdaFunctionExecRoleRole, |
| 86 | + lambdaFunction: escoApiLambdaFunction, |
| 87 | + lambdaFunctionUrl: escoApiLambdaFunctionUrl, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +function setupLambdaWarmerScheduler( |
| 92 | + setup: ISetup, |
| 93 | + escoApiLambdaFunction: aws.lambda.Function, |
| 94 | + escoApiRegion: aws.Provider |
| 95 | +) { |
| 96 | + const escoApiWarmupSchedulerEventConfig = setup.getResourceConfig('EscoApiWarmupSchedulerEvent'); |
| 97 | + const escoApiWarmupSchedulerEvent = new aws.cloudwatch.EventRule( |
| 98 | + escoApiWarmupSchedulerEventConfig.name, |
| 99 | + { |
| 100 | + scheduleExpression: 'rate(5 minutes)', |
| 101 | + description: 'Warmup scheduler for Esco API lambda function', |
| 102 | + tags: escoApiWarmupSchedulerEventConfig.tags, |
| 103 | + }, |
| 104 | + { provider: escoApiRegion } |
| 105 | + ); |
| 106 | + |
| 107 | + const escoApiWarmupSchedulerTargetConfig = setup.getResourceConfig('EscoApiWarmupSchedulerTarget'); |
| 108 | + new aws.cloudwatch.EventTarget( |
| 109 | + escoApiWarmupSchedulerTargetConfig.name, |
| 110 | + { |
| 111 | + rule: escoApiWarmupSchedulerEvent.name, |
| 112 | + arn: escoApiLambdaFunction.arn, |
| 113 | + input: JSON.stringify({ source: 'warmup' }), |
| 114 | + }, |
| 115 | + { provider: escoApiRegion } |
| 116 | + ); |
| 117 | + |
| 118 | + // Permission for the warmup scheduler to invoke the lambda function |
| 119 | + const escoApiWarmupSchedulerPermissionConfig = setup.getResourceConfig('EscoApiWarmupSchedulerPermission'); |
| 120 | + new aws.lambda.Permission( |
| 121 | + escoApiWarmupSchedulerPermissionConfig.name, |
| 122 | + { |
| 123 | + action: 'lambda:InvokeFunction', |
| 124 | + function: escoApiLambdaFunction.name, |
| 125 | + principal: 'events.amazonaws.com', |
| 126 | + sourceArn: escoApiWarmupSchedulerEvent.arn, |
| 127 | + }, |
| 128 | + { provider: escoApiRegion } |
| 129 | + ); |
| 130 | +} |
0 commit comments