简体   繁体   English

使用 AWS CDK 部署 Lambda function(不作为应用程序的一部分)

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

I am trying with AWS CDK to deploy few lambdas.我正在尝试使用 AWS CDK 部署一些 lambda。 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).所以.. CDK API 中是否有任何选项可以仅创建 function(不是 lambda 应用程序)。 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.狭义的答案是否定的,没有App就没有 CDK,这是 CDK 的 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.文档:AWS CDK 应用程序由称为 Constructs 的构建块组成,它们组合在一起形成堆栈和应用程序。

More broadly, if the question is "I just want a simple lambda with no extra stuff (or cost,)? can I use the CDK?"更广泛地说,如果问题是“我只想要一个简单的 lambda,没有额外的东西(或成本)?我可以使用 CDK 吗?” , then yes, of course. ,那么是的,当然。 The CDK "App" itself is not a deployed resource and has no price. CDK“应用程序”本身不是部署资源,没有价格。

We can demonstrate this with a minimal, lambda-only stack and the generated CloudFormation template cdk deploy sends to AWS.我们可以用一个最小的、仅 lambda 堆栈和生成的 CloudFormation 模板cdk deploy发送到 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 :该模板只有 2 个资源,Lambda 和一个自动创建的AWS::CDK::Metadata用于版本报告

// 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".根据来自 aws 支持的回复“无法使用 CDK 和 CloudFormation 单独创建独立的 lambda function(没有 Lambda 应用程序)无法使用控制台或 AWS CLI 创建。因此,AWS

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. 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您与使用 CDK 和 CloudFormation 部署的 Lambda function 交互的方式,因为您将在 AWS Lambda35 中看到独立的 Lambda 函数。

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].如果您想创建 function 而不创建 Lambda 应用程序,您可以查看使用 AWS 控制台或 AWS CLI [1] 创建 function。 " "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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