class Version (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.Lambda.Version |
Java | software.amazon.awscdk.services.lambda.Version |
Python | aws_cdk.aws_lambda.Version |
TypeScript (source) | @aws-cdk/aws-lambda » Version |
Implements
IConstruct
, IConstruct
, IDependable
, IResource
, IFunction
, IConnectable
, IGrantable
, IClient
, IVersion
Tag the current state of a Function with a Version number.
Avoid using this resource directly. If you need a Version object, use
function.currentVersion
instead. That will add a Version object to your
template, and make sure the Version is invalidated whenever the Function
object changes. If you use the Version
resource directly, you are
responsible for making sure it is invalidated (by changing its
logical ID) whenever necessary.
Version resources can then be used in Alias
resources to refer to a
particular deployment of a Lambda.
If you want to ensure that you're associating the right version with
the right deployment, specify the codeSha256
property while
creating the `Version.
Example
const lambdaCode = lambda.Code.fromCfnParameters();
const func = new lambda.Function(this, 'Lambda', {
code: lambdaCode,
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
});
// used to make sure each CDK synthesis produces a different Version
const version = func.currentVersion;
const alias = new lambda.Alias(this, 'LambdaAlias', {
aliasName: 'Prod',
version,
});
new codedeploy.LambdaDeploymentGroup(this, 'DeploymentGroup', {
alias,
deploymentConfig: codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
});
Initializer
new Version(scope: Construct, id: string, props: VersionProps)
Parameters
- scope
Construct
- id
string
- props
Version
Props
Construct Props
Name | Type | Description |
---|---|---|
lambda | IFunction | Function to get the value of. |
code | string | SHA256 of the version of the Lambda source code. |
description? | string | Description of the version. |
max | Duration | The maximum age of a request that Lambda sends to a function for processing. |
on | IDestination | The destination for failed invocations. |
on | IDestination | The destination for successful invocations. |
provisioned | number | Specifies a provisioned concurrency configuration for a function's version. |
removal | Removal | Whether to retain old versions of this function when a new version is created. |
retry | number | The maximum number of times to retry when the function returns an error. |
lambda
Type:
IFunction
Function to get the value of.
codeSha256?
Type:
string
(optional, default: No validation is performed)
SHA256 of the version of the Lambda source code.
Specify to validate that you're deploying the right version.
description?
Type:
string
(optional, default: Description of the Lambda)
Description of the version.
maxEventAge?
Type:
Duration
(optional, default: Duration.hours(6))
The maximum age of a request that Lambda sends to a function for processing.
Minimum: 60 seconds Maximum: 6 hours
onFailure?
Type:
IDestination
(optional, default: no destination)
The destination for failed invocations.
onSuccess?
Type:
IDestination
(optional, default: no destination)
The destination for successful invocations.
provisionedConcurrentExecutions?
Type:
number
(optional, default: No provisioned concurrency)
Specifies a provisioned concurrency configuration for a function's version.
removalPolicy?
Type:
Removal
(optional, default: RemovalPolicy.DESTROY)
Whether to retain old versions of this function when a new version is created.
retryAttempts?
Type:
number
(optional, default: 2)
The maximum number of times to retry when the function returns an error.
Minimum: 0 Maximum: 2
Properties
Name | Type | Description |
---|---|---|
architecture | Architecture | The architecture of this Lambda Function. |
can | boolean | Whether the addPermission() call adds any permissions. |
connections | Connections | Access the Connections object. |
edge | string | The ARN of the version for Lambda@Edge. |
env | Resource | The environment this resource belongs to. |
function | string | The ARN fo the function. |
function | string | The name of the function. |
grant | IPrincipal | The principal this Lambda Function is running as. |
is | boolean | Whether or not this Lambda function was bound to a VPC. |
lambda | IFunction | The underlying AWS Lambda function. |
latest | IVersion | The $LATEST version of this function. |
node | Construct | The construct tree node associated with this construct. |
permissions | Construct | The construct node where permissions are attached. |
qualifier | string | The qualifier of the version or alias of this function. |
resource | string[] | The ARN(s) to put into the resource field of the generated IAM policy for grantInvoke(). |
stack | Stack | The stack in which this resource is defined. |
version | string | The most recently deployed version of this function. |
role? | IRole | The IAM role associated with this function. |
architecture
Type:
Architecture
The architecture of this Lambda Function.
canCreatePermissions
Type:
boolean
Whether the addPermission() call adds any permissions.
True for new Lambdas, false for version $LATEST and imported Lambdas from different accounts.
connections
Type:
Connections
Access the Connections object.
Will fail if not a VPC-enabled Lambda Function
edgeArn
Type:
string
The ARN of the version for Lambda@Edge.
env
Type:
Resource
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.
functionArn
Type:
string
The ARN fo the function.
functionName
Type:
string
The name of the function.
grantPrincipal
Type:
IPrincipal
The principal this Lambda Function is running as.
isBoundToVpc
Type:
boolean
Whether or not this Lambda function was bound to a VPC.
If this is is false
, trying to access the connections
object will fail.
lambda
Type:
IFunction
The underlying AWS Lambda function.
latestVersion
Type:
IVersion
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.
node
Type:
Construct
The construct tree node associated with this construct.
permissionsNode
Type:
Construct
The construct node where permissions are attached.
qualifier
Type:
string
The qualifier of the version or alias of this function.
A qualifier is the identifier that's appended to a version or alias ARN.
resourceArnsForGrantInvoke
Type:
string[]
The ARN(s) to put into the resource field of the generated IAM policy for grantInvoke().
stack
Type:
Stack
The stack in which this resource is defined.
version
Type:
string
The most recently deployed version of this function.
role?
Type:
IRole
(optional)
The IAM role associated with this function.
Undefined if the function was imported without a role.
Methods
Name | Description |
---|---|
add | Defines an alias for this version. |
add | Adds an event source to this function. |
add | Adds an event source that maps to this AWS Lambda function. |
add | Adds a url to this lambda function. |
add | Adds a permission to the Lambda resource policy. |
add | Adds a statement to the IAM role assumed by the instance. |
apply | Apply the given removal policy to this resource. |
configure | Configures options for asynchronous invocation. |
consider | A warning will be added to functions under the following conditions: - permissions that include lambda:InvokeFunction are added to the unqualified function. |
grant | Grant the given identity permissions to invoke this Lambda. |
grant | Grant the given identity permissions to invoke this Lambda Function URL. |
metric(metricName, props?) | Return the given named metric for this Function. |
metric | How long execution of this Lambda takes. |
metric | How many invocations of this Lambda fail. |
metric | How often this Lambda is invoked. |
metric | How often this Lambda is throttled. |
to | Returns a string representation of this construct. |
static from | Construct a Version object from a Version ARN. |
static from |
Alias(aliasName, options?)
addpublic addAlias(aliasName: string, options?: AliasOptions): Alias
⚠️ Deprecated: Calling addAlias
on a Version
object will cause the Alias to be replaced on every function update. Call function.addAlias()
or new Alias()
instead.
Parameters
- aliasName
string
— The name of the alias (e.g. "live"). - options
Alias
— Alias options.Options
Returns
Defines an alias for this version.
EventSource(source)
addpublic addEventSource(source: IEventSource): void
Parameters
- source
IEvent
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));
EventSourceMapping(id, options)
addpublic addEventSourceMapping(id: string, options: EventSourceMappingOptions): EventSourceMapping
Parameters
- id
string
- options
Event
Source Mapping Options
Returns
Adds an event source that maps to this AWS Lambda function.
FunctionUrl(options?)
addpublic addFunctionUrl(options?: FunctionUrlOptions): FunctionUrl
Parameters
- options
Function
Url Options
Returns
Adds a url to this lambda function.
Permission(id, permission)
addpublic addPermission(id: string, permission: Permission): void
Parameters
- id
string
— The id for the permission construct. - permission
Permission
— The permission to grant to this Lambda function.
Adds a permission to the Lambda resource policy.
See also: [Permission for details.](Permission for details.)
ToRolePolicy(statement)
addpublic addToRolePolicy(statement: PolicyStatement): void
Parameters
- statement
Policy
Statement
Adds a statement to the IAM role assumed by the instance.
RemovalPolicy(policy)
applypublic applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
Removal
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
).
AsyncInvoke(options)
configurepublic configureAsyncInvoke(options: EventInvokeConfigOptions): void
Parameters
- options
Event
Invoke Config Options
Configures options for asynchronous invocation.
WarningOnInvokeFunctionPermissions(_scope, _action)
considerpublic considerWarningOnInvokeFunctionPermissions(_scope: Construct, _action: string): void
Parameters
- _scope
Construct
- _action
string
A warning will be added to functions under the following conditions: - permissions that include lambda:InvokeFunction
are added to the unqualified function.
- function.currentVersion is invoked before or after the permission is created.
This applies only to permissions on Lambda functions, not versions or aliases. This function is overridden as a noOp for QualifiedFunctionBase.
Invoke(grantee)
grantpublic grantInvoke(grantee: IGrantable): Grant
Parameters
- grantee
IGrantable
Returns
Grant the given identity permissions to invoke this Lambda.
InvokeUrl(grantee)
grantpublic grantInvokeUrl(grantee: IGrantable): Grant
Parameters
- grantee
IGrantable
Returns
Grant the given identity permissions to invoke this Lambda Function URL.
metric(metricName, props?)
public metric(metricName: string, props?: MetricOptions): Metric
Parameters
- metricName
string
- props
Metric
Options
Returns
Return the given named metric for this Function.
Duration(props?)
metricpublic metricDuration(props?: MetricOptions): Metric
Parameters
- props
Metric
Options
Returns
How long execution of this Lambda takes.
Average over 5 minutes
Errors(props?)
metricpublic metricErrors(props?: MetricOptions): Metric
Parameters
- props
Metric
Options
Returns
How many invocations of this Lambda fail.
Sum over 5 minutes
Invocations(props?)
metricpublic metricInvocations(props?: MetricOptions): Metric
Parameters
- props
Metric
Options
Returns
How often this Lambda is invoked.
Sum over 5 minutes
Throttles(props?)
metricpublic metricThrottles(props?: MetricOptions): Metric
Parameters
- props
Metric
Options
Returns
How often this Lambda is throttled.
Sum over 5 minutes
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.
VersionArn(scope, id, versionArn)
static frompublic static fromVersionArn(scope: Construct, id: string, versionArn: string): IVersion
Parameters
- scope
Construct
— The cdk scope creating this resource. - id
string
— The cdk id of this resource. - versionArn
string
— The version ARN to create this version from.
Returns
Construct a Version object from a Version ARN.
VersionAttributes(scope, id, attrs)
static frompublic static fromVersionAttributes(scope: Construct, id: string, attrs: VersionAttributes): IVersion
Parameters
- scope
Construct
- id
string
- attrs
Version
Attributes
Returns