gpt4 book ai didi

java - 通过 Java SDK 与 Lambda 函数进行 AWS api 网关集成,必须为 AWS 集成异常指定角色 ARN

转载 作者:行者123 更新时间:2023-12-02 01:02:47 25 4
gpt4 key购买 nike

我尝试使用 Java SDK 将 aws Api gateway 与 Lambda 集成。我能够使用 JAVA SDK 获取目标 API 和相应的 Lambda。

我可以从java SDK获取目标API我可以从帐户中获取 Lambda。但集成代码无法正常工作,并给出 com.amazonaws.services.apigateway.model.BadRequestException: Role ARN必须为AWS集成指定

BasicAWSCredentials awsCreds = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);

    // prepare getway object
AmazonApiGateway gateway = AmazonApiGatewayClientBuilder.standard().withCredentials(
new AWSStaticCredentialsProvider(awsCreds)).withRegion(Regions.US_EAST_1).build();

// find all rest apis
GetRestApisResult restApis = gateway.getRestApis(new GetRestApisRequest());
System.out.println("restApis" + restApis);

// find our predefined api by name
RestApi targetRestApi = restApis.getItems().stream().filter(item -> item.getName().equals("dockertest"))
.findFirst().orElseThrow(RuntimeException::new);
System.out.println(targetRestApi);

List<Resource> resources = gateway.getResources(new GetResourcesRequest().withRestApiId(targetRestApi.getId()))
.getItems();

System.out.println("resources" + resources);
Resource rootResource = resources.stream().filter(resource -> resource.getId().equals("knk0ie")).findFirst()
.orElseThrow(RuntimeException::new);

System.out.println("rootResource" + rootResource);
AWSLambda awsLambda = AWSLambdaClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(
awsCreds)).withRegion(Regions.US_EAST_1).build();
FunctionConfiguration targetFunction = awsLambda.getFunction(new GetFunctionRequest().withFunctionName(
"MyFunction")).getConfiguration();

System.out.println("targetFunction" + targetFunction);

gateway.putIntegration(new PutIntegrationRequest().withCredentials(
null).withIntegrationHttpMethod("GET").withHttpMethod("GET").withType(
IntegrationType.AWS).withRestApiId(
targetRestApi.getId()).withResourceId(rootResource.getId()).withPassthroughBehavior("WHEN_NO_MATCH")
.withUri(
String.format(
"arn:aws:apigateway:xxxxxx:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:xxxxxx:"
+ "function:MyFunction/invocations\"\r\n"
/* + "", targetFunction.getFunctionArn() */)));

最佳答案

我能够通过为 Lamda 执行赋予适当的角色来解决此问题。

关于java - 通过 Java SDK 与 Lambda 函数进行 AWS api 网关集成,必须为 AWS 集成异常指定角色 ARN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755897/

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