AWSTemplateFormatVersion: '2010-09-09'
Description: >-
  SaaSLayer foundation: trust-identifier-gated access for the managing control plane to deploy and support
  SaaSLayer modules in this account. https://saaslayer.com/docs/getting-started/account-access

Parameters:
  ControlPlaneAccountId:
    Type: String
    AllowedPattern: '^[0-9]{12}$'
    Description: >-
      Account of the managing control plane.
  ControlPlaneId:
    Type: String
    AllowedPattern: '^[a-z][a-z0-9-]{1,48}[a-z0-9]$'
    Description: >-
      Id of the managing control plane; names its <id>-deploy and <id>-support identity roles.
  ControlPlaneTrustIdentifiers:
    Type: CommaDelimitedList
    Description: >-
      The control plane's trust identifiers (ExternalIds, not secrets): one, or two during a rotation.
  ConnectionAuthorizerArns:
    Type: CommaDelimitedList
    Description: IAM principals that may authorize connections.
  StackNamePrefix:
    Type: String
    Default: sl-
    AllowedPattern: '^[a-z][a-z0-9]{1,6}-$'
    Description: >-
      Prefix of the stacks the deployment role manages.
  OrgId:
    Type: String
    Default: ''
    Description: Connected org ID.
  EnableEmailInbound:
    Type: String
    AllowedValues: ['true', 'false']
    Default: 'false'
    Description: Allow inbound SES resources.
  EnableTenantEdge:
    Type: String
    AllowedValues: ['true', 'false']
    Default: 'false'
    Description: >-
      Native tenant-edge CloudFront permissions (cplane accounts enable-capability <host> tenant-edge); native-edge hosts enable it before a >= 1.1.0 redeploy.
  EnableExternalHooks:
    Type: String
    AllowedValues: ['true', 'false']
    Default: 'false'
    Description: >-
      Scoped-boundary roles may invoke the customer's own hook functions (cplane accounts enable-capability <host> external-hooks); dedicated accounts only.
  PermissionMode:
    Type: String
    AllowedValues: ['scoped', 'broad']
    Default: 'scoped'
    Description: >-
      scoped: permissions limited to SaaSLayer-managed resources. broad: account-wide resource permissions, with restricted IAM
      management. Accounts you own or have dedicated only.

Conditions:
  EmailInboundEnabled: !Equals [!Ref EnableEmailInbound, 'true']
  TenantEdgeEnabled: !Equals [!Ref EnableTenantEdge, 'true']
  ExternalHooksEnabled: !Equals [!Ref EnableExternalHooks, 'true']
  BroadPermissionsEnabled: !Equals [!Ref PermissionMode, 'broad']

Mappings:
  Meta:
    Foundation:
      Version: '11.1.0'

