
AWS CLIupdate-authorizer实战用 JSON Patch 更新 API Gateway 自定义授权器【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cliAPI Gateway 的 Authorizer授权器是控制 API 方法访问权限的关键组件而aws apigateway update-authorizer命令允许你在不改动整个 RestApi 的情况下对已存在的授权器进行局部更新。本文以 awscli/examples/apigateway/update-authorizer.rst 中的官方示例为主体结合仓库内 API Gateway 服务模型源码系统讲解update-authorizer的命令参数、--patch-operations的 JSON Patch 语法、Authorizer 资源的所有可更新字段以及如何通过底层 PATCH 调用原理进行排错。读完本文你将能够熟练地对 TOKEN / REQUEST / COGNITO_USER_POOLS 三类授权器执行改名、换 Lambda 函数、调整缓存 TTL、修改身份来源等常见运维操作。命令概览update-authorizer是什么update-authorizer是 AWS CLI 中 API Gateway 服务的更新类命令对应的底层 API 操作是UpdateAuthorizer服务端方法为 HTTPPATCH请求路径为/restapis/{restapi_id}/authorizers/{authorizer_id}见 service-2.json。与create-authorizer的整体创建不同update-authorizer采用增量更新模型你不需要重新提交授权器的全部配置只需通过--patch-operations描述在哪里改、怎么改、改成什么。这种设计让更新操作更精准、更省流量也更适合在自动化脚本中对单个属性做微调。命令的基本形态aws apigateway update-authorizer \ --rest-api-id rest-api-id \ --authorizer-id authorizer-id \ --patch-operations opreplace,path/xxx,valueyyy其中--rest-api-id与--authorizer-id是必填参数--patch-operations是核心参数支持在一条命令中传入多个 patch 操作一次性完成多项修改。核心参数详解根据 service-2.json 中UpdateAuthorizerRequest结构的定义本命令共包含三个参数参数必填说明--rest-api-id是授权器所属 RestApi 的字符串标识符位于请求 URI 的restapi_id位置--authorizer-id是要更新的 Authorizer 资源的标识符位于请求 URI 的authorizer_id位置--patch-operations否一组 PATCH 操作每个操作描述一次字段级更新支持重复传入多个操作注意区分两个 ID--rest-api-id是 API 实例RestApi的 ID通常在aws apigateway get-rest-apis的输出中查看--authorizer-id是授权器自身的 ID可通过aws apigateway get-authorizers --rest-api-id id获取对应示例 get-authorizer.rst 中的gfi4n3这类值。理解--patch-operationsAPI Gateway 的 JSON Patch 机制update-authorizer的更新能力完全由PatchOperation结构驱动。在 service-2.json 中PatchOperation定义了四个字段字段说明op操作类型合法值为add、remove、replace、copy。不是所有操作对所有资源都受支持具体取决于资源的运行上下文对不支持的操作发起请求会返回错误path操作目标是一个 JSON Pointer 值指向目标资源内的某个位置。例如资源有可更新属性{name:value}则该属性的 path 为/name若属性值本身是 JSON 对象如{name: {child/name: child-value}}子属性的 path 写作/name/child~1name——即 path 中出现的/必须用~1转义每个 op 只能关联一个 pathvalue更新的目标值适用于add和replace操作。当用 AWS CLI 更新 JSON 值类型的属性时在 Linux shell 中需要用一对单引号包裹整个 JSON 对象例如{a: ...}fromcopy操作的源是一个 JSON Pointer 值指向目标资源内要复制值的位置命令行写法上每个 patch 操作用keyvalue形式表达多个操作之间用空格分隔--patch-operations opreplace,path/name,valuetestAuthorizer \ opreplace,path/authorizerResultTtlInSeconds,value600op 的四种语义replace替换某个属性的值。这是更新 Authorizer 最常用的操作官方示例的两个场景都用它。add添加新属性或新元素。对 Authorizer 而言可用于在未被占用的字段上补充配置。remove删除某个属性例如移除identityValidationExpression校验表达式。copy将目标资源内另一个位置的值复制过来使用from指定源。常见于 Stage 的 canary 部署切换场景Authorizer 更新中较少使用。Authorizer 资源结构可被 patch 的字段全景要写对path必须先弄清 Authorizer 资源本身的结构。Authorizer形状在 service-2.json 中定义其成员即update-authorizer可以 patch 的字段字段path 写法类型与说明name/name授权器名称type/type授权器类型枚举值TOKEN、REQUEST、COGNITO_USER_POOLS见 AuthorizerTypeTOKEN为使用单个授权令牌的 Lambda 授权器REQUEST为基于请求参数的 Lambda 授权器COGNITO_USER_POOLS为基于 Amazon Cognito 用户池的授权器providerARNs/providerARNsCOGNITO_USER_POOLS授权器使用的 Cognito 用户池 ARN 列表格式为arn:aws:cognito-idp:{region}:{account_id}:userpool/{user_pool_id}TOKEN或REQUEST授权器不定义该字段authType/authType可选的用户自定义字段用于 OpenAPI 导入导出无功能影响authorizerUri/authorizerUri授权器 URI。对TOKEN/REQUEST授权器必须是合法的 Lambda 函数 URI形如arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:{account_id}:function:{lambda_function_name}/invocationsauthorizerCredentials/authorizerCredentials授权 API Gateway 调用授权器的 IAM 角色 ARN若使用 Lambda 资源策略授权则置为 nullidentitySource/identitySource身份来源。TOKEN/COGNITO_USER_POOLS授权器为携带令牌的自定义请求头映射表达式如method.request.header.AuthorizationREQUEST授权器为逗号分隔的多个映射表达式如method.request.header.Authorization,context.accountIdidentityValidationExpression/identityValidationExpression传入身份令牌的校验表达式。TOKEN授权器为正则表达式COGNITO_USER_POOLS授权器用正则匹配令牌aud字段不适用于REQUEST授权器authorizerResultTtlInSeconds/authorizerResultTtlInSeconds授权结果缓存 TTL秒。为 0 表示禁用缓存大于 0 表示启用缓存未设置时默认 300最大 36001 小时update-authorizer的返回值就是这个完整的Authorizer结构因此每次更新后你都能在输出中看到授权器的全量最新状态便于确认改动是否生效。实战一更改 Custom Authorizer 的名称原文档示例一展示了最典型的场景——给授权器改名aws apigateway update-authorizer --rest-api-id 1234123412 --authorizer-id gfi4n3 --patch-operations opreplace,path/name,valuetestAuthorizer--rest-api-id 1234123412指定目标 API 实例--authorizer-id gfi4n3指定要改名的授权器--patch-operations opreplace,path/name,valuetestAuthorizer执行一次replace操作把/name属性的值替换为testAuthorizer。命令返回的完整授权器对象节选关键字段{ authType: custom, name: testAuthorizer, authorizerUri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:customAuthorizer/invocations, authorizerResultTtlInSeconds: 300, identitySource: method.request.header.Authorization, type: TOKEN, id: gfi4n3 }从返回值可以读出这个授权器的完整画像type为TOKENidentitySource取自请求头AuthorizationauthorizerResultTtlInSeconds为默认的 300 秒authorizerUri指向customAuthorizer这个 Lambda 函数。注意name已经变为testAuthorizer而id保持不变——更新不会改变授权器 ID这也是增量更新的含义之一。实战二更换 Custom Authorizer 背后的 Lambda 函数原文档示例二演示了更新授权器最关键的生产场景——当你想把授权逻辑从一个 Lambda 函数切换到另一个函数时只需替换authorizerUriaws apigateway update-authorizer --rest-api-id 1234123412 --authorizer-id gfi4n3 --patch-operations opreplace,path/authorizerUri,valuearn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:newAuthorizer/invocations该操作把authorizerUri指向的 Lambda 函数从customAuthorizer更换为newAuthorizer返回结果中只有这一处发生变化{ authType: custom, name: testAuthorizer, authorizerUri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:newAuthorizer/invocations, authorizerResultTtlInSeconds: 300, identitySource: method.request.header.Authorization, type: TOKEN, id: gfi4n3 }理解authorizerUri的组成authorizerUri的通用形式为arn:aws:apigateway:{region}:lambda:path/{service_api}其中{region}与托管 Lambda 函数的区域一致示例中为us-west-2path表示 URI 剩余部分应作为资源路径处理包括开头的/对 Lambda 函数而言通常是/2015-03-31/functions/[FunctionARN]/invocations的形式2015-03-31是 Lambda 的 API 版本标识。因此更换 Lambda 函数时实际只需要替换路径中间的函数 ARN 部分。建议将新旧函数的 ARN 写成变量在脚本中动态拼装 URI避免手工改错aws apigateway update-authorizer \ --rest-api-id 1234123412 \ --authorizer-id gfi4n3 \ --patch-operations opreplace,path/authorizerUri,valuearn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/$NEW_FUNCTION_ARN/invocations更多高频 Patch 操作组合除官方示例覆盖的name与authorizerUri外结合 Authorizer 字段表 可将update-authorizer扩展为授权器在线调参工具。以下为常见组合调整授权结果缓存 TTL例如从默认 300 秒收紧到 60 秒aws apigateway update-authorizer \ --rest-api-id 1234123412 \ --authorizer-id gfi4n3 \ --patch-operations opreplace,path/authorizerResultTtlInSeconds,value60修改身份来源请求头例如令牌从Authorization头改到X-Api-Token头aws apigateway update-authorizer \ --rest-api-id 1234123412 \ --authorizer-id gfi4n3 \ --patch-operations opreplace,path/identitySource,valuemethod.request.header.X-Api-Token一次性执行多个 patch改名 换函数 调 TTL一条命令完成aws apigateway update-authorizer \ --rest-api-id 1234123412 \ --authorizer-id gfi4n3 \ --patch-operations opreplace,path/name,valueauth-v2 \ opreplace,path/authorizerUri,valuearn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:authLambdaV2/invocations \ opreplace,path/authorizerResultTtlInSeconds,value600移除身份校验表达式对 TOKEN 授权器取消正则校验使用removeaws apigateway update-authorizer \ --rest-api-id 1234123412 \ --authorizer-id gfi4n3 \ --patch-operations opremove,path/identityValidationExpression提示当 path 包含/时例如嵌套 JSON 属性需要用~1转义如/name/child~1name使用add或replace且 value 为 JSON 对象时需用单引号包裹整个 JSON 值如value{key: value}。底层原理从 CLI 参数到 PATCH 请求理解update-authorizer的底层机制有助于快速定位参数问题CLI 命令生成AWS CLI 根据 service-2.json 中的服务模型动态生成update-authorizer命令及其参数校验逻辑UpdateAuthorizer操作的http.method被声明为PATCH、requestUri为/restapis/{restapi_id}/authorizers/{authorizer_id}见 service-2.json因此请求最终以 PATCH 方法发出。URI 参数注入restApiId与authorizerId在模型中标记为location: uri会被填充进 URL 路径patchOperations则作为请求体序列化发送。服务端执行API Gateway 收到 PATCH 后按PatchOperation列表依次应用到 Authorizer 资源并返回更新后的完整Authorizer结构。这个模型驱动的设计意味着命令支持的全部参数、path 取值空间都以模型文档为准任何超范围的 op/path 组合都会在服务端被拒绝。常见错误与排错建议NotFoundException404--rest-api-id或--authorizer-id不存在或授权器不属于该 RestApi。用aws apigateway get-authorizers --rest-api-id id核对 ID 再重试。BadRequestException400--patch-operations中op非法、path指向不存在的字段或对当前资源类型使用了不支持的 op。检查 path 拼写与 op 取值add/remove/replace/copy。LimitExceededException/ConflictException触发 API 配额限制或资源状态冲突需要调整限流或解决冲突后重试。更新后未生效确认返回值中目标字段确实已变更若涉及 Lambda 权限或资源策略还需同步检查 Lambda 的调用权限配置authorizerCredentials或 Lambda 资源策略。与相邻命令的组合使用update-authorizer通常与以下命令配合构成完整的授权器生命周期管理仓库内均有配套示例可参考create-authorizer.rst创建 TOKEN / REQUEST / COGNITO_USER_POOLS 三种类型的授权器get-authorizer.rst 与 get-authorizers.rst查询单个或全部授权器获取authorizer-iddelete-authorizer.rst删除不再使用的授权器test-invoke-authorizer.rst在不发起真实请求的情况下测试授权器逻辑flush-stage-authorizers-cache.rst更新授权器配置后若需要立即清空指定阶段的授权器缓存可配合使用。典型工作流为create-authorizer创建 →get-authorizers确认 ID → 后续随业务迭代用update-authorizer持续调整改名、换函数、调 TTL、改身份来源→ 不再需要时delete-authorizer清理。掌握update-authorizer的 JSON Patch 语法与 Authorizer 字段结构即可在不重建 API 的前提下安全、精准地完成授权器的全部在线变更。【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考