Home > OS >  CDK: How to modify lambda instance which is imported from Lambda base class?
CDK: How to modify lambda instance which is imported from Lambda base class?

Time:12-19

I am trying to add managed policy to the lambda which is being instantiated from the imported lambda function class, but I am unable to do so as it used error like

Property 'role' does not exist on type 'SlackNotificationLambda'.

 Base class

export class SlackNotificationLambda extends cdk.Stack {
    public readonly lambda: NodejsFunction;
    constructor(scope: cdk.App, id: string, props?: LambdaProps) {
        super(scope, id, props);

        this.lambda = new NodejsFunction(this, 'slack-notification-lambda', {
            memorySize: 1024,
            timeout: cdk.Duration.seconds(10),
            runtime: lambda.Runtime.NODEJS_16_X,
            handler: 'main',
            entry: path.join(__dirname, props.lambdaPath)
        });

   }
}

File where instance is being created

import { SlackNotificationLambda } from '../lib/pipeline-slack-notifications-lambda'

const pipelineApprovalHandler = new SlackNotificationLambda(app, 'slack-approval-handler', {
    lambdaPath: '../slack-notifications-lambda/src/approval-handler.ts'
})
pipelineApprovalHandler.role?.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AWSCodePipelineApproverAccess'))

CodePudding user response:

You want to reference the stack's Lambda Function, but your code is referencing the Stack instance.

pipelineApprovalHandler.lambda.role?.addManagedPolicy(...)

or

const { lambda } = new SlackNotificationLambda(...)
lambda.role?.addManagedPolicy(...)
  • Related