简体   繁体   English

aws cdk 将值传递给 lambda 自定义资源

[英]aws cdk passing values to a lambda custom resource

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.我需要将一些值从我的 cdk v2 堆栈 (Python3.8) 传递到 lambda function (Python3.8),这是堆栈执行时调用的自定义资源。

This is the lambda这是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.当堆栈运行 lambda 时,我可以在 cloudwatch 日志中看到打印语句。 How do I send some custom values to this lambda function from the stack.如何从堆栈向此 lambda function 发送一些自定义值。 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?诸如自定义字符串或 json 字符串之类的东西,其中包含帐号、区域和我需要发送到 lambda 的任何其他内容?

Pass properties field which map of key values when you are creating CustomResource.在创建 CustomResource 时传递 map 个键值的属性字段。 The properties will be passed as input in the event object for the lambda. Check the below documentation对于 lambda,属性将作为事件 object 的输入传递。检查以下文档

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM