CloudWatchLogs / Client / put_account_policy
put_account_policy#
- CloudWatchLogs.Client.put_account_policy(**kwargs)#
Creates an account-level data protection policy or subscription filter policy that applies to all log groups or a subset of log groups in the account.
Data protection policy
A data protection policy can help safeguard sensitive data that’s ingested by your log groups by auditing and masking the sensitive log data. Each account can have only one account-level data protection policy.
Warning
Sensitive data is detected and masked when it is ingested into a log group. When you set a data protection policy, log events ingested into the log groups before that time are not masked.
If you use
PutAccountPolicyto create a data protection policy for your whole account, it applies to both existing log groups and all log groups that are created later in this account. The account-level policy is applied to existing log groups with eventual consistency. It might take up to 5 minutes before sensitive data in existing log groups begins to be masked.By default, when a user views a log event that includes masked data, the sensitive data is replaced by asterisks. A user who has the
logs:Unmaskpermission can use a GetLogEvents or FilterLogEvents operation with theunmaskparameter set totrueto view the unmasked log events. Users with thelogs:Unmaskcan also view unmasked data in the CloudWatch Logs console by running a CloudWatch Logs Insights query with theunmaskquery command.For more information, including a list of types of data that can be audited and masked, see Protect sensitive log data with masking.
To use the
PutAccountPolicyoperation for a data protection policy, you must be signed on with thelogs:PutDataProtectionPolicyandlogs:PutAccountPolicypermissions.The
PutAccountPolicyoperation applies to all log groups in the account. You can use PutDataProtectionPolicy to create a data protection policy that applies to just one log group. If a log group has its own data protection policy and the account also has an account-level data protection policy, then the two policies are cumulative. Any sensitive term specified in either policy is masked.Subscription filter policy
A subscription filter policy sets up a real-time feed of log events from CloudWatch Logs to other Amazon Web Services services. Account-level subscription filter policies apply to both existing log groups and log groups that are created later in this account. Supported destinations are Kinesis Data Streams, Firehose, and Lambda. When log events are sent to the receiving service, they are Base64 encoded and compressed with the GZIP format.
The following destinations are supported for subscription filters:
An Kinesis Data Streams data stream in the same account as the subscription policy, for same-account delivery.
An Firehose data stream in the same account as the subscription policy, for same-account delivery.
A Lambda function in the same account as the subscription policy, for same-account delivery.
A logical destination in a different account created with PutDestination, for cross-account delivery. Kinesis Data Streams and Firehose are supported as logical destinations.
Each account can have one account-level subscription filter policy per Region. If you are updating an existing filter, you must specify the correct name in
PolicyName. To perform aPutAccountPolicysubscription filter operation for any destination except a Lambda function, you must also have theiam:PassRolepermission.See also: AWS API Documentation
Request Syntax
response = client.put_account_policy( policyName='string', policyDocument='string', policyType='DATA_PROTECTION_POLICY'|'SUBSCRIPTION_FILTER_POLICY', scope='ALL', selectionCriteria='string' )
- Parameters:
policyName (string) –
[REQUIRED]
A name for the policy. This must be unique within the account.
policyDocument (string) –
[REQUIRED]
Specify the policy, in JSON.
Data protection policy
A data protection policy must include two JSON blocks:
The first block must include both a
DataIdentiferarray and anOperationproperty with anAuditaction. TheDataIdentiferarray lists the types of sensitive data that you want to mask. For more information about the available options, see Types of data that you can mask. TheOperationproperty with anAuditaction is required to find the sensitive data terms. ThisAuditaction must contain aFindingsDestinationobject. You can optionally use thatFindingsDestinationobject to list one or more destinations to send audit findings to. If you specify destinations such as log groups, Firehose streams, and S3 buckets, they must already exist.The second block must include both a
DataIdentiferarray and anOperationproperty with anDeidentifyaction. TheDataIdentiferarray must exactly match theDataIdentiferarray in the first block of the policy. TheOperationproperty with theDeidentifyaction is what actually masks the data, and it must contain the"MaskConfig": {}object. The"MaskConfig": {}object must be empty.
For an example data protection policy, see the Examples section on this page.
Warning
The contents of the two
DataIdentiferarrays must match exactly.In addition to the two JSON blocks, the
policyDocumentcan also includeName,Description, andVersionfields. TheNameis different than the operation’spolicyNameparameter, and is used as a dimension when CloudWatch Logs reports audit findings metrics to CloudWatch.The JSON specified in
policyDocumentcan be up to 30,720 characters long.Subscription filter policy
A subscription filter policy can include the following attributes in a JSON block:
DestinationArn The ARN of the destination to deliver log events to. Supported destinations are:
An Kinesis Data Streams data stream in the same account as the subscription policy, for same-account delivery.
An Firehose data stream in the same account as the subscription policy, for same-account delivery.
A Lambda function in the same account as the subscription policy, for same-account delivery.
A logical destination in a different account created with PutDestination, for cross-account delivery. Kinesis Data Streams and Firehose are supported as logical destinations.
RoleArn The ARN of an IAM role that grants CloudWatch Logs permissions to deliver ingested log events to the destination stream. You don’t need to provide the ARN when you are working with a logical destination for cross-account delivery.
FilterPattern A filter pattern for subscribing to a filtered stream of log events.
Distribution The method used to distribute log data to the destination. By default, log data is grouped by log stream, but the grouping can be set to
Randomfor a more even distribution. This property is only applicable when the destination is an Kinesis Data Streams data stream.
policyType (string) –
[REQUIRED]
The type of policy that you’re creating or updating.
scope (string) – Currently the only valid value for this parameter is
ALL, which specifies that the data protection policy applies to all log groups in the account. If you omit this parameter, the default ofALLis used.selectionCriteria (string) –
Use this parameter to apply the subscription filter policy to a subset of log groups in the account. Currently, the only supported filter is
LogGroupName NOT IN []. TheselectionCriteriastring can be up to 25KB in length. The length is determined by using its UTF-8 bytes.Using the
selectionCriteriaparameter is useful to help prevent infinite loops. For more information, see Log recursion prevention.Specifing
selectionCriteriais valid only when you specifySUBSCRIPTION_FILTER_POLICYforpolicyType.
- Return type:
dict
- Returns:
Response Syntax
{ 'accountPolicy': { 'policyName': 'string', 'policyDocument': 'string', 'lastUpdatedTime': 123, 'policyType': 'DATA_PROTECTION_POLICY'|'SUBSCRIPTION_FILTER_POLICY', 'scope': 'ALL', 'selectionCriteria': 'string', 'accountId': 'string' } }
Response Structure
(dict) –
accountPolicy (dict) –
The account policy that you created.
policyName (string) –
The name of the account policy.
policyDocument (string) –
The policy document for this account policy.
The JSON specified in
policyDocumentcan be up to 30,720 characters.lastUpdatedTime (integer) –
The date and time that this policy was most recently updated.
policyType (string) –
The type of policy for this account policy.
scope (string) –
The scope of the account policy.
selectionCriteria (string) –
The log group selection criteria for this subscription filter policy.
accountId (string) –
The Amazon Web Services account ID that the policy applies to.
Exceptions
CloudWatchLogs.Client.exceptions.InvalidParameterExceptionCloudWatchLogs.Client.exceptions.OperationAbortedExceptionCloudWatchLogs.Client.exceptions.ServiceUnavailableExceptionCloudWatchLogs.Client.exceptions.LimitExceededException