Skip to main content

CDK Constructs for AWS Lambda

Project description

AWS Lambda Construct Library


Stability: Stable

This is a developer preview (public beta) module. Releases might lack important features and might have future breaking changes.


This construct library allows you to define AWS Lambda Functions.

import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(this, 'MyFunction', {
    runtime: lambda.Runtime.Nodejs810,
    handler: 'index.handler',
    code: lambda.Code.asset('./lambda-handler'),
});

Handler Code

The lambda.Code class includes static convenience methods for various types of runtime code.

  • lambda.Code.bucket(bucket, key[, objectVersion]) - specify an S3 object that contains the archive of your runtime code.
  • lambda.Code.inline(code) - inline the handle code as a string. This is limited to 4KB.
  • lambda.Code.asset(path) - specify a directory or a .zip file in the local filesystem which will be zipped and uploaded to S3 before deployment.

The following example shows how to define a Python function and deploy the code from the local directory my-lambda-handler to it:

new lambda.Function(this, 'MyLambda', {
  code: lambda.Code.asset(path.join(__dirname, 'my-lambda-handler')),
  handler: 'index.main',
  runtime: lambda.Runtime.PYTHON_3_6
});

When deploying a stack that contains this code, the directory will be zip archived and then uploaded to an S3 bucket, then the exact location of the S3 objects will be passed when the stack is deployed.

Layers

The lambda.LayerVersion class can be used to define Lambda layers and manage granting permissions to other AWS accounts or organizations.

const layer = new lambda.LayerVersion(stack, 'MyLayer', {
  code: lambda.Code.asset(path.join(__dirname, 'layer-code')),
  compatibleRuntimes: [lambda.Runtime.NODEJS_8_10],
  license: 'Apache-2.0',
  description: 'A layer to test the L2 construct',
});

// To grant usage by other AWS accounts
layer.addPermission('remote-account-grant', { accountId: awsAccountId });

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });

new lambda.Function(stack, 'MyLayeredLambda', {
  code: new lambda.InlineCode('foo'),
  handler: 'index.handler',
  runtime: lambda.Runtime.NODEJS_8_10,
  layers: [layer],
});

Event Rule Target

You can use an AWS Lambda function as a target for an Amazon CloudWatch event rule:

import targets = require('@aws-cdk/aws-events-targets');
rule.addTarget(new targets.LambdaFunction(myFunction));

Event Sources

AWS Lambda supports a variety of event sources.

In most cases, it is possible to trigger a function as a result of an event by using one of the onXxx methods on the source construct. For example, the s3.Bucket construct has an onEvent method which can be used to trigger a Lambda when an event, such as PutObject occurs on an S3 bucket.

An alternative way to add event sources to a function is to use function.addEventSource(source). This method accepts an IEventSource object. The module @aws-cdk/aws-lambda-event-sources includes classes for the various event sources supported by AWS Lambda.

For example, the following code adds an SQS queue as an event source for a function:

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

The following code adds an S3 bucket notification as an event source:

import { S3EventSource } from '@aws-cdk/aws-lambda-event-sources';
fn.addEventSource(new S3EventSource(bucket, {
  events: [ s3.EventType.ObjectCreated, s3.EventType.ObjectDeleted ],
  filters: [ { prefix: 'subdir/' } ] // optional
}));

See the documentation for the @aws-cdk/aws-lambda-event-sources module for more details.

Lambda with DLQ

import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(this, 'MyFunction', {
    runtime: lambda.Runtime.Nodejs810,
    handler: 'index.handler',
    code: lambda.Code.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
    deadLetterQueueEnabled: true
});

See the AWS documentation to learn more about AWS Lambdas and DLQs.

Lambda with X-Ray Tracing

import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(this, 'MyFunction', {
    runtime: lambda.Runtime.Nodejs810,
    handler: 'index.handler',
    code: lambda.Code.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
    tracing: lambda.Tracing.Active
});

See the AWS documentation to learn more about AWS Lambda's X-Ray support.

Lambda with Reserved Concurrent Executions

import lambda = require('@aws-cdk/aws-lambda');

const fn = new lambda.Function(this, 'MyFunction', {
    runtime: lambda.Runtime.Nodejs810,
    handler: 'index.handler',
    code: lambda.Code.inline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); }'),
    reservedConcurrentExecutions: 100
});

See the AWS documentation managing concurrency.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-lambda-0.38.0.tar.gz (225.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk.aws_lambda-0.38.0-py3-none-any.whl (223.1 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-lambda-0.38.0.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-lambda-0.38.0.tar.gz
  • Upload date:
  • Size: 225.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-lambda-0.38.0.tar.gz
Algorithm Hash digest
SHA256 b3d8a8a668ffa6229f4d444646d9369148eeaf7c02e48548aabd18a145f7e7db
MD5 553d4ed8588926711403bca9e54747c3
BLAKE2b-256 bbefd0b19460074eebe0a123df3f225257a7be3adeb53bd7fbbae83a0905b37e

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_lambda-0.38.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_lambda-0.38.0-py3-none-any.whl
  • Upload date:
  • Size: 223.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_lambda-0.38.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a398df2a910edb3a29591b11234ae2cf6e527da41d851f7bc59ad9472b3b1b90
MD5 fc94a38f1ac0dd74ea14b2a9ce7a5ed9
BLAKE2b-256 4a616ee5970717f17134d38cfcb4281e1b8042cf558ff5f1a9a769207364a63d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page