Skip to content

ApiKey 操作

apikey 命令是对云沙箱访问密钥操作的命令:主要包括密钥的创建、查看、列举、更新、重置、删除等功能。

命令解析

当执行命令apikey -h/apikey --help时,可以获取帮助文档。

ApiKey 是 Team 下的资源,因此每个子命令都需要先定位 Team

  • --team-id:直接指定 Team ID;
  • --team-name:指定 Team 名称,未显式传入时回落到 s.yaml 的 props.teamName

定位 ApiKey 自身则用 --api-key-id--api-key-name(Team 内的身份键)。

⚠️ 关于密钥明文(apiKeyValue:它的可见性取决于接口,不是一律看不到。

  • apikey getDescribeApiKey输出的是明文 —— 取回密钥正是这个命令的用途,所以明文丢了不用重置
  • apikey create / reset返回值中是 ***,明文由单独一行 warn 打印;
  • apikey listListApiKeys,该接口本身不返回这个字段,只有 apiKeyMask
  • --debug 日志一律脱敏为 ***s sync 产物绝不包含该字段。

反过来说,apikey get 的输出是凭证,不要接进 CI 日志或在共享终端里执行。完整说明见约束二

IP 名单参数格式

--ip-whitelist / --ip-blacklist 接受两种写法:

# 1. 逗号分隔的 IP / 网段
--ip-whitelist 1.2.3.0/24,4.5.6.7

# 2. JSON 数组(可以带 description)
--ip-whitelist '[{"ipAddress":"1.2.3.0/24","description":"office"}]'

JSON 写法中每一项都必须有 ipAddress 字段,否则报错 Invalid --ip-whitelist: every item requires an "ipAddress" field

apikey create 命令

apikey create 命令,是创建访问密钥的命令。

当执行命令apikey create -h/apikey create --help时,可以获取帮助文档。

参数解析

参数全称 参数缩写 Yaml 模式下必填 Cli 模式下必填 参数含义
region - 选填 必填 地域名称,取值参见支持的地域
team-id - 选填 选填 Team ID,与 --team-name 二选一
team-name - 选填 选填 Team 名称,--team-id 缺省时使用
api-key-name - 必填 必填 ApiKey 名称
expire-time - 选填 选填 过期时间,RFC3339 格式,如 2027-01-01T00:00:00Z
ip-whitelist - 选填 选填 IP 白名单,格式见上文
ip-blacklist - 选填 选填 IP 黑名单,格式同 --ip-whitelist

当前命令还支持部分全局参数(例如-a/--access, --debug等),详情可参考 Serverless Devs 全局参数文档

操作案例

s cli fc-sandbox apikey create --team-name dev --api-key-name ci --region cn-hangzhou -a default

s cli fc-sandbox apikey create --team-name dev --api-key-name ci \
    --expire-time 2027-01-01T00:00:00Z --ip-whitelist 1.2.3.0/24 \
    --region cn-hangzhou -a default

上述命令的执行结果示例:

ApiKey "ci" value: sk-xxxxxxxxxxxxxxxxxxxx
  ⚠️  这是凭证,请妥善保存,不要写进代码仓库或 CI 日志。
      之后可用 `s cli fc-sandbox apikey get --api-key-name <name> --team-name <team>` 再次取回。

apiKeyID:    k-1039xxxxxxxx
apiKeyName:  ci
apiKeyMask:  sk-****abcd
apiKeyValue: '***'
teamID:      t-5lycxxxxxxxx
status:      active
expireTime:  2027-01-01T00:00:00Z
ipWhitelist:
  -
    ipAddress: 1.2.3.0/24

⚠️ 注意:明文只出现在上方那一行 warn 提示里,命令的返回值中 apiKeyValue***,因此不能靠管道或重定向命令输出来保存密钥。需要程序化取值请用 apikey get

apikey get 命令

apikey get 命令,是获取指定访问密钥详情的命令。

当执行命令apikey get -h/apikey get --help时,可以获取帮助文档。

参数解析

参数全称 参数缩写 Yaml 模式下必填 Cli 模式下必填 参数含义
region - 选填 必填 地域名称,取值参见支持的地域
team-id - 选填 选填 Team ID,与 --team-name 二选一
team-name - 选填 选填 Team 名称,--team-id 缺省时使用
api-key-id - 选填 选填 ApiKey ID,与 --api-key-name 二选一
api-key-name - 选填 选填 ApiKey 名称,--api-key-id 缺省时使用

当前命令还支持部分全局参数(例如-a/--access, --debug等),详情可参考 Serverless Devs 全局参数文档

操作案例

s cli fc-sandbox apikey get --team-name dev --api-key-name ci --region cn-hangzhou -a default

上述命令的执行结果示例:

apiKeyID:     k-1039xxxxxxxx
apiKeyName:   ci
apiKeyMask:   sk-****abcd
apiKeyValue:  sk-xxxxxxxxxxxxxxxxxxxx
teamID:       t-5lycxxxxxxxx
status:       active
expireTime:   2027-01-01T00:00:00Z
lastUsedTime: 2026-08-16T09:12:03Z

⚠️ get 走的是 DescribeApiKey输出中的 apiKeyValue 是明文——取回密钥正是这个命令的用途,明文丢了用它取回即可,不需要 reset

也正因如此,这条命令的输出是凭证:不要接进 CI 日志,也不要在共享终端 / 录屏时执行。--debug 日志里该字段仍然是 ***,但命令的正常输出不会脱敏。

apikey list 命令

apikey list 命令,是列举访问密钥列表的命令。

当执行命令apikey list -h/apikey list --help时,可以获取帮助文档。

参数解析

参数全称 参数缩写 Yaml 模式下必填 Cli 模式下必填 参数含义
region - 选填 必填 地域名称,取值参见支持的地域
team-id - 选填 选填 按 Team ID 过滤
team-name - 选填 选填 按 Team 名称过滤,--team-id 缺省时使用
api-key-name - 选填 选填 按 ApiKey 名称过滤
status - 选填 选填 按状态过滤
resource-group-id - 选填 选填 按资源组 ID 过滤
table - 选填 选填 是否以表格形式输出,--table 表示以表格形式输出

当前命令还支持部分全局参数(例如-a/--access, --debug等),详情可参考 Serverless Devs 全局参数文档

操作案例

s cli fc-sandbox apikey list --team-name dev --table --region cn-hangzhou -a default

上述命令的执行结果示例:

-
  apiKeyID:     k-1039xxxxxxxx
  apiKeyName:   ci
  apiKeyMask:   sk-****abcd
  teamID:       t-5lycxxxxxxxx
  status:       active
  expireTime:   2027-01-01T00:00:00Z
  lastUsedTime: 2026-08-16T09:12:03Z

如果指定了--table参数,输出示例:

apiKeyID apiKeyName apiKeyMask teamID status expireTime lastUsedTime
k-1039xxxxxxxx ci sk-****abcd t-5lycxxxxxxxx active 2027-01-01T00:00:00Z 2026-08-16T09:12:03Z

list 内部已做全量翻页(ApiKey 用的是 pageNumber + pageSize 风格)。不指定 Team 时列举当前地域下的全部密钥。

输出里没有 apiKeyValue —— ListApiKeys 本身不返回该字段,只给 apiKeyMask。需要明文请对单个密钥执行 apikey get

apikey update 命令

apikey update 命令,是更新访问密钥配置的命令。

当执行命令apikey update -h/apikey update --help时,可以获取帮助文档。

参数解析

参数全称 参数缩写 Yaml 模式下必填 Cli 模式下必填 参数含义
region - 选填 必填 地域名称,取值参见支持的地域
team-id - 选填 选填 Team ID,与 --team-name 二选一
team-name - 选填 选填 Team 名称,--team-id 缺省时使用
api-key-id - 选填 选填 ApiKey ID,与 --api-key-name 二选一
api-key-name - 选填 选填 ApiKey 名称,--api-key-id 缺省时使用
new-api-key-name - 选填 选填 把 ApiKey 重命名为指定名称
expire-time - 选填 选填 过期时间
status - 选填 选填 期望状态
ip-whitelist - 选填 选填 IP 白名单
ip-blacklist - 选填 选填 IP 黑名单

当前命令还支持部分全局参数(例如-a/--access, --debug等),详情可参考 Serverless Devs 全局参数文档

操作案例

s cli fc-sandbox apikey update --team-name dev --api-key-name ci --status inactive \
    --region cn-hangzhou -a default

注意事项

  • --new-api-key-name / --expire-time / --status / --ip-whitelist / --ip-blacklist 至少要指定一个,否则报错:

    Nothing to update, please specify at least one of --new-api-key-name / --expire-time / --status / --ip-whitelist / --ip-blacklist
    
  • update 不会改变密钥明文。改名后请同步修改 s.yaml 的 apiKeys[].apiKeyName,否则下次 s deploy 会按旧名字新建一个密钥。

apikey reset 命令

apikey reset 命令,是重置访问密钥值的命令。

当执行命令apikey reset -h/apikey reset --help时,可以获取帮助文档。

参数解析

参数全称 参数缩写 Yaml 模式下必填 Cli 模式下必填 参数含义
region - 选填 必填 地域名称,取值参见支持的地域
team-id - 选填 选填 Team ID,与 --team-name 二选一
team-name - 选填 选填 Team 名称,--team-id 缺省时使用
api-key-id - 选填 选填 ApiKey ID,与 --api-key-name 二选一
api-key-name - 选填 选填 ApiKey 名称,--api-key-id 缺省时使用
assume-yes y 选填 选填 在交互时,默认选择y

当前命令还支持部分全局参数(例如-a/--access, --debug等),详情可参考 Serverless Devs 全局参数文档

操作案例

s cli fc-sandbox apikey reset --team-name dev --api-key-name ci --region cn-hangzhou -a default -y

上述命令的执行结果示例:

? Are you sure you want to reset the api key k-1039xxxxxxxx? The old value will stop working immediately. yes

ApiKey "ci" value: sk-yyyyyyyyyyyyyyyyyyyy
  ⚠️  这是凭证,请妥善保存,不要写进代码仓库或 CI 日志。
      之后可用 `s cli fc-sandbox apikey get --api-key-name <name> --team-name <team>` 再次取回。

apiKeyID:    k-1039xxxxxxxx
apiKeyName:  ci
apiKeyMask:  sk-****wxyz
apiKeyValue: '***'
teamID:      t-5lycxxxxxxxx
status:      active

⚠️ 注意:重置会立即失效旧值,请先确认所有使用方都能同步更新。取消确认时不做任何变更,只输出 Skip resetting api key k-1039xxxxxxxx

reset 的用途是轮换凭证(怀疑泄露、定期更换)。如果只是忘了明文,用 apikey get 取回,不要 reset。

apikey remove 命令

apikey remove 命令,是删除访问密钥的命令。

当执行命令apikey remove -h/apikey remove --help时,可以获取帮助文档。

参数解析

参数全称 参数缩写 Yaml 模式下必填 Cli 模式下必填 参数含义
region - 选填 必填 地域名称,取值参见支持的地域
team-id - 选填 选填 Team ID,与 --team-name 二选一
team-name - 选填 选填 Team 名称,--team-id 缺省时使用
api-key-id - 选填 选填 ApiKey ID,与 --api-key-name 二选一
api-key-name - 选填 选填 ApiKey 名称,--api-key-id 缺省时使用
assume-yes y 选填 选填 在交互时,默认选择y

当前命令还支持部分全局参数(例如-a/--access, --debug等),详情可参考 Serverless Devs 全局参数文档

操作案例

s cli fc-sandbox apikey remove --team-name dev --api-key-name ci --region cn-hangzhou -a default -y

上述命令的执行结果示例:

? Are you sure you want to delete the api key k-1039xxxxxxxx? yes
ApiKey k-1039xxxxxxxx removed

权限与策略说明

  • create / update / reset / removeAliyunFCFullAccess
  • get / listAliyunFCReadOnlyAccess