SingletonFunction — AWS Cloud Development Kit 1.204.0 documentation (original) (raw)

class aws_cdk.aws_lambda.SingletonFunction(scope, id, *, uuid, lambda_purpose=None, code, handler, runtime, allow_all_outbound=None, allow_public_subnet=None, architecture=None, architectures=None, code_signing_config=None, current_version_options=None, dead_letter_queue=None, dead_letter_queue_enabled=None, dead_letter_topic=None, description=None, environment=None, environment_encryption=None, ephemeral_storage_size=None, events=None, filesystem=None, function_name=None, initial_policy=None, insights_version=None, layers=None, log_retention=None, log_retention_retry_options=None, log_retention_role=None, memory_size=None, profiling=None, profiling_group=None, reserved_concurrent_executions=None, role=None, security_group=None, security_groups=None, timeout=None, tracing=None, vpc=None, vpc_subnets=None, max_event_age=None, on_failure=None, on_success=None, retry_attempts=None)

Bases: FunctionBase

A Lambda that will only ever be added to a stack once.

This construct is a way to guarantee that the lambda function will be guaranteed to be part of the stack, once and only once, irrespective of how many times the construct is declared to be part of the stack. This is guaranteed as long as the uuid property and the optional lambdaPurpose property stay the same whenever they’re declared into the stack.

Resource:

AWS::Lambda::Function

ExampleMetadata:

fixture=_generated

Example:

The code below shows an example of how to instantiate this type.

The values are placeholders you should change.

import aws_cdk.aws_codeguruprofiler as codeguruprofiler import aws_cdk.aws_ec2 as ec2 import aws_cdk.aws_iam as iam import aws_cdk.aws_kms as kms import aws_cdk.aws_lambda as lambda_ import aws_cdk.aws_logs as logs import aws_cdk.aws_sns as sns import aws_cdk.aws_sqs as sqs import aws_cdk.core as cdk

architecture: lambda.Architecture

code: lambda.Code

code_signing_config: lambda.CodeSigningConfig

destination: lambda.IDestination

event_source: lambda.IEventSource

file_system: lambda.FileSystem

key: kms.Key

lambda_insights_version: lambda.LambdaInsightsVersion

layer_version: lambda.LayerVersion

policy_statement: iam.PolicyStatement

profiling_group: codeguruprofiler.ProfilingGroup

queue: sqs.Queue

role: iam.Role

runtime: lambda.Runtime

security_group: ec2.SecurityGroup

size: cdk.Size

subnet: ec2.Subnet

subnet_filter: ec2.SubnetFilter

topic: sns.Topic

vpc: ec2.Vpc

singleton_function = lambda_.SingletonFunction(self, "MySingletonFunction", code=code, handler="handler", runtime=runtime, uuid="uuid",

# the properties below are optional
allow_all_outbound=False,
allow_public_subnet=False,
architecture=architecture,
architectures=[architecture],
code_signing_config=code_signing_config,
current_version_options=lambda.VersionOptions(
    code_sha256="codeSha256",
    description="description",
    max_event_age=cdk.Duration.minutes(30),
    on_failure=destination,
    on_success=destination,
    provisioned_concurrent_executions=123,
    removal_policy=cdk.RemovalPolicy.DESTROY,
    retry_attempts=123
),
dead_letter_queue=queue,
dead_letter_queue_enabled=False,
dead_letter_topic=topic,
description="description",
environment={
    "environment_key": "environment"
},
environment_encryption=key,
ephemeral_storage_size=size,
events=[event_source],
filesystem=file_system,
function_name="functionName",
initial_policy=[policy_statement],
insights_version=lambda_insights_version,
lambda_purpose="lambdaPurpose",
layers=[layer_version],
log_retention=logs.RetentionDays.ONE_DAY,
log_retention_retry_options=lambda.LogRetentionRetryOptions(
    base=cdk.Duration.minutes(30),
    max_retries=123
),
log_retention_role=role,
max_event_age=cdk.Duration.minutes(30),
memory_size=123,
on_failure=destination,
on_success=destination,
profiling=False,
profiling_group=profiling_group,
reserved_concurrent_executions=123,
retry_attempts=123,
role=role,
security_group=security_group,
security_groups=[security_group],
timeout=cdk.Duration.minutes(30),
tracing=lambda_.Tracing.ACTIVE,
vpc=vpc,
vpc_subnets=ec2.SubnetSelection(
    availability_zones=["availabilityZones"],
    one_per_az=False,
    subnet_filters=[subnet_filter],
    subnet_group_name="subnetGroupName",
    subnet_name="subnetName",
    subnets=[subnet],
    subnet_type=ec2.SubnetType.ISOLATED
)

)

Parameters:

Methods

add_dependency(*up)

Using node.addDependency() does not work on this method as the underlying lambda function is modeled as a singleton across the stack. Use this method instead to declare dependencies.

Parameters:

up (IDependable)

Return type:

None

add_environment(key, value, *, remove_in_edge=None)

Adds an environment variable to this Lambda function.

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

Parameters:

Return type:

