11import { App , assertions , Stack } from "aws-cdk-lib"
22import { Template , Match } from "aws-cdk-lib/assertions"
3- import { ManagedPolicy , PolicyStatement , Role } from "aws-cdk-lib/aws-iam"
3+ import {
4+ ManagedPolicy ,
5+ PolicyStatement ,
6+ Role ,
7+ ServicePrincipal
8+ } from "aws-cdk-lib/aws-iam"
49import { LogGroup } from "aws-cdk-lib/aws-logs"
510import {
611 Architecture ,
@@ -17,6 +22,8 @@ import {
1722} from "vitest"
1823
1924import { PythonLambdaFunction } from "../../src/constructs/PythonLambdaFunction"
25+ import { CfnDeliveryStream } from "aws-cdk-lib/aws-kinesisfirehose"
26+ import { Key } from "aws-cdk-lib/aws-kms"
2027
2128describe ( "pythonFunctionConstruct works correctly" , ( ) => {
2229 let stack : Stack
@@ -400,3 +407,124 @@ describe("pythonFunctionConstruct works correctly with addSplunkSubscriptionFilt
400407 template . resourceCountIs ( "AWS::Logs::SubscriptionFilter" , 0 )
401408 } )
402409} )
410+
411+ describe ( "pythonFunctionConstruct works correctly when not using imports" , ( ) => {
412+ let stack : Stack
413+ let app : App
414+ let template : assertions . Template
415+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
416+ let lambdaLogGroupResource : any
417+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
418+ let cloudWatchLogsKmsKeyResource : any
419+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
420+ let lambdaInsightsLogGroupPolicyResource : any
421+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
422+ let cloudwatchEncryptionKMSPolicyResource : any
423+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
424+ let splunkSubscriptionFilterRoleResource : any
425+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
426+ let splunkDeliveryStreamResource : any
427+
428+ beforeAll ( ( ) => {
429+ app = new App ( )
430+ stack = new Stack ( app , "pythonLambdaConstructStack" )
431+ const cloudWatchLogsKmsKey = new Key ( stack , "cloudWatchLogsKmsKey" )
432+ const cloudwatchEncryptionKMSPolicy = new ManagedPolicy ( stack , "cloudwatchEncryptionKMSPolicy" , {
433+ description : "cloudwatch encryption KMS policy" ,
434+ statements : [
435+ new PolicyStatement ( {
436+ actions : [
437+ "kms:Decrypt" ,
438+ "kms:Encrypt" ,
439+ "kms:GenerateDataKey*" ,
440+ "kms:ReEncrypt*"
441+ ] ,
442+ resources : [ "*" ]
443+ } ) ]
444+ } )
445+ const splunkDeliveryStream = new CfnDeliveryStream ( stack , "SplunkDeliveryStream" , {
446+ deliveryStreamName : "SplunkDeliveryStream" ,
447+ s3DestinationConfiguration : {
448+ bucketArn : "arn:aws:s3:::my-bucket" ,
449+ roleArn : "arn:aws:iam::123456789012:role/my-role"
450+ }
451+ } )
452+ const splunkSubscriptionFilterRole = new Role ( stack , "SplunkSubscriptionFilterRole" , {
453+ assumedBy : new ServicePrincipal ( "logs.amazonaws.com" )
454+ } )
455+ const lambdaInsightsLogGroupPolicy = new ManagedPolicy ( stack , "LambdaInsightsLogGroupPolicy" , {
456+ description : "permissions to create log group and set retention policy for Lambda Insights" ,
457+ statements : [
458+ new PolicyStatement ( {
459+ actions : [
460+ "logs:CreateLogStream" ,
461+ "logs:PutLogEvents"
462+ ] ,
463+ resources : [
464+ "*"
465+ ]
466+ } )
467+ ]
468+ } )
469+
470+ const functionConstruct = new PythonLambdaFunction ( stack , "dummyPythonFunction" , {
471+ functionName : "testPythonLambda" ,
472+ projectBaseDir : resolve ( __dirname , "../../../.." ) ,
473+ packageBasePath : "packages/cdkConstructs" ,
474+ handler : "index.handler" ,
475+ environmentVariables : { } ,
476+ logRetentionInDays : 30 ,
477+ logLevel : "INFO" ,
478+ cloudWatchLogsKmsKey : cloudWatchLogsKmsKey ,
479+ cloudwatchEncryptionKMSPolicy : cloudwatchEncryptionKMSPolicy ,
480+ splunkDeliveryStream : splunkDeliveryStream ,
481+ splunkSubscriptionFilterRole : splunkSubscriptionFilterRole ,
482+ lambdaInsightsLogGroupPolicy : lambdaInsightsLogGroupPolicy
483+ } )
484+ template = Template . fromStack ( stack )
485+ const lambdaLogGroup = functionConstruct . node . tryFindChild ( "LambdaLogGroup" ) as LogGroup
486+ lambdaLogGroupResource = stack . resolve ( lambdaLogGroup . logGroupName )
487+ cloudWatchLogsKmsKeyResource = stack . resolve ( cloudWatchLogsKmsKey . keyId )
488+ lambdaInsightsLogGroupPolicyResource = stack . resolve ( lambdaInsightsLogGroupPolicy . managedPolicyArn )
489+ cloudwatchEncryptionKMSPolicyResource = stack . resolve ( cloudwatchEncryptionKMSPolicy . managedPolicyArn )
490+ splunkSubscriptionFilterRoleResource = stack . resolve ( splunkSubscriptionFilterRole . roleName )
491+ splunkDeliveryStreamResource = stack . resolve ( splunkDeliveryStream . ref )
492+ } )
493+
494+ test ( "it has the correct cloudWatchLogsKmsKey" , ( ) => {
495+ template . hasResourceProperties ( "AWS::Logs::LogGroup" , {
496+ LogGroupName : "/aws/lambda/testPythonLambda" ,
497+ KmsKeyId : { "Fn::GetAtt" : [ cloudWatchLogsKmsKeyResource . Ref , "Arn" ] } ,
498+ RetentionInDays : 30
499+ } )
500+ } )
501+
502+ test ( "it has the correct cloudwatchEncryptionKMSPolicy and lambdaInsightsLogGroupPolicy" , ( ) => {
503+ template . hasResourceProperties ( "AWS::IAM::Role" , {
504+ "AssumeRolePolicyDocument" : {
505+ "Statement" : [
506+ {
507+ "Action" : "sts:AssumeRole" ,
508+ "Effect" : "Allow" ,
509+ "Principal" : {
510+ "Service" : "lambda.amazonaws.com"
511+ }
512+ }
513+ ] ,
514+ "Version" : "2012-10-17"
515+ } ,
516+ "ManagedPolicyArns" : Match . arrayWith ( [
517+ { "Ref" : lambdaInsightsLogGroupPolicyResource . Ref } ,
518+ { "Ref" : cloudwatchEncryptionKMSPolicyResource . Ref }
519+ ] )
520+ } )
521+ } )
522+ test ( "it has the correct subscription filter" , ( ) => {
523+ template . hasResourceProperties ( "AWS::Logs::SubscriptionFilter" , {
524+ LogGroupName : { "Ref" : lambdaLogGroupResource . Ref } ,
525+ FilterPattern : "" ,
526+ RoleArn : { "Fn::GetAtt" : [ splunkSubscriptionFilterRoleResource . Ref , "Arn" ] } ,
527+ DestinationArn : { "Fn::GetAtt" : [ splunkDeliveryStreamResource . Ref , "Arn" ] }
528+ } )
529+ } )
530+ } )
0 commit comments