简体   繁体   中英

Can I invoke a lambda right after it's created using aws_cdk?

With the code below I can create and deploy a lambda, but I wanted to invoke it every time a deploy is performed. This code runs in github actions workflow.

import os

from aws_cdk import (
    core as cdk,
    aws_lambda as lambda_,
    aws_iam as iam,
    aws_stepfunctions as invoke
)

class DMSAutoBackfill(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        lambda_fn = lambda_.Function(
            self, "DmsAutoFillLambda",
            code=lambda_.Code.from_asset(
                os.path.join(os.path.abspath("."), "lambda_function")
            ),
            handler="create_dms_backfill.event_handler",
            environment={
                'schema_name': os.getenv('schema_name', ''),
                'table_name': os.getenv('table_name', '')
            },
            timeout=cdk.Duration.seconds(ADD_EMR_STEP_LAMBDA_TIMEOUT),
            runtime=lambda_.Runtime.PYTHON_3_7,
        )

I've tried using CustomResource also from aws_cdk but it didn't work, it only runs the lambda after its creation (the first time the cloudformation resources were created) and not after each call of the github actions (where an update was generated on the cloudformation resources).

Update

I chose to create a script using boto3 to invoke the lambda, and call using Github Actions.

I came to the conclusion that the CDK is better to be used to create and manage infra as codes, whereas boto3 is better to operate these created resources.

In case someone finds this question I wanted to show a way to do this using cdk.

You can use AwsCustomResource to invoke AWS commands with your stack.

From the docs: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_custom-resources.AwsCustomResource.html

Defines a custom resource that is materialized using specific AWS API calls.

Use this to bridge any gap that might exist in the CloudFormation Coverage. You can specify exactly which calls are invoked for the 'CREATE', 'UPDATE' and 'DELETE' life cycle events.

This custom resource will spin up a lambda function that executes the aws cli command you specified.

Make sure that you the resource is granted permission to call the function. You can do this by assinging a policy or using the grant helpers ( https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_custom-resources.AwsCustomResource.html#policy ):

The custom resource also implements iam.IGrantable, making it possible to use the grantXxx() methods.

In case if anyone is looking to run a lambda during/after the CDK deployment, it can be done using TriggerFunction

new triggers.TriggerFunction(stack, 'MyTrigger', {
  runtime: lambda.Runtime.NODEJS_12_X,
  handler: 'index.handler',
  code: lambda.Code.fromAsset(__dirname + '/my-trigger'),
});

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_triggers.TriggerFunction.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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