Resources:

  SaaSLayerCloudFormationExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: sl-foundation-cfn-exec-role
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: cloudformation.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - !Ref SaaSLayerCloudFormationExecutionPolicy
        - !If [ BroadPermissionsEnabled, !Sub 'arn:${AWS::Partition}:iam::aws:policy/PowerUserAccess', !Ref AWS::NoValue ]
        - !If [ BroadPermissionsEnabled, !Ref SaaSLayerBroadGuardrails, !Ref AWS::NoValue ]
      Policies:
        - PolicyName: saaslayer-approved-template-execution
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action: [
                  application-autoscaling:Describe*,
                  cloudformation:Describe*, cloudformation:Get*, cloudformation:List*,
                  cloudwatch:DescribeAlarms, cloudwatch:GetDashboard, cloudwatch:ListDashboards,
                  cloudwatch:ListTagsForResource,
                  cloudfront:*OriginAccessControl, cloudfront:GetDistribution*,
                  cloudfront:ListTagsForResource,
                  cognito-identity:ListIdentityPools, cognito-identity:SetIdentityPoolRoles,
                  cognito-idp:DescribeUserPoolDomain, cognito-idp:ListUserPools,
                  events:DescribeEventBus, events:DescribeRule, events:ListTagsForResource,
                  events:ListTargetsByRule,
                  kms:ListAliases,
                  iam:GetPolicy, iam:GetPolicyVersion, iam:GetRole,
                  iam:GetRolePolicy, iam:ListAttachedRolePolicies, iam:ListEntitiesForPolicy,
                  iam:ListPolicyVersions, iam:ListRolePolicies,
                  lambda:GetAccountSettings, lambda:GetEventSourceMapping,
                  lambda:ListEventSourceMappings, lambda:ListFunctions, lambda:ListLayerVersions,
                  logs:CreateLogDelivery, logs:DeleteLogDelivery, logs:DescribeIndexPolicies,
                  logs:DescribeLogGroups, logs:DescribeLogStreams, logs:DescribeMetricFilters,
                  logs:DescribeResourcePolicies, logs:GetLogDelivery, logs:ListLogDeliveries,
                  logs:ListTagsForResource, logs:PutResourcePolicy, logs:UpdateLogDelivery,
                  scheduler:GetSchedule, scheduler:GetScheduleGroup, scheduler:ListTagsForResource,
                  secretsmanager:DescribeSecret, secretsmanager:GetResourcePolicy,
                  ssm:DescribeParameters, ssm:ListTagsForResource,
                  sqs:GetQueueAttributes, sqs:ListQueueTags,
                  ]
                Resource: '*'
              - Effect: Allow
                Action:
                  - apigateway:*
                Resource:
                  - !Sub arn:${AWS::Partition}:apigateway:*::/restapis
                  - !Sub arn:${AWS::Partition}:apigateway:*::/restapis/*
                  - !Sub arn:${AWS::Partition}:apigateway:*::/apis
                  - !Sub arn:${AWS::Partition}:apigateway:*::/apis/*
                  - !Sub arn:${AWS::Partition}:apigateway:*::/tags/*
              - Effect: Allow
                Action:
                  - cloudfront:CreateDistribution
                  - cloudfront:TagResource
                  - cognito-identity:CreateIdentityPool
                  - cognito-identity:TagResource
                  - cognito-idp:CreateUserPool
                  - secretsmanager:CreateSecret
                  - secretsmanager:TagResource
                Resource: '*'
                Condition:
                  StringEquals:
                    aws:RequestTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action:
                  - cloudfront:*TagResource
                  - cloudfront:DeleteDistribution
                  - cloudfront:UpdateDistribution
                  - cognito-identity:*IdentityPool
                  - cognito-identity:GetIdentityPoolRoles
                  - cognito-identity:ListTagsForResource
                  - cognito-identity:*PrincipalTagAttributeMap
                  - cognito-identity:*TagResource
                  - cognito-idp:*
                  - secretsmanager:*Replica*
                  - secretsmanager:*TagResource
                  - secretsmanager:DeleteSecret
                  - secretsmanager:PutSecretValue
                  - secretsmanager:RestoreSecret
                  - secretsmanager:Update*
                Resource:
                  - !Sub arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:distribution/*
                  - !Sub arn:${AWS::Partition}:cognito-identity:*:${AWS::AccountId}:identitypool/*
                  - !Sub arn:${AWS::Partition}:cognito-idp:*:${AWS::AccountId}:userpool/*
                  - !Sub arn:${AWS::Partition}:secretsmanager:*:${AWS::AccountId}:secret:*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action:
                  - cloudfront:UpdateDistribution
                Resource: '*'
                Condition:
                  ForAnyValue:StringEquals:
                    aws:CalledVia:
                      - cognito-idp.amazonaws.com
              - Effect: Deny
                Action:
                  - cognito-idp:Admin*
                  - cognito-idp:CreateIdentityProvider
                  - cognito-idp:UpdateIdentityProvider
                  - cognito-idp:CreateUserImportJob
                  - cognito-idp:StartUserImportJob
                  - cognito-idp:CreateResourceServer
                Resource: '*'
              - Effect: Deny
                Action:
                  - cloudfront:TagResource
                  - cognito-idp:TagResource
                  - cognito-identity:TagResource
                  - secretsmanager:TagResource
                  - kms:TagResource
                Resource: '*'
                Condition:
                  ForAnyValue:StringEquals:
                    aws:TagKeys:
                      - managed-by
                  'Null':
                    aws:ResourceTag/managed-by: 'true'
              - Effect: Allow
                Action:
                  - dynamodb:CreateTable
                  - dynamodb:DeleteTable
                  - dynamodb:Describe*
                  - dynamodb:ListTagsOfResource
                  - dynamodb:TagResource
                  - dynamodb:UntagResource
                  - dynamodb:UpdateContinuousBackups
                  - dynamodb:UpdateContributorInsights
                  - dynamodb:UpdateTable
                  - dynamodb:UpdateTimeToLive
                Resource: !Sub arn:${AWS::Partition}:dynamodb:*:${AWS::AccountId}:table/${StackNamePrefix}*
              - Effect: Allow
                Action:
                  - application-autoscaling:Delete*
                  - application-autoscaling:DeregisterScalableTarget
                  - application-autoscaling:Put*
                  - application-autoscaling:RegisterScalableTarget
                Resource: '*'
                Condition:
                  StringEquals:
                    application-autoscaling:service-namespace: dynamodb
              - Effect: Allow
                Action:
                  - kms:CreateKey
                Resource: '*'
                Condition:
                  StringEquals:
                    aws:RequestTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action:
                  - kms:TagResource
                Resource: !Sub arn:${AWS::Partition}:kms:*:${AWS::AccountId}:key/*
                Condition:
                  StringEquals:
                    aws:RequestTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action:
                  - kms:DescribeKey
                Resource: !Sub arn:${AWS::Partition}:kms:*:${AWS::AccountId}:key/*
                Condition:
                  ForAnyValue:StringEquals:
                    kms:ResourceAliases:
                      - alias/aws/dynamodb
              - Effect: Allow
                Action: [
                  kms:*Alias, kms:CreateGrant, kms:Decrypt, kms:DescribeKey,
                  kms:DisableKeyRotation, kms:EnableKeyRotation, kms:Encrypt, kms:GenerateDataKey*,
                  kms:Get*, kms:List*, kms:PutKeyPolicy, kms:ReEncrypt*,
                  kms:RevokeGrant, kms:ScheduleKeyDeletion, kms:TagResource, kms:UntagResource
                ]
                Resource: !Sub arn:${AWS::Partition}:kms:*:${AWS::AccountId}:key/*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action:
                  - kms:*Alias
                Resource: !Sub arn:${AWS::Partition}:kms:*:${AWS::AccountId}:alias/${StackNamePrefix}*
              - Effect: Allow
                Action: [
                  lambda:CreateFunction, lambda:Delete*, lambda:Get*,
                  lambda:InvokeFunction, lambda:List*, lambda:PublishVersion,
                  lambda:Put*, lambda:RemovePermission, lambda:TagResource, lambda:UntagResource,
                  lambda:Update*
                ]
                Resource:
                  - !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
              - Effect: Allow
                Action:
                  - lambda:DeleteEventSourceMapping
                  - lambda:UpdateEventSourceMapping
                Resource: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:event-source-mapping:*
                Condition:
                  ArnLike:
                    lambda:FunctionArn: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
              - Effect: Allow
                Action:
                  - lambda:ListTags
                  - lambda:TagResource
                  - lambda:UntagResource
                Resource: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:event-source-mapping:*
              - Effect: Allow
                Action: [
                  s3:CreateBucket, s3:DeleteBucket*, s3:GetAccelerateConfiguration,
                  s3:GetBucket*, s3:GetEncryptionConfiguration, s3:GetLifecycleConfiguration, s3:GetObject,
                  s3:ListBucket, s3:PutBucket*, s3:PutEncryptionConfiguration,
                  s3:PutLifecycleConfiguration,
                  s3:*TagResource, s3:ListTagsForResource
                ]
                Resource: !Sub arn:${AWS::Partition}:s3:::${StackNamePrefix}*
              - Effect: Allow
                Action:
                  - iam:CreateRole
                Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
                Condition:
                  StringEquals:
                    iam:PermissionsBoundary: [ !Ref ScopedResourceBoundary, !Ref WebIdentityBoundary ]
              - Effect: Allow
                Action:
                  - iam:PutRolePermissionsBoundary
                Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
                Condition:
                  StringEquals:
                    iam:PermissionsBoundary: [ !Ref ScopedResourceBoundary, !Ref WebIdentityBoundary ]
              - Effect: Allow
                Action:
                  - iam:CreatePolicy
                  - iam:CreatePolicyVersion
                  - iam:DeletePolicy
                  - iam:DeletePolicyVersion
                  - iam:SetDefaultPolicyVersion
                  - iam:TagPolicy
                  - iam:UntagPolicy
                Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/${StackNamePrefix}*
              - Effect: Allow
                Action:
                  - iam:CreateServiceLinkedRole
                Resource: 'arn:aws:iam::*:role/aws-service-role/email.cognito-idp.amazonaws.com/*'
                Condition:
                  StringEquals:
                    iam:AWSServiceName: email.cognito-idp.amazonaws.com
              - Effect: Deny
                Action:
                  - iam:CreatePolicyVersion
                  - iam:SetDefaultPolicyVersion
                  - iam:DeletePolicy
                  - iam:DeletePolicyVersion
                  - iam:DeleteRole
                  - iam:PutRolePolicy
                  - iam:DeleteRolePolicy
                  - iam:AttachRolePolicy
                  - iam:DetachRolePolicy
                  - iam:UpdateAssumeRolePolicy
                  - iam:UpdateRole
                  - iam:PassRole
                  - iam:TagRole
                  - iam:UntagRole
                  - iam:UpdateRoleDescription
                  - iam:PutRolePermissionsBoundary
                  - iam:DeleteRolePermissionsBoundary
                  - iam:TagPolicy
                  - iam:UntagPolicy
                Resource:
                  - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/sl-foundation-*
                  - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-cfn-exec-role
                  - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-deploy-role
                  - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-support-ro-role
                  - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-template-guard-*
              - Fn::If:
                  - TenantEdgeEnabled
                  - Effect: Allow
                    Action:
                      - cloudfront:CreateFunction
                      - cloudfront:DescribeFunction
                      - cloudfront:GetFunction
                      - cloudfront:ListFunctions
                      - cloudfront:ListDistributions
                    Resource: '*'
                  - !Ref AWS::NoValue
              - Fn::If:
                  - TenantEdgeEnabled
                  - Effect: Allow
                    Action:
                      - cloudfront:PublishFunction
                      - cloudfront:UpdateFunction
                    Resource: !Sub arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:function/${StackNamePrefix}*
                  - !Ref AWS::NoValue
              - Fn::If:
                  - EmailInboundEnabled
                  - Effect: Allow
                    Action:
                      - ses:CreateReceiptRuleSet
                      - ses:DeleteReceiptRuleSet
                      - ses:CreateReceiptRule
                      - ses:DeleteReceiptRule
                      - ses:UpdateReceiptRule
                      - ses:SetReceiptRulePosition
                      - ses:DescribeReceiptRuleSet
                      - ses:DescribeReceiptRule
                    Resource: '*'
                  - !Ref AWS::NoValue
              - Effect: Allow
                Action: [
                  sms-voice:AssociateProtectConfiguration, sms-voice:CreateConfigurationSet, sms-voice:CreateProtectConfiguration, sms-voice:DeleteConfigurationSet,
                  sms-voice:DeleteProtectConfiguration, sms-voice:DisassociateProtectConfiguration, sms-voice:GetProtectConfigurationCountryRuleSet, sms-voice:ListTagsForResource,
                  sms-voice:TagResource, sms-voice:UntagResource, sms-voice:UpdateProtectConfiguration, sms-voice:UpdateProtectConfigurationCountryRuleSet
                ]
                Resource:
                  - !Sub arn:${AWS::Partition}:sms-voice:*:${AWS::AccountId}:configuration-set/${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:sms-voice:*:${AWS::AccountId}:protect-configuration/*
              - Effect: Allow
                Action:
                  - sms-voice:DescribeConfigurationSets
                  - sms-voice:DescribeProtectConfigurations
                Resource: '*'
              - Effect: Allow
                Action:
                  - cloudformation:CreateChangeSet
                Resource: !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:aws:transform/LanguageExtensions

  SaaSLayerCloudFormationExecutionPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: sl-foundation-cfn-exec-policy
      Description: Managed-prefix lifecycle of module alarms, event buses and rules, log groups, schedules, parameters and queues.
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - iam:DeleteRole
              - iam:UpdateAssumeRolePolicy
              - iam:PutRolePolicy
              - iam:DeleteRolePolicy
            Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
            Condition:
              StringEquals:
                iam:PermissionsBoundary: [ !Ref ScopedResourceBoundary, !Ref WebIdentityBoundary ]
          - Effect: Allow
            Action:
              - iam:AttachRolePolicy
              - iam:DetachRolePolicy
            Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
            Condition:
              StringEquals:
                iam:PermissionsBoundary: [ !Ref ScopedResourceBoundary, !Ref WebIdentityBoundary ]
              ArnLike:
                iam:PolicyARN: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/${StackNamePrefix}*
          - Effect: Allow
            Action:
              - iam:UpdateRole
              - iam:UpdateRoleDescription
              - iam:TagRole
              - iam:UntagRole
            Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
          - Effect: Allow
            Action:
              - iam:PassRole
            Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
            Condition:
              StringEquals:
                iam:PassedToService: [ lambda.amazonaws.com, cognito-idp.amazonaws.com, cognito-identity.amazonaws.com ]
          - Effect: Allow
            Action: iam:PassRole
            Resource:
              - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*-snsSendRole
              - !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*-phoneSms
            Condition:
              StringEqualsIfExists:
                iam:PassedToService: cognito-idp.amazonaws.com
          - Effect: Allow
            Action: lambda:AddPermission
            Resource: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
            Condition:
              StringEquals:
                lambda:Principal: [ apigateway.amazonaws.com, cognito-idp.amazonaws.com, events.amazonaws.com, ses.amazonaws.com ]
          - Effect: Allow
            Action: events:PutTargets
            Resource: !Sub arn:${AWS::Partition}:events:*:${AWS::AccountId}:rule/${StackNamePrefix}*
            Condition:
              ArnLike:
                events:TargetArn: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
          - Effect: Allow
            Action: lambda:CreateEventSourceMapping
            Resource: '*'
            Condition:
              ArnLike:
                lambda:FunctionArn: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
          - Effect: Allow
            Action: sns:Subscribe
            Resource: !Sub arn:${AWS::Partition}:sns:*:${AWS::AccountId}:${StackNamePrefix}*
            Condition:
              StringEquals:
                sns:Protocol: sqs
          - Effect: Allow
            Action: [ ssm:GetParameter, ssm:GetParameters ]
            Resource:
              - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/deployment/*
              - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/${StackNamePrefix}*
          - Effect: Deny
            Action: [ s3:Put*, s3:Delete*, s3:Replicate*, s3:Restore*, s3:Abort* ]
            Resource:
              - !Sub arn:${AWS::Partition}:s3:::sl-artifacts-*
          - Effect: Deny
            Action: [ s3:PutBucketReplication, s3:PutReplicationConfiguration, s3:PutBucketWebsite, s3:PutBucketLogging,
              s3:PutBucketNotification, s3:PutBucketObjectLockConfiguration, s3:PutBucketRequestPayment ]
            Resource: '*'
          - Effect: Allow
            Action:
              - cloudfront:CreateCachePolicy
              - cloudfront:CreateResponseHeadersPolicy
            Resource: '*'
          - Sid: CdnPolicyLifecycle
            Effect: Allow
            Action:
              - cloudfront:GetCachePolicy
              - cloudfront:GetCachePolicyConfig
              - cloudfront:UpdateCachePolicy
              - cloudfront:DeleteCachePolicy
              - cloudfront:GetResponseHeadersPolicy
              - cloudfront:GetResponseHeadersPolicyConfig
              - cloudfront:UpdateResponseHeadersPolicy
              - cloudfront:DeleteResponseHeadersPolicy
            Resource:
              - !Sub arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:cache-policy/*
              - !Sub arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:response-headers-policy/*
          - Effect: Allow
            Action: [
              wafv2:CreateWebACL, wafv2:DeleteWebACL, wafv2:UpdateWebACL,
              wafv2:GetWebACL, wafv2:GetWebACLForResource, wafv2:ListTagsForResource,
              wafv2:TagResource, wafv2:UntagResource, wafv2:AssociateWebACL,
              wafv2:PutLoggingConfiguration, wafv2:GetLoggingConfiguration, wafv2:DeleteLoggingConfiguration,
              ]
            Resource:
              - !Sub arn:${AWS::Partition}:wafv2:*:${AWS::AccountId}:regional/webacl/${StackNamePrefix}*-waf/*
          - Effect: Allow
            Action: [
              cloudwatch:DeleteAlarms, cloudwatch:DeleteDashboards, cloudwatch:DisableAlarmActions,
              cloudwatch:EnableAlarmActions, cloudwatch:PutDashboard, cloudwatch:PutMetricAlarm,
              cloudwatch:TagResource, cloudwatch:UntagResource, events:CreateEventBus,
              events:DeleteEventBus, events:DeleteRule, events:DisableRule,
              events:EnableRule, events:PutRule,
              events:RemoveTargets, events:TagResource, events:UntagResource,
              logs:CreateLogGroup, logs:DeleteLogGroup, logs:DeleteMetricFilter, logs:DeleteRetentionPolicy,
              logs:PutMetricFilter, logs:PutRetentionPolicy, logs:TagLogGroup,
              logs:TagResource, logs:UntagLogGroup, logs:UntagResource,
              scheduler:CreateScheduleGroup,
              scheduler:DeleteSchedule, scheduler:DeleteScheduleGroup, scheduler:TagResource, scheduler:UntagResource,
              ssm:AddTagsToResource, ssm:DeleteParameter,
              ssm:DeleteParameters, ssm:PutParameter, ssm:RemoveTagsFromResource,
              sqs:CreateQueue, sqs:DeleteQueue, sqs:SetQueueAttributes,
              sqs:TagQueue, sqs:UntagQueue,
              sns:CreateTopic, sns:DeleteTopic, sns:GetTopicAttributes,
              sns:ListSubscriptionsByTopic, sns:SetTopicAttributes,
              sns:TagResource, sns:Unsubscribe, sns:UntagResource,
              ]
            Resource:
              - !Sub arn:${AWS::Partition}:cloudwatch:*:${AWS::AccountId}:alarm:${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:cloudwatch::${AWS::AccountId}:dashboard/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:events:*:${AWS::AccountId}:event-bus/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:events:*:${AWS::AccountId}:event-busv2/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:events:*:${AWS::AccountId}:rule/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:logs:*:${AWS::AccountId}:log-group:/aws/lambda/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:logs:*:${AWS::AccountId}:log-group:/aws/cognito/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:logs:*:${AWS::AccountId}:log-group:aws-waf-logs-${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:scheduler:*:${AWS::AccountId}:schedule-group/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:scheduler:*:${AWS::AccountId}:schedule/${StackNamePrefix}*/*
              - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:sqs:*:${AWS::AccountId}:${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:sns:*:${AWS::AccountId}:${StackNamePrefix}*

  ScopedResourceBoundary:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: sl-foundation-scoped-boundary
      Description: Maximum runtime permissions for SaaSLayer module roles, on SaaSLayer resources only.
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          Fn::If:
            - BroadPermissionsEnabled
            - - Sid: BroadResourceCeiling
                Effect: Allow
                NotAction: [ iam:*, organizations:*, account:* ]
                Resource: '*'
              - Effect: Allow
                Action: iam:PassRole
                Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
                Condition:
                  StringEquals:
                    iam:PassedToService: scheduler.amazonaws.com
              - Effect: Deny
                Action: [ s3:PutObject, s3:DeleteObject* ]
                Resource: !Sub arn:${AWS::Partition}:s3:::sl-artifacts-*
              - Sid: DenyFoundationParameterWrites
                Effect: Deny
                Action: [ ssm:PutParameter, ssm:DeleteParameter, ssm:DeleteParameters, ssm:LabelParameterVersion, ssm:UnlabelParameterVersion,
                  ssm:AddTagsToResource, ssm:RemoveTagsFromResource ]
                Resource: !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/foundation/*
              - Sid: DenyTypeAndHookChanges
                Effect: Deny
                Action: [ cloudformation:ActivateType, cloudformation:DeactivateType, cloudformation:DeregisterType, cloudformation:RegisterType,
                  cloudformation:PublishType, cloudformation:SetTypeConfiguration, cloudformation:SetTypeDefaultVersion ]
                Resource: '*'
              - Sid: DenyAssumeOutsidePrefix
                Effect: Deny
                Action: sts:AssumeRole
                NotResource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
              - Sid: DenyStackWritesOutsidePrefix
                Effect: Deny
                Action: [ cloudformation:CreateStack, cloudformation:UpdateStack, cloudformation:DeleteStack, cloudformation:CreateChangeSet,
                  cloudformation:ExecuteChangeSet, cloudformation:DeleteChangeSet, cloudformation:SetStackPolicy, cloudformation:UpdateTerminationProtection,
                  cloudformation:ContinueUpdateRollback, cloudformation:CancelUpdateStack, cloudformation:RollbackStack, cloudformation:CreateStackSet,
                  cloudformation:UpdateStackSet, cloudformation:DeleteStackSet, cloudformation:CreateStackInstances, cloudformation:UpdateStackInstances,
                  cloudformation:DeleteStackInstances, cloudformation:ImportStacksToStackSet ]
                NotResource:
                  - !Sub arn:${AWS::Partition}:cloudformation:*:${AWS::AccountId}:stack/${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:cloudformation:*:aws:transform/*
              - Sid: DenyFunctionWritesOutsidePrefix
                Effect: Deny
                Action: [ lambda:UpdateFunctionCode, lambda:UpdateFunctionConfiguration, lambda:PublishVersion, lambda:CreateAlias, lambda:UpdateAlias,
                  lambda:AddPermission, lambda:PutFunctionEventInvokeConfig, lambda:UpdateFunctionEventInvokeConfig, lambda:CreateFunctionUrlConfig,
                  lambda:UpdateFunctionUrlConfig ]
                NotResource: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
              - Sid: DenyRunningCodeOnInstances
                Effect: Deny
                Action: [ ssm:SendCommand, ssm:StartSession, ssm:StartAutomationExecution, ec2:ModifyInstanceAttribute ]
                Resource: '*'
              - Sid: DenyBuildAndWorkflowRewritesOutsidePrefix
                Effect: Deny
                Action: [ codebuild:StartBuild, codebuild:StartBuildBatch, codebuild:UpdateProject, states:UpdateStateMachine ]
                NotResource:
                  - !Sub arn:${AWS::Partition}:codebuild:*:${AWS::AccountId}:project/${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:states:*:${AWS::AccountId}:stateMachine:${StackNamePrefix}*
            - - Effect: Allow
                Action: [ cognito-idp:AdminCreateUser, cognito-idp:AdminDelete*, cognito-idp:AdminDisable*, cognito-idp:AdminEnableUser,
                  cognito-idp:AdminGetUser, cognito-idp:AdminLinkProviderForUser, cognito-idp:AdminResetUserPassword, cognito-idp:AdminSet*,
                  cognito-idp:AdminUpdateUserAttributes, cognito-idp:AdminUserGlobalSignOut,
                  cognito-idp:ListUsers, cognito-idp:GetUserPoolMfaConfig,
                  cognito-idp:*IdentityProvider, cognito-idp:*UserPoolClient,
                  cognito-idp:ListUserPoolClients, cognito-idp:*ManagedLoginBranding*, cognito-idp:UpdateUserPoolDomain ]
                Resource: !Sub arn:${AWS::Partition}:cognito-idp:*:${AWS::AccountId}:userpool/*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action: [ dynamodb:BatchGetItem, dynamodb:BatchWriteItem, dynamodb:ConditionCheckItem, dynamodb:DeleteItem,
                  dynamodb:DescribeTable, dynamodb:GetItem, dynamodb:PutItem, dynamodb:Query, dynamodb:Scan, dynamodb:UpdateItem,
                  dynamodb:TransactWriteItems,
                  dynamodb:DescribeStream, dynamodb:GetRecords, dynamodb:GetShardIterator, dynamodb:ListStreams,
                  dynamodb:DescribeContinuousBackups, dynamodb:CreateBackup, dynamodb:DescribeBackup,
                  s3:GetObject, s3:PutObject, s3:DeleteObject,
                  sqs:SendMessage, sqs:ReceiveMessage, sqs:DeleteMessage, sqs:GetQueueAttributes, sqs:ChangeMessageVisibility,
                  scheduler:CreateSchedule, scheduler:DeleteSchedule, scheduler:GetSchedule, scheduler:UpdateSchedule,
                  lambda:InvokeFunction,
                  logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents ]
                Resource:
                  - !Sub arn:${AWS::Partition}:dynamodb:*:${AWS::AccountId}:table/${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:s3:::${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:sqs:*:${AWS::AccountId}:${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:scheduler:*:${AWS::AccountId}:schedule/${StackNamePrefix}*/*
                  - !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:logs:*:${AWS::AccountId}:log-group:/aws/lambda/${StackNamePrefix}*
              - Effect: Allow
                Action: scheduler:ListSchedules
                Resource: '*'
              - !If
                - ExternalHooksEnabled
                - Effect: Allow
                  Action: lambda:InvokeFunction
                  Resource: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:*
                - !Ref AWS::NoValue
              - Effect: Allow
                Action: [ logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents, logs:DescribeLogStreams ]
                Resource: !Sub arn:${AWS::Partition}:logs:*:${AWS::AccountId}:log-group:/aws/cognito/*
              - Effect: Allow
                Action: [ ssm:GetParameter, ssm:GetParameters, ssm:GetParametersByPath ]
                Resource:
                  - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/deployment/*
                  - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/${StackNamePrefix}*
              - Effect: Allow
                Action: ssm:GetParameter
                Resource: !Sub arn:${AWS::Partition}:ssm:us-east-1:${AWS::AccountId}:parameter/saaslayer/control-plane/event-bus-connect-ticket-key
                Condition:
                  StringEquals:
                    aws:PrincipalAccount: !Ref AWS::AccountId
                  ArnEquals:
                    aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}control-plane-prod-eventbus-use1-wsConnect
              - Effect: Allow
                Action: kms:Decrypt
                Resource: !Sub arn:${AWS::Partition}:kms:us-east-1:${AWS::AccountId}:key/*
                Condition:
                  StringEquals:
                    aws:PrincipalAccount: !Ref AWS::AccountId
                    kms:ViaService: ssm.us-east-1.amazonaws.com
                    kms:EncryptionContext:PARAMETER_ARN: !Sub arn:${AWS::Partition}:ssm:us-east-1:${AWS::AccountId}:parameter/saaslayer/control-plane/event-bus-connect-ticket-key
                  ArnEquals:
                    aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}control-plane-prod-eventbus-use1-wsConnect
              - Effect: Allow
                Action: [ ssm:PutParameter, ssm:DeleteParameter ]
                Resource:
                  - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/deployment/*
                  - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/${StackNamePrefix}*/worker/credentials/*
                  - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/${StackNamePrefix}*/control/credentials/*
              - Effect: Allow
                Action: [ kms:Decrypt, kms:Encrypt ]
                Resource: !Sub arn:${AWS::Partition}:kms:*:${AWS::AccountId}:key/*
                Condition:
                  StringLike:
                    kms:ViaService: ssm.*.amazonaws.com
                    kms:EncryptionContext:PARAMETER_ARN:
                      - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/deployment/*
                      - !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/${StackNamePrefix}*
              - Effect: Allow
                Action: [ kms:Decrypt, kms:Encrypt, kms:GenerateDataKey, kms:DescribeKey ]
                Resource: !Sub arn:${AWS::Partition}:kms:*:${AWS::AccountId}:key/*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action: iam:PassRole
                Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
                Condition:
                  StringEquals:
                    iam:PassedToService: scheduler.amazonaws.com
              - Effect: Allow
                Action: [ execute-api:Invoke, execute-api:ManageConnections ]
                Resource: !Sub arn:${AWS::Partition}:execute-api:*:${AWS::AccountId}:*
              - Effect: Allow
                Action: events:PutEvents
                Resource: !Sub arn:${AWS::Partition}:events:*:*:event-bus/*
              - Effect: Allow
                Action: sns:Publish
                Resource: '*'
              - Effect: Allow
                Action: ses:SendEmail
                Resource: !Sub arn:${AWS::Partition}:ses:*:${AWS::AccountId}:identity/*
              - Effect: Allow
                Action: sms-voice:SendTextMessage
                Resource: !Sub arn:${AWS::Partition}:sms-voice:*:${AWS::AccountId}:*
              - Effect: Allow
                Action: sms-voice:DescribeOptedOutNumbers
                Resource: !Sub arn:${AWS::Partition}:sms-voice:*:${AWS::AccountId}:opt-out-list/Default
              - Fn::If:
                  - EmailInboundEnabled
                  - Effect: Allow
                    Action: [ ses:DescribeActiveReceiptRuleSet, ses:SendEmail, ses:SetActiveReceiptRuleSet ]
                    Resource: '*'
                  - !Ref AWS::NoValue
              - Effect: Deny
                Action: [ s3:PutObject, s3:DeleteObject* ]
                Resource: !Sub arn:${AWS::Partition}:s3:::sl-artifacts-*

  SaaSLayerBroadGuardrails:
    Type: AWS::IAM::ManagedPolicy
    Condition: BroadPermissionsEnabled
    Properties:
      ManagedPolicyName: sl-foundation-broad-guardrails
      Description: What SaaSLayer may never do in a broad-permission account.
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: DenyFoundationParameterWrites
            Effect: Deny
            Action: [ ssm:PutParameter, ssm:DeleteParameter, ssm:DeleteParameters, ssm:LabelParameterVersion, ssm:UnlabelParameterVersion,
              ssm:AddTagsToResource, ssm:RemoveTagsFromResource ]
            Resource: !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/foundation/*
          - Sid: DenyTypeAndHookChanges
            Effect: Deny
            Action: [ cloudformation:ActivateType, cloudformation:DeactivateType, cloudformation:DeregisterType, cloudformation:RegisterType,
              cloudformation:PublishType, cloudformation:SetTypeConfiguration, cloudformation:SetTypeDefaultVersion ]
            Resource: '*'
          - Sid: DenyAssumeOutsidePrefix
            Effect: Deny
            Action: sts:AssumeRole
            NotResource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${StackNamePrefix}*
          - Sid: DenyStackWritesOutsidePrefix
            Effect: Deny
            Action: [ cloudformation:CreateStack, cloudformation:UpdateStack, cloudformation:DeleteStack, cloudformation:CreateChangeSet,
              cloudformation:ExecuteChangeSet, cloudformation:DeleteChangeSet, cloudformation:SetStackPolicy, cloudformation:UpdateTerminationProtection,
              cloudformation:ContinueUpdateRollback, cloudformation:CancelUpdateStack, cloudformation:RollbackStack, cloudformation:CreateStackSet,
              cloudformation:UpdateStackSet, cloudformation:DeleteStackSet, cloudformation:CreateStackInstances, cloudformation:UpdateStackInstances,
              cloudformation:DeleteStackInstances, cloudformation:ImportStacksToStackSet ]
            NotResource:
              - !Sub arn:${AWS::Partition}:cloudformation:*:${AWS::AccountId}:stack/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:cloudformation:*:aws:transform/*
          - Sid: DenyFunctionWritesOutsidePrefix
            Effect: Deny
            Action: [ lambda:UpdateFunctionCode, lambda:UpdateFunctionConfiguration, lambda:PublishVersion, lambda:CreateAlias, lambda:UpdateAlias,
              lambda:AddPermission, lambda:PutFunctionEventInvokeConfig, lambda:UpdateFunctionEventInvokeConfig, lambda:CreateFunctionUrlConfig,
              lambda:UpdateFunctionUrlConfig ]
            NotResource: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${StackNamePrefix}*
          - Sid: DenyRunningCodeOnInstances
            Effect: Deny
            Action: [ ssm:SendCommand, ssm:StartSession, ssm:StartAutomationExecution, ec2:ModifyInstanceAttribute ]
            Resource: '*'
          - Sid: DenyBuildAndWorkflowRewritesOutsidePrefix
            Effect: Deny
            Action: [ codebuild:StartBuild, codebuild:StartBuildBatch, codebuild:UpdateProject, states:UpdateStateMachine ]
            NotResource:
              - !Sub arn:${AWS::Partition}:codebuild:*:${AWS::AccountId}:project/${StackNamePrefix}*
              - !Sub arn:${AWS::Partition}:states:*:${AWS::AccountId}:stateMachine:${StackNamePrefix}*

  WebIdentityBoundary:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: sl-foundation-web-identity-boundary
      Description: Maximum permissions for SaaSLayer identity-pool roles.
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action: execute-api:Invoke
            Resource: !Sub arn:${AWS::Partition}:execute-api:*:${AWS::AccountId}:*

  SaaSLayerDeploymentRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: sl-foundation-deploy-role
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              AWS: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:root
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                sts:ExternalId: !Ref ControlPlaneTrustIdentifiers
                aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:role/${ControlPlaneId}-deploy
              StringLike:
                sts:RoleSessionName: saaslayer-*
                sts:SourceIdentity: saaslayer.*
              'Null':
                aws:RequestTag/deploymentId: 'false'
                aws:RequestTag/stackPrefix: 'false'
          - Effect: Allow
            Principal:
              AWS: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:root
            Action: sts:TagSession
            Condition:
              StringEquals:
                aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:role/${ControlPlaneId}-deploy
              ForAllValues:StringEquals:
                aws:TagKeys: [ deploymentId, stackPrefix ]
              StringLike:
                aws:RequestTag/deploymentId: dep_?*
                aws:RequestTag/stackPrefix: !Sub '${StackNamePrefix}?*-'
          - Effect: Allow
            Principal:
              AWS: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:root
            Action: sts:SetSourceIdentity
            Condition:
              StringEquals:
                aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:role/${ControlPlaneId}-deploy
              StringLike:
                sts:SourceIdentity: saaslayer.*
      ManagedPolicyArns:
        - !Ref SaaSLayerDeploymentPolicy
        - !If [ BroadPermissionsEnabled, !Sub 'arn:${AWS::Partition}:iam::aws:policy/PowerUserAccess', !Ref AWS::NoValue ]
        - !If [ BroadPermissionsEnabled, !Ref SaaSLayerBroadGuardrails, !Ref AWS::NoValue ]
      Policies:
        - PolicyName: saaslayer-stack-lifecycle
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - wafv2:GetWebACL
                  - wafv2:GetWebACLForResource
                  - wafv2:GetLoggingConfiguration
                Resource:
                  - !Sub arn:${AWS::Partition}:wafv2:*:${AWS::AccountId}:regional/webacl/${!aws:PrincipalTag/stackPrefix}*-waf/*
              - Effect: Allow
                Action:
                  - wafv2:DescribeManagedRuleGroup
                  - wafv2:CheckCapacity
                Resource: '*'
              - Effect: Allow
                Action:
                  - cloudformation:CreateStack
                  - cloudformation:CreateChangeSet
                  - cloudformation:UpdateStack
                Resource: !Sub arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${!aws:PrincipalTag/stackPrefix}*
                Condition:
                  StringEquals:
                    cloudformation:RoleArn: !GetAtt SaaSLayerCloudFormationExecutionRole.Arn
                  StringEqualsIfExists:
                    aws:RequestTag/deploymentId: ${aws:PrincipalTag/deploymentId}
                    aws:RequestTag/stackPrefix: ${aws:PrincipalTag/stackPrefix}
              - Effect: Allow
                Action:
                  - cloudformation:ExecuteChangeSet
                  - cloudformation:SetStackPolicy
                  - cloudformation:UpdateTerminationProtection
                  - cloudformation:DeleteChangeSet
                  - cloudformation:DeleteStack
                  - cloudformation:ContinueUpdateRollback
                  - cloudformation:Describe*
                  - cloudformation:Get*
                  - cloudformation:ListStackResources
                  - cloudformation:DetectStackDrift
                  - cloudformation:DetectStackResourceDrift
                Resource: !Sub arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${!aws:PrincipalTag/stackPrefix}*
              - Sid: DenyImportsOutsideAdoption
                Effect: Deny
                Action: cloudformation:CreateChangeSet
                Resource: '*'
                Condition:
                  'Null':
                    cloudformation:ImportResourceTypes: 'false'
                  StringNotLike:
                    cloudformation:ChangeSetName: saaslayer-adopt-*
              - Sid: DenyImportOfUnadoptableTypes
                Effect: Deny
                Action: cloudformation:CreateChangeSet
                Resource: '*'
                Condition:
                  ForAnyValue:StringNotEquals:
                    cloudformation:ImportResourceTypes:
                      - AWS::DynamoDB::Table
                      - AWS::DynamoDB::GlobalTable
                      - AWS::Cognito::UserPool
                      - AWS::Cognito::UserPoolClient
                      - AWS::Cognito::UserPoolGroup
                      - AWS::Cognito::IdentityPool
                      - AWS::S3::Bucket
                      - AWS::S3::BucketPolicy
                      - AWS::KMS::Key
                      - AWS::SecretsManager::Secret
              - Effect: Allow
                Action:
                  - cloudformation:ValidateTemplate
                  - cloudformation:DescribeStackDriftDetectionStatus
                Resource: '*'
              - Sid: ReadConnectionAuthorizers
                Effect: Allow
                Action: ssm:GetParameter
                Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/saaslayer/foundation/connection-authorizers
              - Effect: Allow
                Action: execute-api:Invoke
                Resource:
                  - !Sub arn:${AWS::Partition}:execute-api:us-east-1:${AWS::AccountId}:*/$default/POST/setup
                  - !Sub arn:${AWS::Partition}:execute-api:us-west-2:${AWS::AccountId}:*/$default/POST/setup
              - Effect: Allow
                Action: iam:PassRole
                Resource: !GetAtt SaaSLayerCloudFormationExecutionRole.Arn
                Condition:
                  StringEquals:
                    iam:PassedToService: cloudformation.amazonaws.com
              - Effect: Allow
                Action:
                  - s3:PutObject
                  - s3:DeleteObject
                  - s3:DeleteObjectVersion
                  - s3:ListBucket
                  - s3:ListBucketVersions
                Resource:
                  - !Sub arn:aws:s3:::${StackNamePrefix}*
                  - !Sub arn:aws:s3:::${StackNamePrefix}*/*
              - Sid: ArtifactReads
                Effect: Allow
                Action:
                  - s3:GetObject
                Resource:
                  - !Sub arn:aws:s3:::${StackNamePrefix}artifacts-*/*
                  - !Sub arn:aws:s3:::${StackNamePrefix}*serverlessdeploymentbucket*/*
                  - !Sub arn:aws:s3:::${StackNamePrefix}*/_publications/*
              - Sid: DenyArtifactsBucketDelete
                Effect: Deny
                Action:
                  - s3:DeleteBucket
                  - s3:PutBucketPolicy
                  - s3:DeleteBucketPolicy
                Resource: !Sub arn:aws:s3:::${StackNamePrefix}artifacts-*
              - Sid: CdnInvalidation
                Effect: Allow
                Action:
                  - cloudfront:GetDistribution
                  - cloudfront:CreateInvalidation
                  - cloudfront:GetInvalidation
                Resource: !Sub arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:distribution/*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
                    aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
              - Sid: CdnRecoveryPolicyRead
                Effect: Allow
                Action:
                  - s3:GetBucketPolicy
                Resource: !Sub arn:${AWS::Partition}:s3:::${StackNamePrefix}*
              - Effect: Allow
                Action:
                  - cloudformation:ListStacks
                Resource: '*'
              - Effect: Allow
                Action:
                  - lambda:GetAlias
                  - lambda:GetFunction
                  - lambda:GetFunctionCodeSigningConfig
                  - lambda:GetFunctionConfiguration
                  - lambda:ListAliases
                  - lambda:ListTags
                  - lambda:ListVersionsByFunction
                Resource: !Sub arn:${AWS::Partition}:lambda:*:${AWS::AccountId}:function:${!aws:PrincipalTag/stackPrefix}*
              - Sid: ApiGatewayReads
                Effect: Allow
                Action:
                  - apigateway:GET
                Resource:
                  - !Sub arn:${AWS::Partition}:apigateway:*::/restapis
                  - !Sub arn:${AWS::Partition}:apigateway:*::/restapis/*
                  - !Sub arn:${AWS::Partition}:apigateway:*::/apis
                  - !Sub arn:${AWS::Partition}:apigateway:*::/apis/*
                  - !Sub arn:${AWS::Partition}:apigateway:*::/domainnames
                  - !Sub arn:${AWS::Partition}:apigateway:*::/domainnames/*
              - Sid: CreateManagedDomains
                Effect: Allow
                Action: apigateway:POST
                Resource: !Sub arn:${AWS::Partition}:apigateway:*::/domainnames
                Condition:
                  StringEquals:
                    aws:RequestTag/managed-by: saaslayer-deployer
                    aws:RequestTag/deploymentId: ${aws:PrincipalTag/deploymentId}
              - Sid: ManagedDomainChanges
                Effect: Allow
                Action:
                  - apigateway:DELETE
                  - apigateway:PATCH
                  - apigateway:POST
                Resource: !Sub arn:${AWS::Partition}:apigateway:*::/domainnames/*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
                    aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
              - Sid: ManagedRestApiStages
                Effect: Allow
                Action:
                  - apigateway:PATCH
                  - apigateway:POST
                Resource:
                  - !Sub arn:${AWS::Partition}:apigateway:*::/restapis/*/stages
                  - !Sub arn:${AWS::Partition}:apigateway:*::/restapis/*/stages/*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
                    aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
              - Sid: ApiGatewayStageTags
                Effect: Allow
                Action:
                  - apigateway:DELETE
                  - apigateway:PUT
                Resource: !Sub arn:${AWS::Partition}:apigateway:*::/tags/*
              - Sid: DirectFunctionUpdate
                Effect: Allow
                Action:
                  - lambda:UpdateFunctionCode
                  - lambda:UpdateFunctionConfiguration
                Resource: !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:${!aws:PrincipalTag/stackPrefix}*
              - Sid: DenyDirectLayerAttach
                Effect: Deny
                Action:
                  - lambda:UpdateFunctionConfiguration
                Resource: '*'
                Condition:
                  'Null':
                    lambda:Layer: 'false'
              - Sid: InvokeModuleSeedFunctions
                Effect: Allow
                Action:
                  - lambda:InvokeFunction
                Resource: !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:${!aws:PrincipalTag/stackPrefix}*-seed
              - Effect: Allow
                Action:
                  - cognito-idp:AdminGetUser
                  - cognito-idp:ListUsers
                Resource: !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/*
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
                    aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
              - Effect: Allow
                Action:
                  - dynamodb:GetItem
                  - dynamodb:Query
                  - dynamodb:DeleteTable
                  - dynamodb:UpdateTable
                Resource:
                  - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${!aws:PrincipalTag/stackPrefix}*
                  - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${!aws:PrincipalTag/stackPrefix}*/index/*
              - Effect: Allow
                Action: logs:DescribeLogGroups
                Resource: '*'
              - Effect: Allow
                Action: logs:DeleteLogGroup
                Resource:
                  - !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${!aws:PrincipalTag/stackPrefix}*
              - Effect: Allow
                Action:
                  - sts:AssumeRole
                  - sts:TagSession
                  - sts:SetSourceIdentity
                Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${!aws:PrincipalTag/stackPrefix}*-migrate
              - Effect: Allow
                Action:
                  - ssm:GetParameter
                  - ssm:GetParameters
                  - ssm:PutParameter
                  - ssm:DeleteParameter
                Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/saaslayer/deployment/${!aws:PrincipalTag/deploymentId}/*
              - Effect: Allow
                Action:
                  - kms:Encrypt
                  - kms:Decrypt
                  - kms:GenerateDataKey
                Resource: '*'
                Condition:
                  StringEquals:
                    kms:ViaService: !Sub ssm.${AWS::Region}.amazonaws.com
              - Fn::If:
                  - TenantEdgeEnabled
                  - Effect: Allow
                    Action:
                      - cloudfront:GetDistribution
                      - cloudfront:GetDistributionConfig
                      - cloudfront:ListDistributions
                      - cloudfront:CreateFunction
                      - cloudfront:DescribeFunction
                      - cloudfront:GetFunction
                      - cloudfront:ListFunctions
                    Resource: '*'
                  - !Ref AWS::NoValue
              - Fn::If:
                  - TenantEdgeEnabled
                  - Effect: Allow
                    Action:
                      - cloudfront:CreateDistribution
                      - cloudfront:TagResource
                    Resource: '*'
                    Condition:
                      StringEquals:
                        aws:RequestTag/managed-by: saaslayer-deployer
                        aws:RequestTag/deploymentId: ${aws:PrincipalTag/deploymentId}
                  - !Ref AWS::NoValue
              - Fn::If:
                  - TenantEdgeEnabled
                  - Effect: Allow
                    Action:
                      - cloudfront:DeleteDistribution
                      - cloudfront:UpdateDistribution
                    Resource: !Sub arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:distribution/*
                    Condition:
                      StringEquals:
                        aws:ResourceTag/managed-by: saaslayer-deployer
                        aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
                  - !Ref AWS::NoValue
              - Fn::If:
                  - TenantEdgeEnabled
                  - Effect: Allow
                    Action:
                      - cloudfront:PublishFunction
                      - cloudfront:UpdateFunction
                      - cloudfront:DeleteFunction
                    Resource: !Sub arn:${AWS::Partition}:cloudfront::${AWS::AccountId}:function/${StackNamePrefix}*
                  - !Ref AWS::NoValue

  SaaSLayerDeploymentPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: sl-foundation-deploy-policy
      Description: Recovery-inventory, purge and domain grants for sl-foundation-deploy-role.
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: RecoveryInventoryScopedReads
            Effect: Allow
            Action:
              - dynamodb:DescribeTable
              - dynamodb:DescribeContinuousBackups
              - s3:GetBucketVersioning
              - secretsmanager:DescribeSecret
              - kms:DescribeKey
            Resource:
              - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${!aws:PrincipalTag/stackPrefix}*
              - !Sub arn:aws:s3:::${StackNamePrefix}*
              - !Sub arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:${StackNamePrefix}*
              - !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/*
          - Sid: RecoveryInventoryPoolReads
            Effect: Allow
            Action:
              - cognito-idp:DescribeUserPool
              - cognito-idp:DescribeUserPoolClient
            Resource: !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/*
            Condition:
              StringEquals:
                aws:ResourceTag/managed-by: saaslayer-deployer
                aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
          - Sid: RecoveryInventoryIdentityPoolReads
            Effect: Allow
            Action:
              - cognito-identity:DescribeIdentityPool
            Resource: !Sub arn:aws:cognito-identity:${AWS::Region}:${AWS::AccountId}:identitypool/*
          - Sid: RecoveryInventoryUnscopedReads
            Effect: Allow
            Action:
              - cognito-idp:DescribeUserPoolDomain
              - ssm:DescribeParameters
            Resource: '*'
          - Sid: PurgeScopedDeletes
            Effect: Allow
            Action:
              - dynamodb:DeleteBackup
              - s3:DeleteBucket
              - secretsmanager:DeleteSecret
            Resource:
              - !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${!aws:PrincipalTag/stackPrefix}*/backup/*
              - !Sub arn:aws:s3:::${StackNamePrefix}*
              - !Sub arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:${StackNamePrefix}*
          - Sid: PurgeManagedPools
            Effect: Allow
            Action:
              - cognito-idp:DeleteUserPool
              - cognito-idp:DeleteUserPoolClient
              - cognito-idp:DeleteUserPoolDomain
              - cognito-idp:UpdateUserPool
              - cognito-identity:DeleteIdentityPool
            Resource:
              - !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/*
              - !Sub arn:aws:cognito-identity:${AWS::Region}:${AWS::AccountId}:identitypool/*
            Condition:
              StringEquals:
                aws:ResourceTag/managed-by: saaslayer-deployer
                aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
          - Sid: PurgeManagedKeys
            Effect: Allow
            Action:
              - kms:ScheduleKeyDeletion
            Resource: !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/*
            Condition:
              StringEquals:
                aws:ResourceTag/managed-by: saaslayer-deployer
                aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
              NumericGreaterThanEquals:
                kms:ScheduleKeyDeletionPendingWindowInDays: 7
          - Sid: AdoptSessionRevocation
            Effect: Allow
            Action:
              - cognito-idp:ListUsers
              - cognito-idp:AdminUserGlobalSignOut
            Resource: !Sub arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/*
            Condition:
              StringEquals:
                aws:ResourceTag/managed-by: saaslayer-deployer
                aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
          - Sid: PurgeBackupListing
            Effect: Allow
            Action:
              - dynamodb:ListBackups
            Resource: '*'
          - Sid: LoginDomainQuotaRead
            Effect: Allow
            Action:
              - cognito-idp:ListUserPools
            Resource: '*'
          - Effect: Allow
            Action:
              - iam:CreateServiceLinkedRole
            Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/ops.apigateway.amazonaws.com/AWSServiceRoleForAPIGateway
            Condition:
              StringEquals:
                iam:AWSServiceName: ops.apigateway.amazonaws.com
          - Effect: Allow
            Action:
              - acm:RequestCertificate
            Resource: '*'
            Condition:
              StringEquals:
                aws:RequestTag/managed-by: saaslayer-deployer
                aws:RequestTag/deploymentId: ${aws:PrincipalTag/deploymentId}
          - Effect: Allow
            Action:
              - acm:DescribeCertificate
              - acm:DeleteCertificate
              - acm:ListTagsForCertificate
            Resource: '*'
            Condition:
              StringEquals:
                aws:ResourceTag/managed-by: saaslayer-deployer
                aws:ResourceTag/deploymentId: ${aws:PrincipalTag/deploymentId}
          - Effect: Allow
            Action:
              - acm:AddTagsToCertificate
            Resource: !Sub arn:${AWS::Partition}:acm:${AWS::Region}:${AWS::AccountId}:certificate/*
            Condition:
              StringEquals:
                aws:RequestTag/managed-by: saaslayer-deployer
                aws:RequestTag/deploymentId: ${aws:PrincipalTag/deploymentId}
          - Effect: Allow
            Action:
              - acm:ListCertificates
            Resource: '*'

  SaaSLayerSupportReadOnlyRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: sl-foundation-support-ro-role
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              AWS: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:root
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                sts:ExternalId: !Ref ControlPlaneTrustIdentifiers
                aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:role/${ControlPlaneId}-support
              StringLike:
                sts:RoleSessionName: saaslayer-support-*
          - Effect: Allow
            Principal:
              AWS: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:root
            Action: sts:TagSession
            Condition:
              StringEquals:
                aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:role/${ControlPlaneId}-support
              ForAllValues:StringEquals:
                aws:TagKeys: [ deploymentId, stackPrefix ]
              StringLike:
                aws:RequestTag/deploymentId: dep_?*
                aws:RequestTag/stackPrefix: !Sub '${StackNamePrefix}?*-'
          - Effect: Allow
            Principal:
              AWS: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:root
            Action: sts:SetSourceIdentity
            Condition:
              StringEquals:
                aws:PrincipalArn: !Sub arn:${AWS::Partition}:iam::${ControlPlaneAccountId}:role/${ControlPlaneId}-support
              StringLike:
                sts:SourceIdentity: saaslayer.*
      Policies:
        - PolicyName: saaslayer-support-read-only
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: ReadConnectionAuthorizers
                Effect: Allow
                Action: ssm:GetParameter
                Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/saaslayer/foundation/connection-authorizers
              - Effect: Allow
                Action:
                  - cloudformation:Describe*
                  - cloudformation:GetTemplateSummary
                  - cloudformation:ListStackResources
                Resource:
                  - !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${StackNamePrefix}*
                  - !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/saaslayer-foundation*
                  - !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/saaslayer-host-bootstrap*
              - Effect: Allow
                Action:
                  - cloudformation:ListStacks
                Resource: '*'
              - Effect: Allow
                Action:
                  - iam:ListRoleTags
                Resource: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sl-foundation-*
              - Effect: Allow
                Action: [
                  apigateway:GET, cognito-idp:DescribeUserPool, cognito-idp:GetUserPoolMfaConfig,
                  dynamodb:DescribeContinuousBackups, dynamodb:DescribeTable, dynamodb:DescribeTimeToLive, dynamodb:ListTagsOfResource,
                  events:DescribeEventBus, events:ListTagsForResource,
                  lambda:GetAlias, lambda:GetFunctionEventInvokeConfig, lambda:GetPolicy,
                  lambda:ListAliases, lambda:ListTags, sqs:GetQueueAttributes,
                  sqs:ListQueueTags
                ]
                Resource: '*'
                Condition:
                  StringEquals:
                    aws:ResourceTag/managed-by: saaslayer-deployer
              - Effect: Allow
                Action:
                  - logs:DescribeLogGroups
                  - logs:ListTagsForResource
                Resource: '*'
              - Effect: Allow
                Action:
                  - logs:DescribeLogStreams
                  - logs:GetLogEvents
                  - logs:GetLogGroupFields
                  - logs:GetLogRecord
                Resource: !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${StackNamePrefix}*:*
              - Effect: Allow
                Action:
                  - sms-voice:DescribeAccountAttributes
                  - sms-voice:DescribeConfigurationSets
                  - sms-voice:DescribePhoneNumbers
                  - sms-voice:DescribePools
                  - sms-voice:DescribeProtectConfigurations
                  - sms-voice:DescribeRegistrations
                  - sms-voice:DescribeSenderIds
                  - sms-voice:DescribeSpendLimits
                  - sms-voice:DescribeVerifiedDestinationNumbers
                  - sms-voice:GetProtectConfigurationCountryRuleSet
                Resource: '*'
              - Effect: Allow
                Action:
                  - ses:GetAccount
                Resource: '*'
              - Effect: Allow
                Action:
                  - ses:GetEmailIdentity
                Resource: !Sub arn:${AWS::Partition}:ses:*:${AWS::AccountId}:identity/*
              - Effect: Allow
                Action:
                  - cloudwatch:DescribeAlarms
                  - cloudwatch:GetDashboard
                  - cloudwatch:ListDashboards
                Resource: '*'
              - Sid: DenyDataPlaneReads
                Effect: Deny
                Action: [
                  cloudformation:GetTemplate, cognito-idp:DescribeUserPoolClient, dynamodb:BatchGetItem, dynamodb:GetItem,
                  dynamodb:Query, dynamodb:Scan, lambda:GetFunction, lambda:GetFunctionConfiguration, lambda:InvokeFunction,
                  lambda:ListFunctions, lambda:ListVersionsByFunction, s3:GetObject, s3:GetObjectVersion, secretsmanager:GetSecretValue
                ]
                Resource: '*'
              - Effect: Deny
                Action: apigateway:GET
                Resource:
                  - !Sub arn:${AWS::Partition}:apigateway:*::/apikeys
                  - !Sub arn:${AWS::Partition}:apigateway:*::/apikeys/*
              - Sid: DenySecretValueReads
                Effect: Deny
                Action:
                  - ssm:GetParameter*
                Resource: !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/deployment/*
              - Effect: Deny
                Action:
                  - kms:Decrypt
                Resource: '*'
                Condition:
                  StringLike:
                    kms:ViaService: ssm.*.amazonaws.com
                    kms:EncryptionContext:PARAMETER_ARN: !Sub arn:${AWS::Partition}:ssm:*:${AWS::AccountId}:parameter/saaslayer/deployment/*

  SaaSLayerConnectionAuthorizers:
    Type: AWS::SSM::Parameter
    Properties:
      Name: /saaslayer/foundation/connection-authorizers
      Type: String
      Description: SaaSLayer connection authorizers and managing control planes of this account (no secret).
      Value: !Join
        - ''
        - - '{"v":1,"connectionAuthorizerArns":["'
          - !Join ['","', !Ref ConnectionAuthorizerArns]
          - '"],"controlPlanes":["'
          - !Ref ControlPlaneId
          - '"]}'

Outputs:
  DeploymentRoleArn:
    Description: Deployment role the managing control planes assume (trust identifier required).
    Value: !GetAtt SaaSLayerDeploymentRole.Arn
  SupportReadOnlyRoleArn:
    Description: Read-only role SaaSLayer support assumes (trust identifier required).
    Value: !GetAtt SaaSLayerSupportReadOnlyRole.Arn
  CloudFormationExecutionRoleArn:
    Description: Service role CloudFormation uses for SaaSLayer module stacks.
    Value: !GetAtt SaaSLayerCloudFormationExecutionRole.Arn
  ManagedResourceBoundaryArn:
    Description: Boundary of every IAM role SaaSLayer module stacks create (the scoped boundary).
    Value: !Ref ScopedResourceBoundary
  ScopedResourceBoundaryArn:
    Description: Resource-scoped boundary SaaSLayer module roles move to (9.0.0).
    Value: !Ref ScopedResourceBoundary
  WebIdentityBoundaryArn:
    Description: Boundary of SaaSLayer Cognito identity-pool roles (9.0.0).
    Value: !Ref WebIdentityBoundary
  PermissionMode:
    Description: Permission mode (scoped or broad).
    Value: !Ref PermissionMode
  FoundationVersion:
    Description: Installed foundation version.
    Value: !FindInMap [Meta, Foundation, Version]
  SaaSLayerAccountRef:
    Description: Share with SaaSLayer support to identify this account.
    Value: !Sub
      - 'org=${OrgId} account=${AWS::AccountId} region=${AWS::Region} prefix=${StackNamePrefix} v=${Ver}'
      - Ver: !FindInMap [Meta, Foundation, Version]
