Show / Hide Table of Contents

Class Function

This construct does not yet reproduce all features from the underlying resource library.

Inheritance
object
Resource
FunctionBase
Function
DockerImageFunction
GoFunction
NodejsFunction
PythonFunction
TriggerFunction
Implements
IFunction
IResource
IConnectable
IGrantable
IFunctionRef
IConstruct
IDependable
IEnvironmentAware
IClientVpnConnectionHandler
Inherited Members
FunctionBase.AddEventSource(IEventSource)
FunctionBase.AddEventSourceMapping(string, IEventSourceMappingOptions)
FunctionBase.AddFunctionUrl(IFunctionUrlOptions)
FunctionBase.AddPermission(string, IPermission)
FunctionBase.AddToRolePolicy(PolicyStatement)
FunctionBase.ConfigureAsyncInvoke(IEventInvokeConfigOptions)
FunctionBase.ConsiderWarningOnInvokeFunctionPermissions(Construct, string)
FunctionBase.GrantInvoke(IGrantable)
FunctionBase.GrantInvokeCompositePrincipal(CompositePrincipal)
FunctionBase.GrantInvokeLatestVersion(IGrantable)
FunctionBase.GrantInvokeUrl(IGrantable)
FunctionBase.GrantInvokeVersion(IGrantable, IVersion)
FunctionBase.Metric(string, IMetricOptions)
FunctionBase.MetricDuration(IMetricOptions)
FunctionBase.MetricErrors(IMetricOptions)
FunctionBase.MetricInvocations(IMetricOptions)
FunctionBase.MetricThrottles(IMetricOptions)
FunctionBase.WarnInvokeFunctionPermissions(Construct)
FunctionBase.Connections
FunctionBase.FunctionRef
FunctionBase.IsBoundToVpc
FunctionBase.LatestVersion
Resource.IsOwnedResource(IConstruct)
Resource.IsResource(IConstruct)
Resource.ApplyRemovalPolicy(RemovalPolicy)
Resource.GeneratePhysicalName()
Resource.GetResourceArnAttribute(string, IArnComponents)
Resource.GetResourceNameAttribute(string)
Resource.With(params IMixin[])
Resource.Env
Resource.PhysicalName
Resource.Stack
Namespace: Amazon.CDK.AWS.Lambda
Assembly: Amazon.CDK.Lib.dll
Syntax (csharp)
public class Function : FunctionBase, IFunction, IResource, IConnectable, IGrantable, IFunctionRef, IConstruct, IDependable, IEnvironmentAware, IClientVpnConnectionHandler
Syntax (vb)
Public Class [Function] Inherits FunctionBase Implements IFunction, IResource, IConnectable, IGrantable, IFunctionRef, IConstruct, IDependable, IEnvironmentAware, IClientVpnConnectionHandler
Remarks

ExampleMetadata: fixture=default infused

Examples
// Create or reference an existing L1 CfnApplicationInferenceProfile
            var cfnProfile = new CfnApplicationInferenceProfile(this, "CfnProfile", new CfnApplicationInferenceProfileProps {
                InferenceProfileName = "my-cfn-profile",
                ModelSource = new InferenceProfileModelSourceProperty {
                    CopyFrom = BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0.InvokableArn
                },
                Description = "Profile created via L1 construct"
            });

            // Import the L1 construct as an L2 ApplicationInferenceProfile
            var importedFromCfn = ApplicationInferenceProfile.FromCfnApplicationInferenceProfile(cfnProfile);

            // Grant permissions to use the imported profile
            var lambdaFunction = new Function(this, "MyFunction", new FunctionProps {
                Runtime = Runtime.PYTHON_3_11,
                Handler = "index.handler",
                Code = Code.FromInline("def handler(event, context): return \"Hello\"")
            });

            importedFromCfn.GrantProfileUsage(lambdaFunction);

Synopsis

Constructors

Function(Construct, string, IFunctionProps)

This construct does not yet reproduce all features from the underlying resource library.

Properties

Architecture

The architecture of this Lambda Function (this is an optional attribute and defaults to X86_64).

CanCreatePermissions

Whether the addPermission() call adds any permissions.

CurrentVersion

Returns a lambda.Version which represents the current version of this Lambda function. A new version will be created every time the function's configuration changes.

DeadLetterQueue

The DLQ (as queue) associated with this Lambda Function (this is an optional attribute).

DeadLetterTopic

The DLQ (as topic) associated with this Lambda Function (this is an optional attribute).

FunctionArn

ARN of this function.

FunctionName

Name of this function.

GrantPrincipal

The principal this Lambda Function is running as.

LogGroup

The LogGroup where the Lambda function's logs are made available.

PROPERTY_INJECTION_ID

Uniquely identifies this class.

PermissionsNode

The construct node where permissions are attached.

ResourceArnsForGrantInvoke

The ARN(s) to put into the resource field of the generated IAM policy for grantInvoke().

Role

Execution role associated with this function.

Runtime

The runtime configured for this lambda.

TenancyConfig

The tenancy configuration for this function.

Timeout

The timeout configured for this lambda.

Methods

AddAlias(string, IAliasOptions?)

Defines an alias for this function.

AddEnvironment(string, string, IEnvironmentOptions?)

Adds an environment variable to this Lambda function.

AddLayers(params ILayerVersion[])

Adds one or more Lambda Layers to this Lambda function.

ClassifyVersionProperty(string, bool)

Record whether specific properties in the AWS::Lambda::Function resource should also be associated to the Version resource.

FromFunctionArn(Construct, string, string)

Import a lambda function into the CDK using its ARN.

FromFunctionAttributes(Construct, string, IFunctionAttributes)

Creates a Lambda function object which represents a function not defined within this stack.

FromFunctionName(Construct, string, string)

Import a lambda function into the CDK using its name.

InvalidateVersionBasedOn(string)

Mix additional information into the hash of the Version object.

MetricAll(string, IMetricOptions?)

Return the given named metric for this Lambda.

MetricAllConcurrentExecutions(IMetricOptions?)

Metric for the number of concurrent executions across all Lambdas.

MetricAllDuration(IMetricOptions?)

Metric for the Duration executing all Lambdas.

MetricAllErrors(IMetricOptions?)

Metric for the number of Errors executing all Lambdas.

MetricAllInvocations(IMetricOptions?)

Metric for the number of invocations of all Lambdas.

MetricAllThrottles(IMetricOptions?)

Metric for the number of throttled invocations of all Lambdas.

MetricAllUnreservedConcurrentExecutions(IMetricOptions?)

Metric for the number of unreserved concurrent executions across all Lambdas.

Constructors

Function(Construct, string, IFunctionProps)

This construct does not yet reproduce all features from the underlying resource library.

public Function(Construct scope, string id, IFunctionProps props)
Parameters
scope Construct
id string
props IFunctionProps
Remarks

ExampleMetadata: fixture=default infused

Examples
// Create or reference an existing L1 CfnApplicationInferenceProfile
            var cfnProfile = new CfnApplicationInferenceProfile(this, "CfnProfile", new CfnApplicationInferenceProfileProps {
                InferenceProfileName = "my-cfn-profile",
                ModelSource = new InferenceProfileModelSourceProperty {
                    CopyFrom = BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0.InvokableArn
                },
                Description = "Profile created via L1 construct"
            });

            // Import the L1 construct as an L2 ApplicationInferenceProfile
            var importedFromCfn = ApplicationInferenceProfile.FromCfnApplicationInferenceProfile(cfnProfile);

            // Grant permissions to use the imported profile
            var lambdaFunction = new Function(this, "MyFunction", new FunctionProps {
                Runtime = Runtime.PYTHON_3_11,
                Handler = "index.handler",
                Code = Code.FromInline("def handler(event, context): return \"Hello\"")
            });

            importedFromCfn.GrantProfileUsage(lambdaFunction);

Properties

Architecture

The architecture of this Lambda Function (this is an optional attribute and defaults to X86_64).

public override Architecture Architecture { get; }
Property Value

Architecture

Overrides
FunctionBase.Architecture
Remarks

ExampleMetadata: fixture=default infused

CanCreatePermissions

Whether the addPermission() call adds any permissions.

protected override bool CanCreatePermissions { get; }
Property Value

bool

Overrides
FunctionBase.CanCreatePermissions
Remarks

True for new Lambdas, false for version $LATEST and imported Lambdas from different accounts.

CurrentVersion

Returns a lambda.Version which represents the current version of this Lambda function. A new version will be created every time the function's configuration changes.

public virtual Version_ CurrentVersion { get; }
Property Value

Version_

Remarks

You can specify options for this version using the currentVersionOptions prop when initializing the lambda.Function.

DeadLetterQueue

