Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Gunakan PutResourcePolicydengan AWS SDK
Contoh kode berikut menunjukkan cara menggunakanPutResourcePolicy.
Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:
- Python
-
- SDK untuk Python (Boto3)
-
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode 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
Untuk daftar lengkap panduan pengembang AWS SDK dan contoh kode, lihatMenggunakan CloudWatch Log dengan AWS SDK. Topik ini juga mencakup informasi tentang memulai dan detail tentang versi SDK sebelumnya.