Lambda の Invoke イベントを記録する CloudTrail の証跡を AWS CLI で作成してみた
Logging AWS Lambda API calls using AWS CloudTrail - AWS Lambda
デフォルトでは記録されない Lambda の Invoke イベントを記録する設定を AWS CLI でやってみました。
前提
- 記録対象の Lambda 関数は作成済み
- AWS CLI の実行環境は CloudShell
S3 バケットの作成
CloudTrail の証跡を保存する S3 バケットを作成します。
create-bucket — AWS CLI 2.24.24 Command Reference
$ aws s3api create-bucket \
--bucket test-trail-20250317 \
--create-bucket-configuration LocationConstraint=ap-northeast-1
{
"Location": "http://test-trail-20250317.s3.amazonaws.com/"
}
次にバケットポリシーを設定します。
パケットポリシーのテンプレートは以下のドキュメントに記載されているので、バケット名やアカウント ID を置換して利用します。
Amazon S3 bucket policy for CloudTrail - AWS CloudTrail
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailAclCheck20150319",
"Effect": "Allow",
"Principal": {"Service": "cloudtrail.amazonaws.com"},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn:aws:cloudtrail:region:myAccountID:trail/trailName"
}
}
},
{
"Sid": "AWSCloudTrailWrite20150319",
"Effect": "Allow",
"Principal": {"Service": "cloudtrail.amazonaws.com"},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/[optionalPrefix]/AWSLogs/myAccountID/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control",
"aws:SourceArn": "arn:aws:cloudtrail:region:myAccountID:trail/trailName"
}
}
}
]
}
$ nano policy.json
# 各環境に合わせて編集した上記 JSON を書き込みます。
最初に作成した S3 バケットにバケットポリシーを追加します。
put-bucket-policy — AWS CLI 2.24.24 Command Reference
$ aws s3api put-bucket-policy \
--bucket test-trail-20250317 \
--policy file://policy.json
CloudTrail 証跡の作成
作成した S3 バケットを指定して証跡をを作成します。
create-trail — AWS CLI 2.24.24 Command Reference
$ aws cloudtrail create-trail \
--name lambda-trail \
--s3-bucket-name test-trail-20250317
{
"Name": "lambda-trail",
"S3BucketName": "test-trail-20250317",
"IncludeGlobalServiceEvents": true,
"IsMultiRegionTrail": false,
"TrailARN": "arn:aws:cloudtrail:ap-northeast-1:012345678901:trail/lambda-trail",
"LogFileValidationEnabled": false,
"IsOrganizationTrail": false
}
Lambda のデータイベントを記録する設定を行います。
put-event-selectors — AWS CLI 2.24.24 Command Reference
$ aws cloudtrail put-event-selectors \
--trail-name lambda-trail \
--event-selectors '[{
"ReadWriteType": "All",
"IncludeManagementEvents": true,
"DataResources": [{
"Type": "AWS::Lambda::Function",
"Values": ["arn:aws:lambda:ap-northeast-1:172454386805:function:my-function"]
}]
}]'
{
"TrailARN": "arn:aws:cloudtrail:ap-northeast-1:012345678901:trail/lambda-trail",
"EventSelectors": [
{
"ReadWriteType": "All",
"IncludeManagementEvents": true,
"DataResources": [
{
"Type": "AWS::Lambda::Function",
"Values": [
"arn:aws:lambda:ap-northeast-1:012345678901:function:my-function"
]
}
],
"ExcludeManagementEventSources": []
}
]
}
証跡のログ記録を有効化します。
[start-logging — AWS CLI 2.24.24 Command Reference](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudtrail/start-logging.html)
```bash
$ aws cloudtrail start-logging \
--name lambda-trail
これで証跡を設定できました。
設定の確認
証跡の設定を確認します。
get-trail — AWS CLI 2.24.24 Command Reference
$ aws cloudtrail get-trail \
--name lambda-trail
{
"Trail": {
"Name": "lambda-trail",
"S3BucketName": "test-trail-20250317",
"IncludeGlobalServiceEvents": true,
"IsMultiRegionTrail": false,
"HomeRegion": "ap-northeast-1",
"TrailARN": "arn:aws:cloudtrail:ap-northeast-1:012345678901:trail/lambda-trail",
"LogFileValidationEnabled": false,
"HasCustomEventSelectors": true,
"HasInsightSelectors": false,
"IsOrganizationTrail": false
}
}
get-trail-status — AWS CLI 2.24.24 Command Reference
$ aws cloudtrail get-trail-status \
--name lambda-trail
{
"IsLogging": true,
"StartLoggingTime": "2025-03-17T11:59:58.782000+00:00",
"LatestDeliveryAttemptTime": "",
"LatestNotificationAttemptTime": "",
"LatestNotificationAttemptSucceeded": "",
"LatestDeliveryAttemptSucceeded": "",
"TimeLoggingStarted": "2025-03-17T11:59:58Z",
"TimeLoggingStopped": ""
}
コンソールからも証跡が作成されていることや Lambda のデータイベントを記録する設定になっていることを確認できます。
Lambda の Invoke イベントを確認してみる
以下コマンドで CloudShell から関数をテスト実行し、証跡が保存されている S3 バケットから Invoke イベントの記録を確認してみます。
$ aws lambda invoke \
--function-name my-function \
response.json
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
関数実行を実行して数分後に証跡が保存されている S3 バケットから最新のファイルをダウンロードしてテキストエディタなどで Invoke イベントを確認できれば記録成功です。
Invoke イベント
{
"Records": [
{
"eventVersion": "1.11",
"userIdentity": {
"type": "AssumedRole",
"principalId": "xxx",
"arn": "xxx",
"accountId": "012345678901",
"accessKeyId": "xxx",
"sessionContext": {
"sessionIssuer": {
"type": "Role",
"principalId": "xxx",
"arn": "xxx",
"accountId": "012345678901",
"userName": "xxx"
},
"attributes": {
"creationDate": "2025-03-17T11:29:02Z",
"mfaAuthenticated": "true"
}
}
},
"eventTime": "2025-03-17T12:15:46Z",
"eventSource": "lambda.amazonaws.com",
"eventName": "Invoke",
"awsRegion": "ap-northeast-1",
"sourceIPAddress": "xxx",
"userAgent": "aws-cli/2.24.21 md/awscrt#0.23.8 ua/2.1 os/linux#6.1.128-136.201.amzn2023.x86_64 md/arch#x86_64 lang/python#3.12.9 md/pyimpl#CPython exec-env/CloudShell cfg/retry-mode#standard md/installer#exe md/distrib#amzn.2023 md/prompt#off md/command#lambda.invoke",
"requestParameters": {
"functionName": "arn:aws:lambda:ap-northeast-1:012345678901:function:my-function"
},
"responseElements": null,
"additionalEventData": {
"functionVersion": "arn:aws:lambda:ap-northeast-1:012345678901:function:my-function:$LATEST"
},
"requestID": "ad815ec7-c750-4936-bdfc-9b3b91b10d19",
"eventID": "c02c7568-47f5-4845-8147-1df83703b53c",
"readOnly": false,
"resources": [
{
"accountId": "012345678901",
"type": "AWS::Lambda::Function",
"ARN": "arn:aws:lambda:ap-northeast-1:012345678901:function:my-function"
}
],
"eventType": "AwsApiCall",
"managementEvent": false,
"recipientAccountId": "012345678901",
"eventCategory": "Data",
"tlsDetails": {
"tlsVersion": "TLSv1.3",
"clientProvidedHostHeader": "lambda.ap-northeast-1.amazonaws.com"
},
"sessionCredentialFromConsole": "true"
}
]
}
参考資料
- Logging AWS Lambda API calls using AWS CloudTrail - AWS Lambda
- create-bucket — AWS CLI 2.24.24 Command Reference
- Amazon S3 bucket policy for CloudTrail - AWS CloudTrail
- put-bucket-policy — AWS CLI 2.24.24 Command Reference
- create-trail — AWS CLI 2.24.24 Command Reference
- put-event-selectors — AWS CLI 2.24.24 Command Reference
- get-trail — AWS CLI 2.24.24 Command Reference
- get-trail-status — AWS CLI 2.24.24 Command Reference
- Creating, updating, and managing trails with the AWS CLI - AWS CloudTrail
Discussion