The DLQ (as queue) associated with this Lambda Function (this is an optional attribute).

public virtual IQueue? DeadLetterQueue { get; }
Property Value

IQueue

Remarks

ExampleMetadata: fixture=default infused

DeadLetterTopic

The DLQ (as topic) associated with this Lambda Function (this is an optional attribute).

public virtual ITopic? DeadLetterTopic { get; }
Property Value

ITopic

Remarks

ExampleMetadata: fixture=default infused

FunctionArn

ARN of this function.

public override string FunctionArn { get; }
Property Value

string

Overrides
FunctionBase.FunctionArn
Remarks

ExampleMetadata: fixture=default infused

FunctionName

Name of this function.

public override string FunctionName { get; }
Property Value

string

Overrides
FunctionBase.FunctionName
Remarks

ExampleMetadata: fixture=default infused

GrantPrincipal

The principal this Lambda Function is running as.

public override IPrincipal GrantPrincipal { get; }
Property Value

IPrincipal

Overrides
FunctionBase.GrantPrincipal
Remarks

ExampleMetadata: fixture=default infused

LogGroup

The LogGroup where the Lambda function's logs are made available.

public virtual ILogGroup LogGroup { get; }
Property Value

ILogGroup

Remarks

If either logRetention is set or this property is called, a CloudFormation custom resource is added to the stack that pre-creates the log group as part of the stack deployment, if it already doesn't exist, and sets the correct log retention period (never expire, by default).

Further, if the log group already exists and the logRetention is not set, the custom resource will reset the log retention to never expire even if it was configured with a different value.

PROPERTY_INJECTION_ID

Uniquely identifies this class.

public static string PROPERTY_INJECTION_ID { get; }
Property Value

string

Remarks

ExampleMetadata: fixture=default infused

PermissionsNode

The construct node where permissions are attached.

public override Node PermissionsNode { get; }
Property Value

Node

Overrides
FunctionBase.PermissionsNode
Remarks

ExampleMetadata: fixture=default infused

ResourceArnsForGrantInvoke

The ARN(s) to put into the resource field of the generated IAM policy for grantInvoke().

public override string[] ResourceArnsForGrantInvoke { get; }
Property Value

string[]

Overrides
FunctionBase.ResourceArnsForGrantInvoke
Remarks

ExampleMetadata: fixture=default infused

Role

Execution role associated with this function.

public override IRole? Role { get; }
Property Value

IRole

Overrides
FunctionBase.Role
Remarks

ExampleMetadata: fixture=default infused

Runtime

The runtime configured for this lambda.

public virtual Runtime Runtime { get; }
Property Value

Runtime

Remarks

ExampleMetadata: fixture=default infused

TenancyConfig

The tenancy configuration for this function.

public override TenancyConfig? TenancyConfig { get; }
Property Value

TenancyConfig

Overrides
FunctionBase.TenancyConfig
Remarks

ExampleMetadata: fixture=default infused

Timeout

The timeout configured for this lambda.

public virtual Duration? Timeout { get; }
Property Value

Duration

Remarks

ExampleMetadata: fixture=default infused

Methods

AddAlias(string, IAliasOptions?)

Defines an alias for this function.

public virtual Alias AddAlias(string aliasName, IAliasOptions? options = null)
Parameters
aliasName string

The name of the alias.

options IAliasOptions

Alias options.

Returns

Alias

Remarks

The alias will automatically be updated to point to the latest version of the function as it is being updated during a deployment.

Function fn;


fn.AddAlias("Live");

// Is equivalent to

// Is equivalent to
new Alias(this, "AliasLive", new AliasProps {
    AliasName = "Live",
    Version = fn.CurrentVersion
});

AddEnvironment(string, string, IEnvironmentOptions?)

Adds an environment variable to this Lambda function.

public virtual Function AddEnvironment(string key, string value, IEnvironmentOptions? options = null)
Parameters
key string

The environment variable key.

value string

The environment variable's value.

options IEnvironmentOptions

Environment variable options.

Returns

Function

Remarks

If this is a ref to a Lambda function, this operation results in a no-op.

AddLayers(params ILayerVersion[])

Adds one or more Lambda Layers to this Lambda function.

public virtual void AddLayers(params ILayerVersion[] layers)
Parameters
layers ILayerVersion[]

the layers to be added.

Remarks

Throws: if there are already 5 layers on this function, or the layer is incompatible with this function's runtime.