Function

add_event_source(source)

Adds an event source to this function.

Event sources are implemented in the @aws-cdk/aws-lambda-event-sources module.

The following example adds an SQS Queue as an event source:

import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources'; myFunction.addEventSource(new SqsEventSource(myQueue));

Parameters:

source (IEventSource)

Return type:

None

add_event_source_mapping(id, *, batch_size=None, bisect_batch_on_error=None, enabled=None, event_source_arn=None, kafka_bootstrap_servers=None, kafka_topic=None, max_batching_window=None, max_record_age=None, on_failure=None, parallelization_factor=None, report_batch_item_failures=None, retry_attempts=None, source_access_configurations=None, starting_position=None, tumbling_window=None)

Adds an event source that maps to this AWS Lambda function.

Parameters:

Return type:

EventSourceMapping

add_function_url(*, auth_type=None, cors=None)

Adds a url to this lambda function.

Parameters:

Return type:

FunctionUrl

add_layers(*layers)

Adds one or more Lambda Layers to this Lambda function.

Parameters:

layers (ILayerVersion) – the layers to be added.

Throws:

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

Return type:

None

add_permission(name, *, principal, action=None, event_source_token=None, function_url_auth_type=None, scope=None, source_account=None, source_arn=None)

Adds a permission to the Lambda resource policy.

Parameters:

Return type:

None

add_to_role_policy(statement)

Adds a statement to the IAM role assumed by the instance.

Parameters:

statement (PolicyStatement)

Return type:

None

apply_removal_policy(policy)

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you’ve removed it from the CDK application or because you’ve made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

Parameters:

policy (RemovalPolicy)

Return type:

None

configure_async_invoke(*, max_event_age=None, on_failure=None, on_success=None, retry_attempts=None)

Configures options for asynchronous invocation.

Parameters:

Return type:

None

consider_warning_on_invoke_function_permissions(scope, action)

A warning will be added to functions under the following conditions: - permissions that include lambda:InvokeFunction are added to the unqualified function.

This applies only to permissions on Lambda functions, not versions or aliases. This function is overridden as a noOp for QualifiedFunctionBase.

Parameters:

Return type:

None

depend_on(down)

The SingletonFunction construct cannot be added as a dependency of another construct using node.addDependency(). Use this method instead to declare this as a dependency of another construct.

Parameters:

down (IConstruct)

Return type:

None

grant_invoke(grantee)

Grant the given identity permissions to invoke this Lambda.

Parameters:

grantee (IGrantable)

Return type:

Grant

grant_invoke_url(grantee)

Grant the given identity permissions to invoke this Lambda Function URL.

Parameters:

grantee (IGrantable)

Return type:

Grant

metric(metric_name, *, account=None, color=None, dimensions=None, dimensions_map=None, label=None, period=None, region=None, statistic=None, unit=None)

Return the given named metric for this Function.

Parameters:

Return type:

Metric

metric_duration(*, account=None, color=None, dimensions=None, dimensions_map=None, label=None, period=None, region=None, statistic=None, unit=None)

How long execution of this Lambda takes.

Average over 5 minutes

Parameters:

Return type:

Metric

metric_errors(*, account=None, color=None, dimensions=None, dimensions_map=None, label=None, period=None, region=None, statistic=None, unit=None)

How many invocations of this Lambda fail.

Sum over 5 minutes

Parameters:

Return type:

Metric

metric_invocations(*, account=None, color=None, dimensions=None, dimensions_map=None, label=None, period=None, region=None, statistic=None, unit=None)

How often this Lambda is invoked.

Sum over 5 minutes

Parameters:

Return type:

Metric

metric_throttles(*, account=None, color=None, dimensions=None, dimensions_map=None, label=None, period=None, region=None, statistic=None, unit=None)

How often this Lambda is throttled.

Sum over 5 minutes

Parameters:

Return type:

Metric

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

architecture

The architecture of this Lambda Function.

connections

Access the Connections object.

Will fail if not a VPC-enabled Lambda Function

Inheritdoc:

true

current_version

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

You can specify options for this version using the currentVersionOptionsprop when initializing the lambda.SingletonFunction.

env

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.

function_arn

The ARN fo the function.

function_name

The name of the function.

grant_principal

The principal this Lambda Function is running as.

is_bound_to_vpc

Whether or not this Lambda function was bound to a VPC.

If this is is false, trying to access the connections object will fail.

Inheritdoc:

true

latest_version

The $LATEST version of this function.

Note that this is reference to a non-specific AWS Lambda version, which means the function this version refers to can return different results in different invocations.

To obtain a reference to an explicit version which references the current function configuration, use lambdaFunction.currentVersion instead.

log_group

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

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.

node

The construct tree node associated with this construct.

permissions_node

The construct node where permissions are attached.

resource_arns_for_grant_invoke

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

role

The IAM role associated with this function.

Undefined if the function was imported without a role.

runtime

The runtime environment for the Lambda function.

stack

The stack in which this resource is defined.

Static Methods

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters:

x (Any)

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct)

Return type:

bool