Home > database >  aws cdk passing values to a lambda custom resource
aws cdk passing values to a lambda custom resource

Time:12-09

I need to pass some values from my cdk v2 stack (Python3.8) to a lambda function (Python3.8) which is custom resource that is called when the stack executes.

This is the lambda

def lambda_handler(event, context):
    
    print('lambda executed')
    print('request: {}'.format(json.dumps(event)))

This is how it is wired up in the stack

from constructs import Construct
from aws_cdk import (
    Stack,
    custom_resources as cr,
    aws_lambda as _lambda,
    CustomResource
)

    cust_res_lambda = _lambda.Function(
        self, 'crLambda',
        runtime=_lambda.Runtime.PYTHON_3_8,
        code=_lambda.Code.from_asset('my-resources'),
        handler='lambda.lambda_handler',
        function_name='cr_Lambda'
    )
    
    res_provider = cr.Provider(
        self,'crProvider',
        on_event_handler= cust_res_lambda
    )
    
    CustomResource(self, 'cust_res',service_token= res_provider.service_token)

When the stack runs the lambda executes and I can see the print statements in cloudwatch logs. How do I send some custom values to this lambda function from the stack. Things like a custom string or a json string that holds account number, region and any other stuff I need to send to the lambda?

CodePudding user response:

Pass properties field which map of key values when you are creating CustomResource. The properties will be passed as input in the event object for the lambda. Check the below documentation

https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk/CustomResource.html

  • Related