AWSTemplateFormatVersion: '2010-09-09'
Parameters:
 #bucketname:
    #Type: String    
    #Default: lakeformationtest
  datalakeAccountid:
    Type: String        
    Default: 838011049881
Resources:
  bucketname:
    Type: AWS::S3::Bucket
  lakeformationpolicy: 
    Type: AWS::S3::BucketPolicy
    Properties: 
      Bucket: 
        Ref: "bucketname"
      PolicyDocument: 
        Statement: 
          - 
            Action: 
              - "s3:GetBucketLocation"
              - "s3:GetObject"
              - "s3:ListBucket"
              - "s3:ListBucketMultipartUploads"
              - "s3:ListMultipartUploadParts"
              - "s3:AbortMultipartUpload"
              - "s3:PutObject"              
            Effect: "Allow"
            Resource: 
              - !Join [ "", [ "arn:aws:s3:::",Ref: "bucketname", "/*" ] ]
              - !Join [ "", [ "arn:aws:s3:::",Ref: "bucketname" ] ]
            Principal: 
              AWS: !Join [ "", [ "arn:aws:iam::",Ref: "datalakeAccountid", ":root" ] ]
  iamrolef5c253f:
    Type: "AWS::IAM::Role"
    Properties:
      Path: "/"
      AssumeRolePolicyDocument: "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"
      MaxSessionDuration: 3600
      Path: /
      Policies:
      - PolicyName: lambdafunc
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Action:
            - s3:Get*
            Resource: 'arn:aws:s3:::amazon-reviews-pds/*'
            Effect: Allow
          - Action:
            - s3:PutObject
            Resource: !Sub 'arn:aws:s3:::${bucketname}/*'
            Effect: Allow
          - Action:
            - logs:CreateLogGroup
            - logs:CreateLogStream
            - logs:PutLogEvents
            Resource: arn:aws:logs:*:*:*
            Effect: Allow

  CopyLambdasFunction:
      Type: AWS::Lambda::Function
      DependsOn: [ "iamrolef5c253f"]
      Properties:
          Role: !GetAtt 'iamrolef5c253f.Arn'
          Runtime: "python2.7"
          Timeout: 13
          Handler: "index.lambda_handler"
          Environment:
            Variables:
              s3destinationBucket: !Ref 'bucketname'
          Code: 
            ZipFile: |
              import cfnresponse
              import boto3
              import os
              def lambda_handler(event, context):
                s3 = boto3.client('s3')
                destinationbucket = os.environ['s3destinationBucket']
                sourceBucket = 'amazon-reviews-pds'
                objectKey = 'tsv/amazon_reviews_us_Camera_v1_00.tsv.gz'
                objectKey2='reviews2/amazon_reviews_us_Camera_v1_00.tsv.gz'
                print(event)
                if event['RequestType'] == "Delete" :
                  cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
                  return ('hello2')
                if event['RequestType'] == 'Create':
                  s3.copy({ 'Bucket': sourceBucket, 'Key': objectKey}, destinationbucket, objectKey2)
                  cfnresponse.send(event, context, cfnresponse.SUCCESS, {})   

  MyCustomResource: 
    Type: "Custom::TestLambdaCrossStackRef"
    DependsOn: ['CopyLambdasFunction']
    Properties: 
      ServiceToken:
        !GetAtt 'CopyLambdasFunction.Arn'