#-----------------------------------------------------------------------#

# Stack Description:
# This is a template that creates an S3 bucket using a combination of
# the profile and function name. This bucket will be used to store
# all serverless deployment templates in the relevant account.

# Stack Purpose:
# - Create S3 Bucket

# Stack Targets:
# - Global stack

# Export Values: 
# - S3 Bucket name 

# Cross-stack Dependencies:
# - Bucket is used to store serverless deployment artifacts.

#-----------------------------------------------------------------------#

service: ${self:custom.ResourceName} 

provider:
  name: aws
  runtime: python3.7
  profile: ${opt:profile, 'default'} 
  region: ${opt:region, 'eu-west-1'}
  stage: ${self:provider.profile}
  deploymentBucket:
    name: ${self:custom.ServerlessBucketName}
    serverSideEncryption: AES256
    blockPublicAccess: true

plugins:
  - serverless-pseudo-parameters
  - serverless-deployment-bucket

custom:
  
  # Combination of profile and function name used as bucket name:
  ResourceName: serverless-framework # Only lowercase letters and numbers allowed. Used to populate bucket name.
  ServerlessBucketName: '${self:custom.ResourceName}-${self:provider.stage}-${self:provider.region}' 

  users-account-num: '572803610270'
  test-datalake-account-num: '652967484765'
  datalake-account-num: '366599434747'
  test-datawarehouse-account-num: '798445658517'
  datawarehouse-account-num: '203074606141'
  dev-account-num: '499475197278'
  analytics-account-num: '884452354923'
  audit-account-num: '466280104154'

resources:

  Resources:
      
    ServerlessBucketPolicy:
      Type: AWS::S3::BucketPolicy
      Properties: 
        Bucket: ${self:custom.ServerlessBucketName}
        PolicyDocument: 
          Statement: [
            {
              "Effect": "Allow",
              "Principal": {
                "AWS": [
                  !Sub "arn:aws:iam::#{AWS::AccountId}:root" 
                ]
              },
              "Action": "s3:*",
              "Resource": !Sub "arn:aws:s3:::${self:custom.ServerlessBucketName}"
            }
          ]

  Outputs:
    ServerlessBucket:
      Description: "Serverless Deployment Bucket name" 
      Value: ${self:custom.ServerlessBucketName}
      Export:
        Name: ${self:custom.ServerlessBucketName}

#-----------------------------------------------------------------------#
