Skip to content

Cannot create AWS::CloudTrail::ResourcePolicy due AlreadyExists error with cloudformation (EventDataStore)

0

I am not able to setup resource policies for cloudtrail EventDataStore with cloudformation. It claims that the the resource policy already exists, this is partially true as the console shows an empty policy and the API returns a "empty" default policy:

Resource handler returned message: "Resource of type 'AWS::CloudTrail::ResourcePolicy' with identifier '{"/properties/ResourceArn":"arn:aws:cloudtrail:eu-central-1:zys:eventdatastore/69edcf3e-5cb8-4333-a4c7-891e75a1d923"}' already exists." (RequestToken: 75673759-cef9-1d5a-a984-0adfbb61e3d0, HandlerErrorCode: AlreadyExists)

I even tried to import the resource (what was working fine), but i could not apply updates to it due some unknown errors (strangely drift detection did work):

Resource handler returned message: "Invalid request provided: AWS::CloudTrail::ResourcePolicy" (RequestToken: 710cb863-4605-cbfc-8bf8-aba0b193070f, HandlerErrorCode: InvalidRequest)

Not all very helpful and i am lost how to achieve that.

Parameters:
  Retention:
    Type: Number
    Default: 365
  SecurityAccountId:
    Type: String

Resources:
  EventDataStore:
    Type: AWS::CloudTrail::EventDataStore
    DeletionPolicy: Retain
    Properties:
      Name: aws-org-events
      MultiRegionEnabled: true
      IngestionEnabled: true
      #FederationEnabled: true
      #FederationRoleArn: "arn:aws:iam::account-id:role/federation-role-name"
      BillingMode: "EXTENDABLE_RETENTION_PRICING"
      RetentionPeriod: !Ref Retention
      OrganizationEnabled: true
      TerminationProtectionEnabled: true
      InsightsDestination: !GetAtt InsightsEventsDataStore.EventDataStoreArn
      InsightSelectors:
        - InsightType: "ApiCallRateInsight"
        - InsightType: "ApiErrorRateInsight"
      AdvancedEventSelectors:
        - Name: "ManagementEventSelectors"
          FieldSelectors:
            - Field: "eventCategory"
              Equals: ["Management"]

  # destination store for the insights events
  InsightsEventsDataStore:
    Type: AWS::CloudTrail::EventDataStore
    DeletionPolicy: Retain
    Properties:
      Name: aws-org-insights
      MultiRegionEnabled: false
      IngestionEnabled: true
      BillingMode: "EXTENDABLE_RETENTION_PRICING"
      RetentionPeriod: !Ref Retention
      OrganizationEnabled: true
      TerminationProtectionEnabled: true
      AdvancedEventSelectors:
        - Name: "InsightEventSelectors"
          FieldSelectors:
            - Field: "eventCategory"
              Equals: ["Insight"]


  ResourcePolicyEventDataStore:
    Type: AWS::CloudTrail::ResourcePolicy
    DeletionPolicy: Delete
    Properties:
      ResourceArn: !GetAtt EventDataStore.EventDataStoreArn
      ResourcePolicy:
        Fn::Sub: |
          {
            "Version": "2012-10-17",
            "Statement": [
              {
                "Effect": "Allow",
                "Principal":
                {
                  "Service": "cloudtrail.amazonaws.com"
                },
                "Action":
                [
                  "cloudtrail:StartQuery",
                  "cloudtrail:StartDashboardRefresh",
                  "cloudtrail:GetEventDataStore",
                  "cloudtrail:GetQueryResults"
                ],
                "Condition": {
                  "StringEquals": {
                      "AWS:SourceAccount":"${AWS::AccountId}"
                  }
                },
                "Resource": "*"
              },
              {
                "Sid": "AllowSecurityAccount",
                "Effect": "Allow",
                "Principal": {
                  "AWS": [
                      "arn:aws:iam::${SecurityAccountId}:root"
                  ]
                },
                "Action": [
                  "cloudtrail:StartQuery",
                  "cloudtrail:StartDashboardRefresh",
                  "cloudtrail:GetEventDataStore",
                  "cloudtrail:GetQueryResults"
                ]
              }
            ]
          }

  ResourcePolicyInsightsEventsDataStore:
    DeletionPolicy: Delete
    Type: AWS::CloudTrail::ResourcePolicy
    Properties:
      ResourceArn: !GetAtt InsightsEventsDataStore.EventDataStoreArn
      ResourcePolicy:
        Fn::Sub: |
          {
            "Version": "2012-10-17",
            "Statement": [
              {
                "Effect": "Allow",
                "Principal":
                {
                  "Service": "cloudtrail.amazonaws.com"
                },
                "Action":
                [
                  "cloudtrail:StartQuery",
                  "cloudtrail:StartDashboardRefresh",
                  "cloudtrail:GetEventDataStore",
                  "cloudtrail:GetQueryResults"
                ],
                "Condition": {
                  "StringEquals": {
                      "AWS:SourceAccount":"${AWS::AccountId}"
                  }
                }
              },
              {
                "Sid": "AllowSecurityAccount",
                "Effect": "Allow",
                "Principal": {
                  "AWS": [
                      "arn:aws:iam::${SecurityAccountId}:root"
                  ]
                },
                "Action": [
                  "cloudtrail:StartQuery",
                  "cloudtrail:StartDashboardRefresh",
                  "cloudtrail:GetEventDataStore",
                  "cloudtrail:GetQueryResults"
                ]
              }
            ]
          }
2 Answers
0

The issue you're experiencing with creating AWS::CloudTrail::ResourcePolicy resources for CloudTrail EventDataStore using CloudFormation is likely due to the default resource policy that AWS automatically creates when you create an EventDataStore.

When you create an EventDataStore, AWS CloudTrail automatically creates a default resource policy for it. This default policy is why you're seeing an "empty" policy in the console and getting an "AlreadyExists" error when trying to create a new one through CloudFormation.

To resolve this issue, you have a few options:

  1. Use the AWS CLI or SDK to update the existing resource policy instead of trying to create a new one through CloudFormation. You can use the put-resource-policy command in the AWS CLI for CloudTrail.

  2. If you want to manage the resource policy through CloudFormation, you might need to use a custom resource that calls the AWS SDK to update the existing policy rather than trying to create a new one.

  3. Another approach could be to create the EventDataStore without a resource policy initially, then immediately update it with your desired policy. This might require splitting your stack into two separate stacks or using a nested stack approach.

The "InvalidRequest" error you encountered when trying to import the resource could be due to discrepancies between the actual resource and how you're defining it in CloudFormation. Make sure all properties match exactly, including any default values that might be set on the existing resource.

Remember that resource policies for CloudTrail EventDataStores are relatively new features, and there might be some limitations or quirks in how they interact with CloudFormation that aren't well documented yet. If these approaches don't work, you might need to reach out to AWS support for further assistance.
Sources
AWS::CloudTrail::ResourcePolicy - AWS CloudFormation
Getting started with AWS CloudTrail tutorials - AWS CloudTrail
AWS::CloudTrail::EventDataStore - AWS CloudFormation

answered 2 years ago

0

Hi,

From the error message that you get, it seems that you already have active policies on the resources for which you provide the ARN.

So, go to your CloudTrail console and check if you have some policies already active on those resources ARNs: you cannot have multiple such policies on 1 same resource defined by its ARN.

Best,

Didier

EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.