必须为AWS集成异常指定通过Java SDK与Lamda函数的AWS API网关集成以提供角色ARN

时间:2019-09-02 11:08:34

标签: java amazon-web-services aws-lambda aws-sdk aws-api-gateway

我尝试使用Java SDK将aws Api网关与Lamda集成。我可以使用JAVA SDK获得目标API和相应的Lamda。

我能够从Java SDK获取目标API 我可以从帐户中获取Lamda。 但是集成代码无法正常工作,并给我com.amazonaws.services.apigateway.model.BadRequestException:必须为AWS集成指定角色ARN

BasicAWSCredentials awsCreds =新的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() */)));

1 个答案:

答案 0 :(得分:0)

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