View a markdown version of this page

PutResourcePolicy 搭配 AWS SDK 使用 - Amazon CloudWatch Logs

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

PutResourcePolicy 搭配 AWS SDK 使用

以下程式碼範例顯示如何使用 PutResourcePolicy。

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

Python
適用於 Python 的 SDK (Boto3)
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

def put_resource_policy( self, log_group_arn: str, vpc_endpoint_id: str, ) -> dict[str, Any]: """ Creates or updates a resource-scoped policy on a log group that grants the syslog.logs.amazonaws.com service principal permission to call logs:PutLogEvents and logs:CreateLogStream. A resource-scoped policy (one attached to a specific log group) is specified with resourceArn and policyDocument. The PutResourcePolicy API does not allow policyName to be combined with resourceArn. :param log_group_arn: The ARN of the log group (with trailing :*). :param vpc_endpoint_id: The VPC endpoint ID to scope the condition. :return: The resource policy response. """ policy_document = json.dumps( { "Version":"2012-10-17", "Statement": [ { "Sid": "SyslogIngestPermissions", "Effect": "Allow", "Principal": {"Service": "syslog.logs.amazonaws.com"}, "Action": [ "logs:PutLogEvents", "logs:CreateLogStream", ], "Resource": log_group_arn, "Condition": { "StringEquals": {"aws:sourceVpce": vpc_endpoint_id} }, } ], } ) try: response = self.logs_client.put_resource_policy( policyDocument=policy_document, resourceArn=log_group_arn, ) logger.info("Put resource policy on log group '%s'.", log_group_arn) return response except ClientError as error: logger.error( "Failed to put resource policy: %s", error.response["Error"]["Message"], ) raise
  • 如需 API 詳細資訊,請參閱《適用於 AWS Python (Boto3) 的 SDK API 參考》中的 PutResourcePolicy。

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭配 AWS SDK 使用 CloudWatch Logs。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。