Class: Jets::Stack::S3Event

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/stack/s3_event.rb

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name) ⇒ S3Event

Returns a new instance of S3Event.



3
4
5
# File 'lib/jets/stack/s3_event.rb', line 3

def initialize(bucket_name)
  @bucket_name = bucket_name
end

Instance Method Details

#build_stackObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jets/stack/s3_event.rb', line 15

def build_stack
  # assign to local variable so its available in the block
  bucket = @bucket_name

  Jets::Stack.new_class(stack_name) do
    s3_bucket_configuration(:s3_bucket_configuration,
      service_token: "!GetAtt JetsS3BucketConfig.Arn", # Cannot change this w/o changing the logical id
      # These properties correspond to the ruby aws-sdk s3.put_bucket_notification_configuration
      # in jets/s3_bucket_config.rb, not the CloudFormation Bucket properties. The CloudFormation
      # bucket properties have a similiar structure but is slightly different so it can be confusing.
      #
      #   Ruby aws-sdk S3 Docs: https://amzn.to/2N7m5Lr
      bucket: bucket,
      notification_configuration: Jets.config.s3_event.notification_configuration,
    ) if Jets.config.s3_event.configure_bucket

    # Important note: If we change the name of this function we should also change the
    # logical id of the s3_bucket_configuration custom resource or we'll get this error:
    #   Modifying service token is not allowed.
    function("jets/s3_bucket_config",
      role: "!GetAtt BucketConfigIamRole.Arn",
      layers: ["!Ref GemLayer"],
    )

    sns_topic(:sns_topic)
    sns_topic_policy(:sns_topic_policy,
      policy_document: {
        version: "2012-10-17",
        statement: {
          effect: "Allow",
          principal: { service: "s3.amazonaws.com"},
          action: "sns:Publish",
          resource: "!Ref SnsTopic",
          condition: {
            arn_like: {
              "aws:SourceArn" => "!Sub arn:aws:s3:*:*:#{bucket}"
            }
          }
        }
      },
      topics: ["!Ref SnsTopic"],
    )

    iam_role(:bucket_config_iam_role,
      assume_role_policy_document: {
        version: '2012-10-17',
        statement: [
          effect: "Allow",
          principal: {service: ["lambda.amazonaws.com"]},
          action: ['sts:AssumeRole'],
        ]
      },
      path: "/",
      managed_policy_arns: ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"],
      policies: [
        policy_name: "S3Policy",
        policy_document: {
          version: '2012-10-17',
          statement: [
            effect: "Allow",
            action: [
              's3:GetBucketNotification',
              's3:PutBucketNotification',
            ],
            resource: "*"
          ]
        }
      ]
    )
  end
end

#stack_nameObject

Stack names can only contain alpha numeric chars. Bucket names are limit to 64 chars: amzn.to/2SIzvme Stack names are limit to 128 chars: amzn.to/2SFkrG0 This gsub should handle this.



11
12
13
# File 'lib/jets/stack/s3_event.rb', line 11

def stack_name
  @bucket_name.gsub(/[^0-9a-z\-_]/i, '').gsub('-','_').camelize
end