简体   繁体   中英

Deploying Lambda function (not as part of an Application) using AWS CDK

I am trying with AWS CDK to deploy few lambdas. Code pasted below created a Serverless application also a newly created role associated. But for my use case only standalone functions are required. So.. is there any option in CDK API to create only the function (not lambda application). Could you share your suggestion or experience on this?

    new lambda.Function(this, 'History', {
      functionName: 'History',
      code: lambda.Code.fromAsset(path.join(__dirname, '../lambda/getHistory')),
      handler: 'index.handler',
      runtime: lambda.Runtime.NODEJS_14_X,
      environment: {
        ---;
      },
      timeout: cdk.Duration.seconds(120),
      description: '---',
      // role:
    });

This can be confusing, because "Application" is an overloaded term. The narrow answer is no, no CDK without an App , which is the CDK's root collection of Constructs. But that should not bother you.

Docs : AWS CDK apps are composed of building blocks known as Constructs, which are composed together to form stacks and apps.

More broadly, if the question is "I just want a simple lambda with no extra stuff (or cost,)? can I use the CDK?" , then yes, of course. The CDK "App" itself is not a deployed resource and has no price.

We can demonstrate this with a minimal, lambda-only stack and the generated CloudFormation template cdk deploy sends to AWS.

// a minimial lambda stack
// Only a lambda will be created.  Not even a role, because we are reusing an existing one (not a best practice, just for illustration)
export class MinimalStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: cdk.StackProps) {
    super(scope, id, props);

    new lambda.Function(this, 'MyPlaygroundFunction', {
      code: new lambda.InlineCode("exports.handler = () => console.log('hello lambda')"),
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: 'index.handler',
      role: iam.Role.fromRoleArn(this, 'MyExistingRole', 'arn:aws:iam::xxxxxxxx8348350:role/xxxxxxxxxxxxx-GI5QDFDJWT0A'),
    });
  }
}

The template has just 2 resources, the Lambda and an auto-created AWS::CDK::Metadata used for version reporting :

// CloudFormation template Resource section - the template will be in the cdk.out folder
  "Resources": {
    "MyPlaygroundFunctionBAD1926E": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": { "ZipFile": "exports.handler = () => console.log('hello lambda')"},
        "Role": "arn:aws:iam::xxxxxxxx8348350:role/xxxxxxxxxxxxx-GI5QDFDJWT0A",
        "Handler": "index.handler",
        "Runtime": "nodejs14.x"
      },
      "Metadata": {"aws:cdk:path": "TsCdkPlaygroundMinimalStack/MyPlaygroundFunction/Resource"}
    },
    "CDKMetadata": {
      "Type": "AWS::CDK::Metadata",
      "Properties": { "Analytics": "v2:deflate64:H4sAAAAA/zWK...."},
      "Metadata": { "aws:cdk:path": "TsCdkPlaygroundMinimalStack/CDKMetadata/Default"}
    }
  },

I missed to update on this. Based on the reply from aws support "Standalone lambda function alone (without Lambda application) can not be created by using CDK & CloudFormation. So, either AWS Console or AWS CLI has to be used".

Pasting extracted content, "From your latest correspondence, a standalone lambda function can be created using the CDK & CloudFormation however, a Lambda application is also created that contains any relevant resources that work with your Lambda function. As previously discussed, this does not change the way in which you interact with a Lambda function that you deploy using the CDK and CloudFormation as you will see the standalone Lambda functions in your AWS Lambda Console.

If you would like to create the function without creating a Lambda application, you can look at creating a function using the AWS Console or AWS CLI [1]. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM