Jason Strimpel
Jason Strimpel

Reputation: 15476

Create API key in AWS API Gateway from AWS Lambda using boto3

I am using an AWS Lambda function to create an API key using Boto3.

Testing locally with the following is successful:

import boto3

client = boto3.client('apigateway')

response = client.create_api_key(
    name='test_user_from_boto',
    description='This is the description',
    enabled=True,
    generateDistinctId=True,
    value='',
    stageKeys=[{
        'restApiId':'aaa',
        'stageName':'beta'
    }]
)

This works no problem returning a dictionary as expected. The return dictionary includes a value key that has the generated api key value which is what I'm after.

When doing something similar in AWS Lambda, the return dictionary does not include the value key.

This is my Lambda hander function.

import boto3


api_id = 'zzz'
plan_id_map = {
    'trial': 'aaa', 
    'basic': 'bbb', 
    'professional': 'ccc'
}

def handler(event, context):
    user_name = event['user_name']
    stage = event['stage']
    plan = event['plan']

    client = boto3.client('apigateway')
    api_key_response = client.create_api_key(
        name=user_name, 
        description='', 
        enabled=True, 
        # generateDistinctId=True, # including this argument throws an error
        # value='', # including this argument throws an error
        stageKeys=[{
            'restApiId': api_id, 
            'stageName': stage
        }]
    )

    user_key_id = api_key_response['id']
    user_api_key = api_key_response['value'] # throws a key error here

    plan_response = client.create_usage_plan_key(
        usagePlanId=plan_id_map[plan],
        keyId=user_key_id,
        keyType='API_KEY')    

    return {
        'user_name': user_name,
        'user_key_id': user_key_id,
        'user_api_key': user_api_key
    }

The results from printing api_key_response is the following:

{
    u'name': u'test_user_from_lambda', 
    'ResponseMetadata': {
        'HTTPStatusCode': 201, 
        'RequestId': 'b8298d38-7aec-11e6-8322-5bc341fc4b73', 
        'HTTPHeaders': {
            'x-amzn-requestid': 'b8298d38-7aec-11e6-8322-5bc341fc4b73',
            'date': 'Thu, 15 Sep 2016 02:33:00 GMT', 
            'content-length': '203', 
            'content-type': 'application/json'
        }
    },
    u'createdDate': datetime.datetime(2016, 9, 15, 2, 33, tzinfo=tzlocal()), 
    u'lastUpdatedDate': datetime.datetime(2016, 9, 15, 2, 33, tzinfo=tzlocal()), 
    u'enabled': True, 
    u'id': u'xyzxyz', 
    u'stageKeys': [u'abcabc/beta']
}

When attempting to use get_api_key, I get a parameter validation error:

get_api_key_response = client.get_api_key(
    apiKey='585yw0f1tk',
    includeValue=True
)

Unknown parameter in input: "includeValue", must be one of: apiKey: ParamValidationError

Is the AWSboto3 module modified to exclude the value key? How to return the generated api key?

Upvotes: 4

Views: 5512

Answers (1)

RyanG
RyanG

Reputation: 4152

The difference here can be attributed to different versions of the AWS SDK in your Lambda environment vs your development environment.

In newer versions of the SDK, the API key value is omitted from certain responses as a security measure. You can retrieve the API Key value via a separate call to get_api_key with includeValue=True

Upvotes: 3

Related Questions