Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Lambda Layers in lambda.Function construct #1399

Closed
spg opened this issue Dec 19, 2018 · 4 comments
Closed

Add support for Lambda Layers in lambda.Function construct #1399

spg opened this issue Dec 19, 2018 · 4 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved.

Comments

@spg
Copy link
Contributor

spg commented Dec 19, 2018

As of now, adding layers to a lambda function requires jumping through a few hoops:

const entrypoint = new lambda.Function(this, 'MyFunc', {
    runtime: new Runtime("python3.7"),
    code: lambda.Code.asset("my_custom_python_func"),
    handler: "hello.my_handler"
});

const asset = new assets.ZipDirectoryAsset(this, 'SampleAsset', {
    path: path.join(__dirname, '../layers/my-layer')
});

const layer = new cdk.Resource(this, 'MyLayer', {
    type: 'AWS::Lambda::LayerVersion',
    properties: {
        Content: {
            S3Bucket: asset.s3BucketName,
            S3Key: asset.s3ObjectKey,
        }
    }
});

const functionResource = entrypoint.findChild('Resource') as lambda.cloudformation.FunctionResource;
functionResource.propertyOverrides.layers = [layer.ref];

Adding layers support to lambda.FunctionProps would simplify this process.

@eladb
Copy link
Contributor

eladb commented Dec 19, 2018

👍

@eladb eladb added feature-request A feature should be added or improved. @aws-cdk/aws-lambda Related to AWS Lambda labels Dec 19, 2018
@spg spg changed the title Add support for Lambda Layers support in lambda.Function construct Add support for Lambda Layers in lambda.Function construct Dec 19, 2018
@rix0rrr rix0rrr added gap and removed gap labels Jan 4, 2019
@madshargreave
Copy link

👍

@spg
Copy link
Contributor Author

spg commented May 9, 2019

Done in #1411

@spg spg closed this as completed May 9, 2019
@yoslev
Copy link

yoslev commented Jan 13, 2020

The python version of cdk layer deployment for those who need:
import typing

from aws_cdk import (
aws_lambda as _lambda,
core,
aws_iam)
from aws_cdk.aws_iam import PolicyStatement
from aws_cdk.aws_lambda import LayerVersion, AssetCode
from aws_cdk.core import Stack

class CdkLayersStack(core.Stack):

def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
    super().__init__(scope, id, **kwargs)

    # 1) deploy lambda functions
    testLambda : _lambda.Function = CdkLayersStack.cdkResourcesCreate(self)

    # 2) attach policy to function
    projectPolicy = CdkLayersStack.createPolicy(self, testLambda)

# -----------------------------------------------------------------------------------
@staticmethod
def createPolicy(this, testLambda:_lambda.Function) -> None:
    projectPolicy:PolicyStatement = PolicyStatement(
        effect=aws_iam.Effect.ALLOW,
        # resources=["*"],
        resources=[testLambda.function_arn],
        actions=[ "dynamodb:Query",
                  "dynamodb:Scan",
                  "dynamodb:GetItem",
                  "dynamodb:PutItem",
                  "dynamodb:UpdateItem",
                  "states:StartExecution",
                  "states:SendTaskSuccess",
                  "states:SendTaskFailure",
                  "cognito-idp:ListUsers",
                  "ses:SendEmail"
                ]
    )
    return projectPolicy;
# -----------------------------------------------------------------------------------
@staticmethod
def cdkResourcesCreate(self) -> None:
    lambdaFunction:_lambda.Function = _lambda.Function(self, 'testLambda',
                                                       function_name='testLambda',
                                                       handler='testLambda.lambda_handler',
                                                       runtime=_lambda.Runtime.PYTHON_3_7,
                                                       code=_lambda.Code.asset('functions'),
                                                       )
    ac = AssetCode("layers")
    layer  = LayerVersion(self, "l1", code=ac, description="test-layer", layer_version_name='Test-layer-version-name')
    lambdaFunction.add_layers(layer)

    return lambdaFunction
# -----------------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

5 participants