1
votes

When a user creates a new application, I'm also needing to generate an API key from AWS. I need to use a specific Usage Plan (static) to assign to each new Client. I'm getting back the error unexpected value at params[:usage_plan_id] which causes a rescue. Any ideas what I'm missing here?

def create
  @app = App.create(app_params)

  begin

    # ...

    aws_client = Aws::APIGateway::Client.new(
      region: "xxxxxxxxxxx",
      access_key_id: AWS_ACCESS_KEY,
      secret_access_key: AWS_ACCESS_SECRET
    ).create_api_key({
      name: @app.name,
      enabled: true,
      generate_distinct_id: true,
      usage_plan_id: "xxxxxx"
    })

   logger.debug aws_client.inspect

  rescue => e
    logger.error e.message
     # ...
  end

  respond_with(@app)
end
1

1 Answers

1
votes

Here is what needs to happen:

  1. Establish new client connection to AWS API-Gateway.
  2. Create API key for client.
  3. Add client with api_key to usage plan.

in aws config file:

API_GATEWAY = Aws::APIGateway::Client.new(
  region: "xxxxx",
  access_key_id: "xxxxxxxxxxxxxxxx",
  secret_access_key: "xxxxxxxxxxxxxx"
)

in the controller:

aws_api_key = API_GATEWAY.create_api_key({
  name: @app.name,
  enabled: true,
  generate_distinct_id: true,
  stage_keys: [{
    rest_api_id: "xxxxxxx",
    stage_name: "xxxxxxxx"
  }]
})

resp = API_GATEWAY.create_usage_plan_key({
  usage_plan_id: "xxxxxx",
  key_id: aws_api_key.id,
  key_type: "API_KEY"
})