ClassifyVersionProperty(string, bool)

Record whether specific properties in the AWS::Lambda::Function resource should also be associated to the Version resource.

public static void ClassifyVersionProperty(string propertyName, bool locked)
Parameters
propertyName string

The property to classify.

locked bool

whether the property should be associated to the version or not.

Remarks

See 'currentVersion' section in the module README for more details.

FromFunctionArn(Construct, string, string)

Import a lambda function into the CDK using its ARN.

public static IFunction FromFunctionArn(Construct scope, string id, string functionArn)
Parameters
scope Construct
id string
functionArn string
Returns

IFunction

Remarks

For Function.addPermissions() to work on this imported lambda, make sure that is in the same account and region as the stack you are importing it into.

FromFunctionAttributes(Construct, string, IFunctionAttributes)

Creates a Lambda function object which represents a function not defined within this stack.

public static IFunction FromFunctionAttributes(Construct scope, string id, IFunctionAttributes attrs)
Parameters
scope Construct

The parent construct.

id string

The name of the lambda construct.

attrs IFunctionAttributes

the attributes of the function to import.

Returns

IFunction

Remarks

For Function.addPermissions() to work on this imported lambda, set the sameEnvironment property to true if this imported lambda is in the same account and region as the stack you are importing it into.

FromFunctionName(Construct, string, string)

Import a lambda function into the CDK using its name.

public static IFunction FromFunctionName(Construct scope, string id, string functionName)
Parameters
scope Construct
id string
functionName string
Returns

IFunction

Remarks

ExampleMetadata: fixture=default infused

InvalidateVersionBasedOn(string)

Mix additional information into the hash of the Version object.

public virtual void InvalidateVersionBasedOn(string x)
Parameters
x string
Remarks

The Lambda Function construct does its best to automatically create a new Version when anything about the Function changes (its code, its layers, any of the other properties).

However, you can sometimes source information from places that the CDK cannot look into, like the deploy-time values of SSM parameters. In those cases, the CDK would not force the creation of a new Version object when it actually should.

This method can be used to invalidate the current Version object. Pass in any string into this method, and make sure the string changes when you know a new Version needs to be created.

This method may be called more than once.

MetricAll(string, IMetricOptions?)

Return the given named metric for this Lambda.

public static Metric MetricAll(string metricName, IMetricOptions? props = null)
Parameters
metricName string
props IMetricOptions
Returns

Metric

Remarks

ExampleMetadata: fixture=default infused

MetricAllConcurrentExecutions(IMetricOptions?)

Metric for the number of concurrent executions across all Lambdas.

public static Metric MetricAllConcurrentExecutions(IMetricOptions? props = null)
Parameters
props IMetricOptions
Returns

Metric

Remarks

Default: max over 5 minutes

MetricAllDuration(IMetricOptions?)

Metric for the Duration executing all Lambdas.

public static Metric MetricAllDuration(IMetricOptions? props = null)
Parameters
props IMetricOptions
Returns

Metric

Remarks

Default: average over 5 minutes

MetricAllErrors(IMetricOptions?)

Metric for the number of Errors executing all Lambdas.

public static Metric MetricAllErrors(IMetricOptions? props = null)
Parameters
props IMetricOptions
Returns

Metric

Remarks

Default: sum over 5 minutes

MetricAllInvocations(IMetricOptions?)

Metric for the number of invocations of all Lambdas.

public static Metric MetricAllInvocations(IMetricOptions? props = null)
Parameters
props IMetricOptions
Returns

Metric

Remarks

Default: sum over 5 minutes

MetricAllThrottles(IMetricOptions?)

Metric for the number of throttled invocations of all Lambdas.

public static Metric MetricAllThrottles(IMetricOptions? props = null)
Parameters
props IMetricOptions
Returns

Metric

Remarks

Default: sum over 5 minutes

MetricAllUnreservedConcurrentExecutions(IMetricOptions?)

Metric for the number of unreserved concurrent executions across all Lambdas.

public static Metric MetricAllUnreservedConcurrentExecutions(IMetricOptions? props = null)
Parameters
props IMetricOptions
Returns

Metric

Remarks

Default: max over 5 minutes

Implements

IFunction
IResource
IConnectable
IGrantable
IFunctionRef
Constructs.IConstruct
Constructs.IDependable
IEnvironmentAware
IClientVpnConnectionHandler
Back to top Generated by DocFX