Create (and override) APIKey for an AWS GatewayAPI

Recently we added the APIKey and UsagePlans to one of our endpoints hit by one of our Mobile Apps so that we can monitor its usage and define throttling for specific (macro)users.
You can learn more about UsagePlans here

Add an API Key / Usage Plan to a GatewayAPI

Deployment was done via AWS CDK:

Create the APIKey

const apiKeyName = "my-api-key"
const apiKey = new apigateway.ApiKey(this, `MyAPIkey`, {
                apiKeyName,
                description: `APIKey used by my api to do awesome stuff`,
                enabled: true,
            })

Create the usagePlan for your specific API and deployment stage, and assign the apikey you have just created to it.

const usagePlanProps: apigateway.UsagePlanProps = {
                name: "MyUsagePlan,
                apiKey,
                apiStages: [{api: myRestApi, stage: myRestApi.deploymentStage}],
                throttle: {burstLimit: 500, rateLimit: 1000}, quota: {limit: 10000000, period: Period.MONTH}
    }

Assign the usage Plan to your RestApi.

myRestApi.addUsagePlan("MyUsagePlan", usagePlanProps)

noice

Add a bad idea

At some point, I decided to give our APIkeys a more meaningful name
and deployed our Dev Environment.

The APKs (bundled android apps) currently being tested by the QA people stopped working!
What happened?
The new named caused CloudFormation to delete the current APIKey and create a new one - with of course a new value!

panic

The devs could quickly change the settings in their local environment, but the bundled APPs could not be modified.
Imagine if this happened in production or while the App was in the approval process from Appstore or Google Apps...

Find a solution

Is it possible to replace/override/set the value of an APIKey?
AWS CLI provides an update-api-key method but unfortunately, that did not allow me to change the value of the key itself (only the name, description and enabled properties are editable - like they are in the UIConsole).

Both in the UIConsole and as AWS CLI command it is though possible to Import API Keys
Just pass in a CSV file and the key will be generated.

Name,key,description,Enabled,usageplanIds
MyFirstApiKey,apikey1234abcdefghij0123456789,An imported key,TRUE,c7y23b' 

I quickly did that in the UIConsole to solve the issue for the QA tester and everything worked again.

Something that is worth noticing, though

  • do not try to use the same name when importing: it would work, no overriding, two keys with different id and same name will be attached to the API, but it will be confusing, and if you decide to remove the old one, then you will have problems with next deployments due to CloudFormation IDs conflicting.

As a general rule it is better not to fiddle too much through console or CLI with Resources created via CloudFormation

Of course, this was just temporary and within the next QA build devs will be using only the new API Key (and possibly reference to it at runtime, not build time), but it has been interesting understanding the process and figuring out some measures in case of emergency.

Hope it helps


Photo by CMDR Shane on Unsplash

推荐文章

指向指向指针的指针(错误:表达式必须具有classtype)

指向指向指针的指针(错误:表达式必须具有classtype)

推荐文章

搜索引擎优化地理定位网站的虚拟文件夹

搜索引擎优化地理定位网站的虚拟文件夹

推荐文章

php表单的HTML输出

php表单的HTML输出

推荐文章

将SQL Server Express表迁移到SQL Server数据库

将SQL Server Express表迁移到SQL Server数据库

推荐文章

Android Gridview拖放示例

Android Gridview拖放示例

推荐文章

使变量在中可选下划线.js模板

使变量在中可选下划线.js模板

推荐文章

为什么找不到处理程序引用的“Google App Engine warn me”文件:日常基础.py“一次又一次?

为什么找不到处理程序引用的“Google App Engine warn me”文件:日常基础.py“一次又一次?

推荐文章

检查大型url列表的文件类型的最快方法是什么(以及如何优化我的代码)

检查大型url列表的文件类型的最快方法是什么(以及如何优化我的代码)

推荐文章

弹簧-质量系统的阻尼效应(或这是弹性?)

弹簧-质量系统的阻尼效应(或这是弹性?)

推荐文章

如何在DJango中添加数据然后连接表

如何在DJango中添加数据然后连接表

推荐文章

蟒蛇3-ttk treeview。。。将特定列中的值提取到函数中

蟒蛇3-ttk treeview。。。将特定列中的值提取到函数中

推荐文章

在Windows上开发

在Windows上开发

推荐文章

调用“eval”超时

调用“eval”超时

推荐文章

虚拟机可以访问内部网络,但不能访问internet

虚拟机可以访问内部网络,但不能访问internet

推荐文章

如何使用flex 4构建基于控制台的无窗口AIR应用程序

如何使用flex 4构建基于控制台的无窗口AIR应用程序

推荐文章

如何从托管代码中调试到英特尔C++库?

如何从托管代码中调试到英特尔C++库?