constructor()

in src/index.ts [85:124]


  constructor(scope: cdk.Construct, id: string, props: ServerlessApiProps) {
    super(scope, id);

    const DEFAULT_LAMBDA_ASSET_PATH = path.join(__dirname, '../composer/laravel58-bref');
    const DEFAULT_DB_MASTER_USER = 'admin';

    this.vpc = props.vpc;

    this.handler = props.handler ?? new lambda.Function(this, 'handler', {
      runtime: lambda.Runtime.PROVIDED_AL2,
      handler: 'public/index.php',
      layers: [
        lambda.LayerVersion.fromLayerVersionArn(this, 'BrefPHPLayer', props.brefLayerVersion),
      ],
      code: lambda.Code.fromAsset(props?.lambdaCodePath ?? DEFAULT_LAMBDA_ASSET_PATH),
      environment: {
        APP_STORAGE: '/tmp',
        DB_WRITER: props.databaseConfig?.writerEndpoint ?? '',
        DB_READER: props.databaseConfig?.readerEndpoint ?? props.databaseConfig?.writerEndpoint ?? '',
        DB_USER: props.databaseConfig?.masterUserName ?? DEFAULT_DB_MASTER_USER,
      },
      timeout: cdk.Duration.seconds(120),
      vpc: props.vpc,
    });

    // allow lambda execution role to connect to RDS proxy
    if (props.rdsProxy) {
      this.handler.addToRolePolicy(new iam.PolicyStatement({
        actions: ['rds-db:connect'],
        resources: [props.rdsProxy.dbProxyArn],
      }));
    }

    const endpoint = new apigateway.HttpApi(this, 'apiservice', {
      defaultIntegration: new LambdaProxyIntegration({
        handler: this.handler,
      }),
    });
    new cdk.CfnOutput(this, 'EndpointURL', { value: endpoint.url! });
  }