1
votes

I have tried to integrate aws Api gateway with Lamda using Java SDK . I am able to get the target API and respective Lamda using JAVA SDK.

I am able to get the target API from java SDK I am able to get the Lamda from the account . But the integration code is not working and giving me com.amazonaws.services.apigateway.model.BadRequestException: Role ARN must be specified for AWS integration

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() */)));
1

1 Answers

0
votes

I have able to resolve this issue by giving appropriate role for Lamda execution .