constructor()

in source/infrastructure/lib/custom-resources.ts [79:147]


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

    this.sourceCodeBucket = props.solutionConfig.sourceCodeBucket;
    this.sourceCodePrefix = props.solutionConfig.sourceCodePrefix;

    this.helperFunctionRole = new Role(this, 'HelperFunctionRole', {
      assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
      path: '/',
      inlinePolicies: {
        'GreengrassIoTPolicy': new PolicyDocument({
          statements: [
            new PolicyStatement({
              effect: Effect.ALLOW,
              actions: ['iot:DescribeEndpoint'],
              resources: ['*']
            })
          ]
        })
      }
    });
    this.helperFunctionRole.attachInlinePolicy(props.cloudWatchLogsPolicy);
    addCfnSuppressRules(this.helperFunctionRole, [
      { id: 'W11', reason: 'iot:DescribeEndpoint cannot specify the resource.' }
    ]);

    this.helperFunction = new LambdaFunction(this, 'HelperFunction', {
      description: 'Machine to Cloud Connectivity custom resource function',
      handler: 'custom-resource/index.handler',
      runtime: Runtime.NODEJS_14_X,
      code: Code.fromBucket(this.sourceCodeBucket, `${this.sourceCodePrefix}/custom-resource.zip`),
      timeout: Duration.seconds(240),
      role: this.helperFunctionRole,
      environment: {
        LOGGING_LEVEL: props.solutionConfig.loggingLevel,
        SOLUTION_ID: props.solutionConfig.solutionId,
        SOLUTION_VERSION: props.solutionConfig.solutionVersion
      }
    });
    (this.helperFunction.node.defaultChild as CfnFunction).addDependsOn(props.cloudWatchLogsPolicy.node.defaultChild as CfnPolicy);

    const customUuid = new CustomResource(this, 'UUID', {
      serviceToken: this.helperFunction.functionArn,
      properties: {
        Resource: 'CreateUUID'
      }
    });
    this.uuid = customUuid.getAtt('UUID').toString();

    const sendAnonymousMetrics = new CustomResource(this, 'SendAnonymousMetrics', {
      serviceToken: this.helperFunction.functionArn,
      properties: {
        Resource: 'SendAnonymousMetrics',
        ExistingGreengrassGroup: props.existingGreengrassGroup,
        ExistingKinesisStream: props.existingKinesisStream,
        SolutionUUID: this.uuid
      }
    });
    const cfnSendAnonymousMetrics = sendAnonymousMetrics.node.defaultChild as CfnCustomResource;
    cfnSendAnonymousMetrics.cfnOptions.condition = props.sendAnonymousUsageCondition;

    const describeIoTEndpoint = new CustomResource(this, 'DescribeIoTEndpoint', {
      serviceToken: this.helperFunction.functionArn,
      properties: {
        Resource: 'DescribeIoTEndpoint'
      }
    });
    this.iotEndpointAddress = describeIoTEndpoint.getAtt('IOT_ENDPOINT').toString();
  }