View a markdown version of this page

PutResourcePolicy搭配使用 AWS SDK - Amazon CloudWatch 日志

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

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 的详细信息,请参阅适用PutResourcePolicy于 Python 的AWS SDK (Boto3) API 参考。

有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将 L CloudWatch ogs 与 AWS SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。