gpt4 book ai didi

amazon-web-services - 在 CDK 中配置 lambda 并发

转载 作者:行者123 更新时间:2023-12-04 12:16:58 24 4
gpt4 key购买 nike

我正在尝试通过 CDK 提供 lambda 并发性。我有几个 CfnFunction,我需要配置它们。

只能对别名进行配置。因此,当我将 autoPublishAlias 添加到 CfnFunction 时,APIG 触发器不起作用。

如何通过 CDK 实现(配置)配置?

谢谢,
维诺德。

最佳答案

我想我明白了:

Java 代码

package YOURPACKAGENAMEHERE;

import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import java.util.UUID;
import software.amazon.awscdk.core.Duration;
import software.amazon.awscdk.services.lambda.Alias;
import software.amazon.awscdk.services.lambda.AliasProps;
import software.amazon.awscdk.services.lambda.Code;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.IFunction;
import software.amazon.awscdk.services.lambda.Runtime;
import software.amazon.awscdk.services.lambda.SingletonFunction;
import software.amazon.awscdk.services.lambda.Version;
import software.amazon.awscdk.services.lambda.VersionProps;
import software.amazon.awscdk.services.iam.Role;

public class JavacdexampleStack extends Stack {

public JavacdexampleStack(final Construct scope, final String id) {
super(scope, id);

//create Lambda function
SingletonFunction lambdaFunction =
SingletonFunction.Builder.create(this, "cdk-lambda-example")
.description("Lambda which prints \"I'm running\"")
.code(Code.fromInline("def main(event, context):\n" + " print(\"I'm running!\")\n"))
.handler("index.main")
.timeout(Duration.seconds(300))
.runtime(Runtime.PYTHON_3_7)
.uuid(UUID.randomUUID().toString())
.role(Role.fromRoleArn(this, "roleId", "arn:aws:iam::111122223333:role/116CD745-C8DA-4569-8058-828EFA93E6D7"))//replace with your execution role or create one in cdk
.build();

IFunction lambda = Function.fromFunctionArn(this, "functionId", lambdaFunction.getFunctionArn());

//create version for alias to be assigned to
VersionProps versionprop = new VersionProps.Builder()
.lambda(lambda)
//assign version which the alias points to with provisioned concurrency
.provisionedConcurrentExecutions(5)
.build();

Version version = new Version(this, "versionId", versionprop);

//create alias
AliasProps aliasprops = new AliasProps.Builder()
.aliasName("YourAliasNameHere")
.version(version)
.build();
Alias alias = new Alias(this,"aliasId", aliasprops);

}
}

输出 :

provisionedConcurrencyImage

采取的初步步骤

# assumes npm and mvn are installed
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
npm install -g aws-cdk
cdk --version
# pasting here for visability on which version I used
1.22.0 (build 309ac1b)

mkdir javacdkexample
cd javacdkexample
cdk init --language java

# recommended to run after creating the project from docs
# https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_update
mvn versions:use-latest-versions


pom.xml
<dependencies>
<!-- AWS Cloud Development Kit -->
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>core</artifactId>
<version>1.22.0</version>
</dependency>

<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>lambda</artifactId>
<version>1.22.0</version>
</dependency>

<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>events-targets</artifactId>
<version>1.22.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>

关于amazon-web-services - 在 CDK 中配置 lambda 并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59914633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com