constructor()

in src/constructs/lambda/lambda.ts [117:192]


  constructor(scope: GuStack, id: string, props: GuFunctionProps) {
    const {
      app,
      fileName,
      runtime,
      memorySize,
      timeout,
      bucketNamePath,
      withoutFilePrefix = false,
      withoutArtifactUpload = false,
      loggingFormat = LoggingFormat.JSON,
    } = props;

    const bucketName = bucketNamePath
      ? StringParameter.fromStringParameterName(scope, "bucketoverride", bucketNamePath).stringValue
      : GuDistributionBucketParameter.getInstance(scope).valueAsString;

    const defaultEnvironmentVariables = {
      STACK: scope.stack,
      STAGE: scope.stage,
      APP: app,
    };

    const bucket = Bucket.fromBucketName(scope, `${id}-bucket`, bucketName);
    const objectKey = withoutFilePrefix ? fileName : GuDistributable.getObjectKey(scope, { app }, { fileName });
    const code = Code.fromBucket(bucket, objectKey);
    super(scope, id, {
      ...props,
      loggingFormat,
      environment: {
        ...props.environment,
        ...defaultEnvironmentVariables,
      },
      memorySize: defaultMemorySize(runtime, memorySize),
      timeout: timeout ?? Duration.seconds(30),
      code,
    });

    this.app = app;
    this.fileName = fileName;
    this.bucketNamePath = bucketNamePath;
    this.withoutArtifactUpload = withoutArtifactUpload;
    this.withoutFilePrefix = withoutFilePrefix;

    if (props.enableVersioning) {
      this.alias = new Alias(scope, `${id}-AliasForLambda`, {
        aliasName: scope.stage,
        version: this.currentVersion,
      });
    }

    if (props.errorPercentageMonitoring) {
      new GuLambdaErrorPercentageAlarm(scope, `${id}-ErrorPercentageAlarmForLambda`, {
        ...props.errorPercentageMonitoring,
        lambda: this,
      });
    }

    if (props.throttlingMonitoring) {
      new GuLambdaThrottlingAlarm(scope, `${id}-ThrottlingAlarmForLambda`, {
        ...props.throttlingMonitoring,
        lambda: this,
      });
    }

    bucket.grantRead(this, objectKey);

    const ssmParamReadPolicies: PolicyStatement[] = [
      new ReadParametersByPath(scope, props),
      new ReadParametersByName(scope, props),
    ];

    ssmParamReadPolicies.map((policy) => this.addToRolePolicy(policy));

    AppIdentity.taggedConstruct(props, this);
  }