2
votes

The following code will create a new API KEY in AWS API Gateway. Just for fun, I also get an existing usage plan called "Basic" with an id of "1234"

For the life of me I can't find out how to take my newly created API Key and add the existing usage plan to it. This can be done manually on the web portal with the "Add to Usage Plan" button but I want to add my new user to a free plan.

 BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_id, aws_key);

            apiGateway = AmazonApiGatewayClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                    .withRegion(Regions.US_EAST_1).build();

            CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest();
            createApiKeyRequest.setName("awesome company);
            createApiKeyRequest.setEnabled(true);
            createApiKeyRequest.setCustomerId("someid");

            CreateApiKeyResult result = apiGateway.createApiKey(createApiKeyRequest);

            GetUsagePlanRequest getUsagePlanRequest = new GetUsagePlanRequest();
            getUsagePlanRequest.setUsagePlanId("1234");
            GetUsagePlanResult getUsagePlanResult = apiGateway.getUsagePlan(getUsagePlanRequest);

Any AWS SDK experts know how to connect a usage plan to an api key?

2

2 Answers

1
votes

Here's the solution to my post - the key type being "API_KEY" isn't documented anywhere, i found it in some random python sample :/ This creates a new user with an api key and adds them to a usage plan with the api gateway java sdk

 BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_id, aws_key);

            apiGateway = AmazonApiGatewayClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                    .withRegion(Regions.US_EAST_1).build();

            CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest();
            createApiKeyRequest.setName("My awesome new user");
            createApiKeyRequest.setEnabled(true);
            createApiKeyRequest.setCustomerId(UUID.randomUUID().toString());


            CreateApiKeyResult result = apiGateway.createApiKey(createApiKeyRequest);

            GetUsagePlanRequest getUsagePlanRequest = new GetUsagePlanRequest();
            getUsagePlanRequest.setUsagePlanId(BASIC_USAGE_PLAN_ID);

            CreateUsagePlanKeyRequest createUsagePlanKeyRequest = new CreateUsagePlanKeyRequest()
                    .withUsagePlanId(BASIC_USAGE_PLAN_ID);

            createUsagePlanKeyRequest.setKeyId(result.getId());
            createUsagePlanKeyRequest.setKeyType("API_KEY");
            apiGateway.createUsagePlanKey(createUsagePlanKeyRequest);
1
votes

This should maybe be a comment instead, but I made an answer for readability (the key type is documented here).

// Client
AmazonApiGateway client = AmazonApiGatewayClientBuilder.standard().withRegion("my region here").build();

// Create new key
CreateApiKeyRequest keyReq = new CreateApiKeyRequest();
keyReq.setName("key name");
keyReq.setDescription("description");
keyReq.setEnabled(true);
CreateApiKeyResult keyRes = client.createApiKey(keyReq);

// Use existing plan
CreateUsagePlanKeyRequest planReq = new CreateUsagePlanKeyRequest();
planReq.setUsagePlanId("my usage plan id");
planReq.setKeyId(keyRes.getId()); // id from new key
planReq.setKeyType("API_KEY");

// add key to plan
client.createUsagePlanKey(planReq);

Note, this example is without a try-catch block