如何在 AWS Lambda 函数和无服务器框架中最好地管理 Google API 凭证?

问题描述 投票:0回答:1

我正在将 python Flask 应用程序移植到 Lambda 上,并拥有该应用程序使用的 google 服务帐户 json 文件。

我正在使用无服务器框架来管理项目部署。

Google 服务帐户 json 文件通常作为文件从硬盘加载到

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
。据我所知,如果您希望从存储在 AWS 属性存储之类的转义字符串中加载它,还需要 OAuth 提供程序依赖项来处理凭证,我宁愿避免这种情况。

所以我目前正在考虑将google服务帐户json文件存储在lambda函数部署中。

这让我有点不舒服,但我想不出其他方法。

当然,我也不想将 google 服务帐户 json 文件提交到源代码管理。

请教两个问题:

  1. 无服务器框架是否有办法从项目结构之外的位置复制文件并将其包含在部署时的项目结构中,以便我可以在没有 json 文件的情况下将我的项目结构签入源代码管理?

  2. 有更好的方法来处理这个问题吗?

谢谢你。

lambda google-api serverless-framework
1个回答
0
投票

不确定你使用什么语言,但在 python 中 Credentials 类说

服务帐户凭据

Usually, you'll create these credentials with one of the helper
constructors. To create credentials using a Google service account
private key JSON file::

    credentials = service_account.Credentials.from_service_account_file(
        'service-account.json')

Or if you already have the service account file loaded::

    service_account_info = json.load(open('service_account.json'))
    credentials = service_account.Credentials.from_service_account_info(
        service_account_info)

因此,您无需加载文件,只需将 json 文件中的所有内容保存在机密管理器中即可。然后,您访问 lambda 中的值并使用

from_service_account_info
而不是
from_service_account_file

进行身份验证
credentials = service_account.Credentials.from_service_account_info(
            service_account_info)
© www.soinside.com 2019 - 2024. All rights reserved.