简体   繁体   English

从 lambda 函数中访问 AWS API?

[英]Accessing AWS APIs from within a lambda function?

I'm writing my first lambda function (in Python), and I'm a little confused about how credentials are supposed to work in the lambda environment.我正在编写我的第一个 lambda 函数(在 Python 中),我对凭据在 lambda 环境中应该如何工作感到有些困惑。 I'm trying to retrieve a secret (for Aurora database access) from the aws secrets api, using their example code which looks something like this (I've stripped out all the error handling for brevity):我正在尝试从 aws secrets api 中检索一个秘密(用于 Aurora 数据库访问),使用他们的示例代码,看起来像这样(为了简洁,我已经删除了所有错误处理):

def get_secret():
    secret_name = 'dbtest-postgres-secret'
    region_name = 'us-east-2'

    session = boto3.session.Session()                                                                                                                                                          
    client = session.client(                                                                                                                                                                   
        service_name='secretsmanager',                                                                                                                                                         
        region_name=region_name                                                                                                                                                                
    )                                                                                                                                                                                          

    get_secret_value_response = client.get_secret_value(                                                                                                                                   
        SecretId=secret_name                                                                                                                                                               
    )                                                                                                                                                                                      

    secret = get_secret_value_response['SecretString']

    return secret

This works fine locally in an environment in which I have my normal AWS credentials, but returns None without raising any errors when running as part of a lambda function.这在我拥有普通 AWS 凭证的环境中在本地运行良好,但在作为 lambda 函数的一部分运行时返回 None 而不会引发任何错误。 I'm using it like this:我是这样使用它的:

def handler(event, context):
    secret = get_secret()
    assert secret is not None

And it's failing at that assert statement:它在该assert声明中失败了:

{
  "errorType": "AssertionError",
  "stackTrace": [
    "  File \"/var/task/dbtest.py\", line nn, in handler\n    assert secret is not None\n"
  ]
}

I assumed that by assigning a role to the lambda function with appropriate permissions ( AmazonRDSDataFullAccess , which includes permissions to access the secrets manager) that everything would be set.我假设通过将角色分配给具有适当权限( AmazonRDSDataFullAccess ,包括访问机密管理器的权限)的 lambda 函数,所有内容都将被设置。 Do I need to provide explicit credentials (eg, an access key and secret) to the lambda function?我是否需要向 lambda 函数提供显式凭据(例如,访问密钥和机密)?

You do not need to give explicit credentials, the lambda will get the credentials from the role you assigned to it.您不需要提供明确的凭据,lambda 将从您分配给它的角色中获取凭据。 What is the secret you are asking for, because the AmazonRDSDataFullAccess only has access to secrets at rds-db-credentials/* .您要求的秘密是什么,因为AmazonRDSDataFullAccess只能访问rds-db-credentials/*秘密。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM