Create API key in AWS API Gateway from AWS Lambda using boto3
<p>I am using an AWS Lambda function to create an API key using Boto3. </p>

<p>Testing locally with the following is successful:</p>

<pre><code>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'
    }]
)
</code></pre>

<p>This works no problem returning a dictionary as <a href="http://boto3.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.create_api_key" rel="nofollow">expected</a>. The return dictionary includes a <code>value</code> key that has the generated api key value which is what I'm after.</p>

<p>When doing something similar in AWS Lambda, the return dictionary does not include the <code>value</code> key. </p>

<p>This is my Lambda hander function.</p>

<pre><code>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
    }
</code></pre>

<p>The results from printing <code>api_key_response</code> is the following:</p>

<pre><code>{
    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']
}
</code></pre>

<p>When attempting to use <code>get_api_key</code>, I get a parameter validation error:</p>

<pre><code>get_api_key_response = client.get_api_key(
    apiKey='585yw0f1tk',
    includeValue=True
)

Unknown parameter in input: "includeValue", must be one of: apiKey: ParamValidationError
</code></pre>

<p>Is the AWS<code>boto3</code> module modified to exclude the <code>value</code> key? How to return the generated api key?</p>

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

<p>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 <a href="http://boto3.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.get_api_key" rel="nofollow">get_api_key</a> with includeValue=True</p>

python amazon-web-services aws-lambda aws-api-gateway boto3