Sha256: 8ca8c2a4eb516e7e29d49bebe8ea3b6d999ac8bc8f0025b1df6e7cc7e48500f6

Contents?: true

Size: 1.59 KB

Versions: 15

Compression:

Stored size: 1.59 KB

Contents

# Resource: Permissions are at the cfn/resource/permission.rb level because the
# principal is determined by the calling or associated resource.
# IE: Principal: events.amazonaws.com for Jets Jobs.
module Jets::Cfn::Resource::Lambda
  class Permission < Jets::Cfn::Base
    def initialize(replacements, associated_resource, options={})
      @replacements = replacements
      @associated_resource = associated_resource
      # allow override for Jets::Cfn::Resource::Lambda::Function::Controller permission
      @principal = options[:Principal]
      @source_arn = options[:SourceArn]
    end

    def definition
      logical_id = permission_logical_id

      definition = {
        logical_id => {
          Type: "AWS::Lambda::Permission",
          Properties: {
            FunctionName: "!Ref {namespace}LambdaFunction",
            Action: "lambda:InvokeFunction",
            Principal: principal
          }
        }
      }

      # From AWS docs: https://amzn.to/2N0QXQL
      # source_arn is "not supported by all event sources"
      definition[logical_id][:Properties][:SourceArn] = source_arn if source_arn

      definition
    end

    def permission_logical_id
      logical_id = "{namespace}Permission"
      md = @associated_resource.logical_id.match(/(\d+)$/)
      counter = md[1] if md
      [logical_id, counter].compact.join('')
    end

    # Auto-detect principal from the associated resources.
    def principal
      @principal || replacer.principal_map(@associated_resource.type)
    end

    def source_arn
      @source_arn || replacer.source_arn_map(@associated_resource.type)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
jets-5.0.13 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.12 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.11 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.10 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.9 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.8 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.7 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.6 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.5 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.4 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.3 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.2 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.1 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.0 lib/jets/cfn/resource/lambda/permission.rb
jets-5.0.0.beta1 lib/jets/cfn/resource/lambda/permission.rb