Class: AWSCDK::Lambda::Code

Inherits:
Jsii::Object
  • Object
show all
Defined in:
lambda/code.rb

Overview

Represents the Lambda Handler Code.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCode

Returns a new instance of Code.



8
9
10
# File 'lambda/code.rb', line 8

def initialize
  Jsii::Object.instance_method(:initialize).bind(self).call
end

Class Method Details

.from_asset(path, options = nil) ⇒ AWSCDK::Lambda::AssetCode

Loads the function code from a local disk path.

Parameters:

Returns:



24
25
26
27
28
29
# File 'lambda/code.rb', line 24

def self.from_asset(path, options = nil)
  Jsii::Type.check_type(path, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "path")
  options = options.is_a?(Hash) ? ::AWSCDK::S3Assets::AssetOptions.new(**options.transform_keys(&:to_sym)) : options
  Jsii::Type.check_type(options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfczNfYXNzZXRzLkFzc2V0T3B0aW9ucyJ9")), "options") unless options.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromAsset", [path, options])
end

.from_asset_image(directory, props = nil) ⇒ AWSCDK::Lambda::AssetImageCode

Create an ECR image from the specified asset and bind it as the Lambda code.

Parameters:

  • directory (String)

    the directory from which the asset must be created.

  • props (AWSCDK::Lambda::AssetImageCodeProps, nil) (defaults to: nil)

    properties to further configure the selected image.

Returns:



36
37
38
39
40
41
# File 'lambda/code.rb', line 36

def self.from_asset_image(directory, props = nil)
  Jsii::Type.check_type(directory, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "directory")
  props = props.is_a?(Hash) ? ::AWSCDK::Lambda::AssetImageCodeProps.new(**props.transform_keys(&:to_sym)) : props
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfbGFtYmRhLkFzc2V0SW1hZ2VDb2RlUHJvcHMifQ==")), "props") unless props.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromAssetImage", [directory, props])
end

.from_bucket(bucket, key, object_version = nil) ⇒ AWSCDK::Lambda::S3Code

Lambda handler code as an S3 object.

Note: If object_version is not defined, the lambda will not be updated automatically if the code in the bucket is updated. This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.

Parameters:

  • bucket (AWSCDK::S3::IBucket)

    The S3 bucket.

  • key (String)

    The object key.

  • object_version (String, nil) (defaults to: nil)

    Optional S3 object version.

Returns:



52
53
54
55
56
57
# File 'lambda/code.rb', line 52

def self.from_bucket(bucket, key, object_version = nil)
  Jsii::Type.check_type(bucket, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfczMuSUJ1Y2tldCJ9")), "bucket")
  Jsii::Type.check_type(key, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "key")
  Jsii::Type.check_type(object_version, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "objectVersion") unless object_version.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromBucket", [bucket, key, object_version])
end

.from_bucket_v2(bucket, key, options = nil) ⇒ AWSCDK::Lambda::S3CodeV2

Lambda handler code as an S3 object.

Note: If options.objectVersion is not defined, the lambda will not be updated automatically if the code in the bucket is updated. This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.

Parameters:

Returns:



68
69
70
71
72
73
74
# File 'lambda/code.rb', line 68

def self.from_bucket_v2(bucket, key, options = nil)
  Jsii::Type.check_type(bucket, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfczMuSUJ1Y2tldCJ9")), "bucket")
  Jsii::Type.check_type(key, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "key")
  options = options.is_a?(Hash) ? ::AWSCDK::Lambda::BucketOptions.new(**options.transform_keys(&:to_sym)) : options
  Jsii::Type.check_type(options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfbGFtYmRhLkJ1Y2tldE9wdGlvbnMifQ==")), "options") unless options.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromBucketV2", [bucket, key, options])
end

.from_cfn_parameters(props = nil) ⇒ AWSCDK::Lambda::CfnParametersCode

Creates a new Lambda source defined using CloudFormation parameters.

Parameters:

Returns:



80
81
82
83
84
# File 'lambda/code.rb', line 80

def self.from_cfn_parameters(props = nil)
  props = props.is_a?(Hash) ? ::AWSCDK::Lambda::CfnParametersCodeProps.new(**props.transform_keys(&:to_sym)) : props
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfbGFtYmRhLkNmblBhcmFtZXRlcnNDb2RlUHJvcHMifQ==")), "props") unless props.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromCfnParameters", [props])
end

.from_custom_command(output, command, options = nil) ⇒ AWSCDK::Lambda::AssetCode

Runs a command to build the code asset that will be used.

Parameters:

  • output (String)

    Where the output of the command will be directed, either a directory or a .zip file with the output Lambda code bundle * For example, if you use the command to run a build script (e.g., [ 'node', 'bundle_code.js' ]), and the build script generates a directory /my/lambda/code containing code that should be ran in a Lambda function, then output should be set to /my/lambda/code.

  • command (Array<String>)

    The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ].

  • options (AWSCDK::Lambda::CustomCommandOptions, nil) (defaults to: nil)

    options for the custom command, and other asset options -- but bundling options are not allowed.

