创建 API key 的权限需要在 IAM 用户或角色的策略中指定如下权限:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"apigateway:CreateApiKey",
"apigateway:GetApiKey",
"apigateway:UpdateApiKey",
"apigateway:DeleteApiKey"
],
"Resource": [
"arn:aws:apigateway:{region_name}::/apikeys/*"
]
}
]
}
其中,region_name
需要替换为实际使用的 AWS 区域。
创建 API key 的示例代码:
import boto3
client = boto3.client('apigateway')
response = client.create_api_key(
name='my_api_key',
enabled=True,
stageKeys=[
{
'restApiId': 'rest_api_id',
'stageName': 'prod'
},
],
)
api_key_id = response['id']
print(api_key_id)
其中,rest_api_id
和 stage_name
分别需要替换为实际使用的 API Gateway ID 和 stage 名称。