AWS-CDK和Powershell Lambda

时间:2019-10-07 12:37:39

标签: amazon-web-services powershell aws-lambda aws-cdk

我有一个Powershell Lambda,我想通过AWS CDK进行部署,但是在运行它时遇到了问题。

通过手动Publish-AWSPowerShellLambda部署Powershell的工作原理:

Publish-AWSPowerShellLambda -ScriptPath .\PowershellLambda.ps1 -Name PowershellLambda

尽管CDK拥有权限,但与CDK一起部署的相同脚本不会记录到CloudWatch Logs:

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

export class LambdaCronStack extends cdk.Stack {
  constructor(app: cdk.App, id: string) {
    super(app, id);

    const lambdaFn = new lambda.Function(this, 'Singleton', {      
      code: new lambda.AssetCode('./PowershellLambda/PowershellLambda.zip'),
      handler: 'PowershellLambda::PowershellLambda.Bootstrap::ExecuteFunction',
      timeout: cdk.Duration.seconds(300),
      runtime: lambda.Runtime.DOTNET_CORE_2_1
    });

    const rule = new events.Rule(this, 'Rule', {
      schedule: events.Schedule.expression('rate(1 minute)')
    });

    rule.addTarget(new targets.LambdaFunction(lambdaFn));
  }
}

const app = new cdk.App();
new LambdaCronStack(app, 'LambdaCronExample');
app.synth();

powershell脚本当前仅包含以下几行,并且在由CLI上的Publish-AWSPowerShellLambda部署时可以运行:

#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.335.0'}
Write-Host "Powershell Lambda Executed" 

注意:对于CDK部署,我使用package.json中的构建步骤生成.zip文件:

 "scripts": {
    "build": "tsc",
    "build-package": "pwsh -NoProfile -ExecutionPolicy Unrestricted -command New-AWSPowerShellLambdaPackage -ScriptPath './PowershellLambda/PowershellLambda.ps1' -OutputPackage ./PowershellLambda/PowershellLambda.zip",
    "watch": "tsc -w",
    "cdk": "cdk"
  }

CDK部署良好,Lambda可以按预期运行,但是Cloudwatch Logs中唯一的事情是: START RequestId:4c12fe1a-a9e0-4137-90cf-747b6aecb639版本:$ LATEST

我检查了CDK脚本中的Handler是否与Publish-AWSPowerShellLambda的输出匹配,并且zip文件上传正常并且包含正确的代码。

关于此功能为何无效的任何建议?

1 个答案:

答案 0 :(得分:1)

在lambda内将内存大小设置为512mb。该功能已解决了该问题。

cloudwatch条目显示lambda开始,但似乎没有足够的内存来初始化和运行.net运行时。