Returns:



92
93
94
95
96
97
98
# File 'lambda/code.rb', line 92

def self.from_custom_command(output, command, options = nil)
  Jsii::Type.check_type(output, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "output")
  Jsii::Type.check_type(command, JSON.parse(Base64.strict_decode64("eyJjb2xsZWN0aW9uIjp7ImVsZW1lbnR0eXBlIjp7InByaW1pdGl2ZSI6InN0cmluZyJ9LCJraW5kIjoiYXJyYXkifX0=")), "command")
  options = options.is_a?(Hash) ? ::AWSCDK::Lambda::CustomCommandOptions.new(**options.transform_keys(&:to_sym)) : options
  Jsii::Type.check_type(options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfbGFtYmRhLkN1c3RvbUNvbW1hbmRPcHRpb25zIn0=")), "options") unless options.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromCustomCommand", [output, command, options])
end

.from_docker_build(path, options = nil) ⇒ AWSCDK::Lambda::AssetCode

Loads the function code from an asset created by a Docker build.

By default, the asset is expected to be located at /asset in the image.

Parameters:

Returns:



108
109
110
111
112
113
# File 'lambda/code.rb', line 108

def self.from_docker_build(path, options = nil)
  Jsii::Type.check_type(path, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "path")
  options = options.is_a?(Hash) ? ::AWSCDK::Lambda::DockerBuildAssetOptions.new(**options.transform_keys(&:to_sym)) : options
  Jsii::Type.check_type(options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfbGFtYmRhLkRvY2tlckJ1aWxkQXNzZXRPcHRpb25zIn0=")), "options") unless options.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromDockerBuild", [path, options])
end

.from_ecr_image(repository, props = nil) ⇒ AWSCDK::Lambda::ECRImageCode

Use an existing ECR image as the Lambda code.

Parameters:

Returns:



120
121
122
123
124
125
# File 'lambda/code.rb', line 120

def self.from_ecr_image(repository, props = nil)
  Jsii::Type.check_type(repository, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfZWNyLklSZXBvc2l0b3J5In0=")), "repository")
  props = props.is_a?(Hash) ? ::AWSCDK::Lambda::ECRImageCodeProps.new(**props.transform_keys(&:to_sym)) : props
  Jsii::Type.check_type(props, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfbGFtYmRhLkVjckltYWdlQ29kZVByb3BzIn0=")), "props") unless props.nil?
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromEcrImage", [repository, props])
end

.from_inline(code) ⇒ AWSCDK::Lambda::InlineCode

Inline code for Lambda handler.

Parameters:

  • code (String)

    The actual handler code (the resulting zip file cannot exceed 4MB).

Returns:



131
132
133
134
# File 'lambda/code.rb', line 131

def self.from_inline(code)
  Jsii::Type.check_type(code, JSON.parse(Base64.strict_decode64("eyJwcmltaXRpdmUiOiJzdHJpbmcifQ==")), "code")
  Jsii::Kernel.instance.call_static("aws-cdk-lib.aws_lambda.Code", "fromInline", [code])
end

.jsii_overridable_methodsObject



12
13
14
15
16
17
# File 'lambda/code.rb', line 12

def self.jsii_overridable_methods
  {
    :bind => { kind: :method, name: "bind", is_optional: false },
    :bind_to_resource => { kind: :method, name: "bindToResource", is_optional: false },
  }
end

Instance Method Details

#bind(scope) ⇒ AWSCDK::Lambda::CodeConfig

Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.

Parameters:

  • scope (Constructs::Construct)

    The binding scope.

Returns:



140
141
142
143
# File 'lambda/code.rb', line 140

def bind(scope)
  Jsii::Type.check_type(scope, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJjb25zdHJ1Y3RzLkNvbnN0cnVjdCJ9")), "scope")
  jsii_call_method("bind", [scope])
end

#bind_to_resource(_resource, _options = nil) ⇒ void

This method returns an undefined value.

Called after the CFN function resource has been created to allow the code class to bind to it.

Specifically it's required to allow assets to add metadata for tooling like SAM CLI to be able to find their origins.

Parameters:



153
154
155
156
157
158
# File 'lambda/code.rb', line 153

def bind_to_resource(_resource, _options = nil)
  Jsii::Type.check_type(_resource, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5DZm5SZXNvdXJjZSJ9")), "_resource")
  _options = _options.is_a?(Hash) ? ::AWSCDK::Lambda::ResourceBindOptions.new(**_options.transform_keys(&:to_sym)) : _options
  Jsii::Type.check_type(_options, JSON.parse(Base64.strict_decode64("eyJmcW4iOiJhd3MtY2RrLWxpYi5hd3NfbGFtYmRhLlJlc291cmNlQmluZE9wdGlvbnMifQ==")), "_options") unless _options.nil?
  jsii_call_method("bindToResource", [_resource, _options])
end