AWSTemplateFormatVersion: '2010-09-09'
Description: PhotoS3
Parameters:
  BucketPrefix:
    Type: String
    Description: Prefix for S3 bucket name
    ConstraintDescription: must be valid S3 bucket characters and prefix+accountnumber+suffix <=63 characters
    Default: photos3-
    MinLength: 0
    MaxLength: 63
    AllowedPattern: '[-_\.a-zA-Z0-9]*'

  BucketSuffix:
    Type: String
    Description: Suffix for S3 bucket name
    ConstraintDescription: must be valid S3 bucket characters and prefix+accountnumber+suffix <=63 characters
    Default: ''
    MinLength: 0
    MaxLength: 63
    AllowedPattern: '[-_\.a-zA-Z0-9]*'

Conditions: {}
Mappings: {}

Outputs:
  UploadBucket:
    Description: S3 bucket for uploads
    Value: !Ref PhotoBucket

Resources:
  PhotoBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub ${BucketPrefix}${AWS::AccountId}${BucketSuffix}
    # TODO Add event notification
    # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-notificationconfig.html

# vim: ts=2:sw=2

AWSTemplateFormatVersion: '2010-09-09'
Description: PhotoS3
Parameters:
  BucketPrefix:
    Type: String
    Description: Prefix for S3 bucket name
    ConstraintDescription: must be valid S3 bucket characters and prefix+accountnumber+suffix <=63 characters
    Default: photos3-
    MinLength: 0
    MaxLength: 63
    AllowedPattern: '[-_\.a-zA-Z0-9]*'

  BucketSuffix:
    Type: String
    Description: Suffix for S3 bucket name
    ConstraintDescription: must be valid S3 bucket characters and prefix+accountnumber+suffix <=63 characters
    Default: ''
    MinLength: 0
    MaxLength: 63
    AllowedPattern: '[-_\.a-zA-Z0-9]*'

Conditions: {}
Mappings: {}

Outputs:
  UploadBucket:
    Description: S3 bucket for uploads
    Value: !Ref PhotoBucket

Resources:
  PhotoBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub ${BucketPrefix}${AWS::AccountId}${BucketSuffix}
      NotificationConfiguration:
        QueueConfigurations:
          - Event: s3:ObjectCreated:*
            Filter:
              S3Key:
                Rules:
                  - Name: prefix
                    Value: source
            Queue: !Sub ${NewImageQueue.Arn}

  NewImageQueue:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 300

  NewImageQueuePolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      Queues:
        - !Ref NewImageQueue
      PolicyDocument:
        Version: 2012-10-17
        Id: S3Publish
        Statement:
          - Sid: S3PublishMessage
            Effect: Allow
            Principal: {Service: s3.amazonaws.com}
            Action: ['SQS:SendMessage']
            Resource: !Sub ${NewImageQueue.Arn}
            Condition:
              ArnLike:
                'aws:SourceArn': !Sub ${PhotoBucket.Arn}

# vim: ts=2:sw=2

Add SQS queue & event notification
Add SQS queue & event notification
