javaamazon-ec2api-keyaws-sdk-java-2.0

aws sdk in ubuntu ec2 instance


I am attempting to bulid a service that takes a webhook containing name, id, contact information and so on, and use the webhook to create an api key in aws api gateway. It is supposed to run in a ubuntu ec2 instance. The issue I have is when the program recives the webhook and about to execute, it goes idle as if it is waiting for more input. I did remote debugging to figure this out. When then code is ran on my local computer it works as expected.

Method call from the Main class where the code doesnt continue from: generator.createAPIKey(util.getIdFromPayload(payload), "created by the webhook");

The method that is called in the generator class:

public void createAPIKey(String name, String descritpion) {
        setKeyValue();
        CreateApiKeyRequest apiKeyRequest = CreateApiKeyRequest.builder()
                .name(name)
                .description(descritpion)
                .enabled(true)
                .value(keyValue)
                .generateDistinctId(true)
                .build();

        response = apigateway.createApiKey(apiKeyRequest);
    }

the response is used to assing a useageplan (which it never reaches)

public void assingUsagePlan(UsagePlan usagePlan) {
        CreateUsagePlanKeyRequest request = CreateUsagePlanKeyRequest.builder()
                   .usagePlanId(usagePlan.getPlanId())
                   .keyId(response.id())
                   .keyType("API_KEY")
                   .build();

        apigateway.createUsagePlanKey(request);
    }

I have uploaded the .aws folder to the instance containing config and credentials files.

Snippet from the pom.xml file

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>${aws.java.sdk.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

<dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.9</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>appsync</artifactId>
            <version>2.20.18</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.12.420</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>apigateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-api-gateway</artifactId>
            <version>1.12.405</version>
        </dependency>

If it is intresting, this is how the id is retrived:

public String getIdFromPayload(String payload){
        JSONObject id = createJsonObj(payload);

        return id.getString("id");
    }

So far have I tried messing around with the credentials, because that could be an issue, but so far no luck. What the program is ultimatly supposed to do is use the webhook id, and generate an api key and see it appear under the useageplan that is passed with the webhook using the aws sdk.

Update: EC2 did not have the necessary permissions to access the API Gateway. That can be a solution to similar problems


Solution

  • The EC2 instance did not have permission to access the API Gateway.