CloudTrail captures API calls for AWS Lambda as events. The calls captured include calls from the AWS Lambda console and code calls to the AWS Lambda API operations.
To remediate the misconfiguration “CloudTrail should be enabled for AWS Lambda” in AWS, follow these steps:
Go to the AWS Management Console and log in to your account.
Open the AWS Lambda console.
Select the function for which you want to enable CloudTrail.
Click on the “Configuration” tab.
Scroll down to the “Advanced settings” section and click on “Edit”.
In the “CloudWatch Logs” section, select “Enable CloudWatch Logs”.
In the “CloudTrail” section, select “Enable CloudTrail logs”.
Choose the S3 bucket where you want to store the CloudTrail logs.
Click on “Save”.
Once you complete these steps, CloudTrail will be enabled for AWS Lambda and the logs will be saved to the specified S3 bucket. This will help you to monitor and audit the actions performed on your AWS Lambda functions, which is important for security and compliance purposes.
Create a new CloudTrail trail if one does not exist.
Copy
Ask AI
if not trail_arn: cloudtrail_client.create_trail(Name='MyCloudTrail') trail_arn = cloudtrail_client.describe_trails()['trailList'][0]['TrailARN']
Check if CloudTrail is enabled for the AWS Lambda function.
Copy
Ask AI
lambda_function_name = 'MyLambdaFunction'response = lambda_client.get_function_configuration(FunctionName=lambda_function_name)if 'CloudWatchLogsLogGroupArn' in response: cloud_watch_logs_arn = response['CloudWatchLogsLogGroupArn'] if cloud_watch_logs_arn not in trail_arn: cloudtrail_client.update_trail( Name='MyCloudTrail', CloudWatchLogsLogGroupArn=cloud_watch_logs_arn, CloudWatchLogsRoleArn='arn:aws:iam::123456789012:role/MyCloudTrailRole' )else: print(f'CloudTrail is not enabled for Lambda function {lambda_function_name}')
If CloudTrail is not enabled for the AWS Lambda function, enable it by adding the CloudWatch log group ARN to the CloudTrail trail.
Copy
Ask AI
lambda_function_name = 'MyLambdaFunction'response = lambda_client.get_function_configuration(FunctionName=lambda_function_name)if 'CloudWatchLogsLogGroupArn' in response: cloud_watch_logs_arn = response['CloudWatchLogsLogGroupArn'] if cloud_watch_logs_arn not in trail_arn: cloudtrail_client.update_trail( Name='MyCloudTrail', CloudWatchLogsLogGroupArn=cloud_watch_logs_arn, CloudWatchLogsRoleArn='arn:aws:iam::123456789012:role/MyCloudTrailRole' )else: print(f'CloudTrail is not enabled for Lambda function {lambda_function_name}')
Verify that CloudTrail is now enabled for the AWS Lambda function.
Copy
Ask AI
response = lambda_client.get_function_configuration(FunctionName=lambda_function_name)if 'CloudWatchLogsLogGroupArn' in response: cloud_watch_logs_arn = response['CloudWatchLogsLogGroupArn'] if cloud_watch_logs_arn in trail_arn: print(f'CloudTrail is enabled for Lambda function {lambda_function_name}')else: print(f'CloudTrail is not enabled for Lambda function {lambda_function_name}')
These steps will enable CloudTrail for the AWS Lambda function and ensure that logs are being captured.