当前位置: 首页 > 知识库问答 >
问题:

AWS Lambda从VPC中访问机密管理器

陆和泰
2023-03-14

使用Secrets Manager提供的默认代码和必要的IAM角色,我可以在我的lambda中从Secrets Manager中读取API密钥:

# Use this code snippet in your app.
# If you need more information about configurations or implementing the sample code, visit the AWS docs:   
# https://aws.amazon.com/developers/getting-started/python/

import boto3
import base64
from botocore.exceptions import ClientError

def get_secret():

    secret_name = "MYSECRET"
    region_name = "ap-southeast-2"

    # Create a Secrets Manager client
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )

    # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
    # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    # We rethrow the exception by default.

    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
 ... # Default error handling..
    else:
        # Decrypts secret using the associated KMS CMK.
        # Depending on whether the secret is a string or binary, one of these fields will be populated.
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
            return secret
        else:
            decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
            return decoded_binary_secret

def lambda_handler(event, context):
    secrt = get_secret()

    return {
        'statusCode': 200,
        "headers": {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json'
        },
        'body': secrt 
    }             

这个Lambda能够成功地从Secrets Manager中检索和打印API密钥。

为了与EC2实例通信,我有一个带有助手层和一些简单测试代码的Lambda:

import apihelper
import json

def lambda_handler(event, context):
  conn = apihelper.getConnection('API KEY')
  return {
    'statusCode': 200,
    "headers": {
      "Access-Control-Allow-Origin": "*"
    },
    'body': json.dumps(conn.listProducts())
  }
    null

我想我已经把范围缩小到VPC了。第一个Lambda只是打印出秘密工作完美,直到我把它放在VPC中。但是我不知道该在哪里查看,也不知道如何配置它,以允许Lambda与VPC内部的EC2以及秘密管理器进行对话。

共有1个答案

劳嘉实
2023-03-14

没有错误信息,只是超时了。

不幸的是,VPC中的lambda函数既没有internet访问,也没有公共IP。来自文档:

将函数连接到公共子网不会给它internet访问或公共IP地址。

    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )
 类似资料:
  • 一些秘密需要通过PODS获取,秘密存储在GCP秘密管理器中,什么是安全有效的方法来获取pod中的秘密? 谢谢你!

  • 在测试我编写的一个Google Cloud函数时,试图访问存储在secret Manager中的一个秘密,我得到了以下错误:

  • 我有一个使用Bosh部署的concourse环境。它配置有AWS机密管理器。管道机密模板的格式为 我在AWS秘密管理器(其他类型的秘密)中创建了一个秘密,其值如下。 我在团队中设置了一个总汇管道。

  • 我正在尝试从使用文件,以使用谷歌云平台机密管理器。我已经按照这里的说明操作了,但是我遇到了一个错误,说我没有权限访问这个秘密。 这就是我得到的错误: 我确实创建了一个具有“所有者”权限的服务帐户,下载了它,并使其

  • 我没有找到任何关于在前端端实现逻辑来管理Azure密钥库的可能性的信息。同时,我创建了一个Node JS服务器项目使用ENV来管理Azure Key Vault,它工作成功,但我想在我的Angular应用程序中得到同样的效果。

  • 我在帐户a中存储了一个密钥(USRFTP),我想从EC2框中使用帐户B中的角色ASHISHROLE访问这个密钥。我正在运行python代码来获取密钥,如下所示,在密钥中使用资源策略,KMS策略如下所示,但仍然得到这个问题 clienterRor:调用GetSecretValue操作时发生错误(AccessDeniedException):User:ARN:AWS:STS::AccountB:假定-