2

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?

user20358
  • 14,182
  • 36
  • 114
  • 186
  • 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/v1/python/aws_cdk.core/CustomResource.html just a check this is for cdk V1? – Vikram S Dec 08 '22 at 00:15
  • 1
    https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk/CustomResource.html V2 also has similar field called properties – Vikram S Dec 08 '22 at 00:45

1 Answers1

2

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

Vikram S
  • 792
  • 4
  • 7