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.
Lambda can be triggered 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'),
});
Node JS Docs: https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_triggers.TriggerFunction.html
Python Docs: https://docs.aws.amazon.com/cdk/api/v1/python/aws_cdk.triggers/Trigger.html#trigger