constructor()

in source/infrastructure/lib/weather-forecast-lambda-ssm-construct.ts [31:74]


  constructor(scope: Construct, id: string, props: WeatherForecastToSSMProps) {
    super(scope, id);

    /** Build WeatherForecast Lambda */
    this._weatherForecastLambda = props.weatherForecastLambda;

    /** Add environment variables to the lambda function */
    this._weatherForecastLambda.addEnvironment(
      'API_PROVIDER',
      Fn.conditionIf(
        props.weatherAPIChosen.logicalId,
        props.weatherAPIProvider,
        Aws.NO_VALUE
      ).toString()
    );
    this._weatherForecastLambda.addEnvironment(
      'SSM_REFERENCE_TO_API_KEY',
      Fn.conditionIf(
        props.weatherAPIChosen.logicalId,
        `${Aws.STACK_NAME}-weather-api-key`,
        Aws.NO_VALUE
      ).toString(),
    );

    /** Add permissions to SSM */
    const SSMPolicy = new Policy(this, 'SSMGet', {
      statements: [
        new PolicyStatement({
          effect: Effect.ALLOW,
          actions: ['ssm:GetParameter'],
          resources: [
            `arn:${Aws.PARTITION}:ssm:${Aws.REGION}:${Aws.ACCOUNT_ID}:parameter/${Aws.STACK_NAME}-weather-api-key`,
          ],
        }),
      ],
    });

    /** Attach SSM Policy to the Lambda's Role */
    this._weatherForecastLambda.role?.attachInlinePolicy(SSMPolicy); //NOSONAR it is a valid expression

    /** Add the WeatherAPIChosen Condition */
    (SSMPolicy.node.defaultChild as CfnPolicy).cfnOptions.condition =
      props.weatherAPIChosen;
  }