AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template for storyblok s3 backup
Parameters : 
  spaceID:
    Type: Number
  bucketName: 
    Type: String
Resources:
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    Description: Creating Amazon S3 bucket
    Properties:
      BucketName: 
        Ref: bucketName
      AccessControl: Private
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      VersioningConfiguration:
        Status: Enabled
  Role:
    Type: 'AWS::IAM::Role'
    Description: Creating Amazon Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              AWS: arn:aws:iam::211471875135:root
            Action:
              - 'sts:AssumeRole'
            Condition: 
              StringEquals:
                sts:ExternalId: 
                  !Join
                  - '' 
                  - - eu-
                    - !Ref spaceID

      Policies:
        - PolicyName: !Sub 
          - 'storyblok-backup-policy-${SpaceID}'
          - SpaceID: !Ref spaceID
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - 's3:PutObject'
                  - 's3:GetObject'
                  - 's3:DeleteObject'
                Resource: 
                  !Join 
                  - ''
                  - - 'arn:aws:s3:::'
                    - !Ref bucketName
                    - '/*'
              - Effect: Allow
                Action:
                  - 's3:ListBucket'
                Resource: 
                  !Join 
                  - ''
                  - - 'arn:aws:s3:::'
                    - !Ref bucketName
      RoleName: !Sub 
        - 'storyblok-backup-role-${SpaceID}'
        - SpaceID: !Ref spaceID
Outputs:
  S3Bucket:
    Description: Bucket Created
    Value: !Ref S3Bucket
  Role: 
    Description: Role ARN 
    Value: !GetAtt [Role, Arn]