以前,当我没有将MFA设置为登录到AWS控制台时,我通过以下方式连接到dynamodb
dynamo = boto3.resource('dynamodb',
region_name='ap-northeast-2',
endpoint_url='http://dynamodb.ap-northeast-2.amazonaws.com')
table = dynamo.Table('tablename')向那张桌子查询是很好的。
response = table.query(
KeyConditionExpression =Key("user_id").eq(123123)
)在我将MFA设置为额外的安全性以登录到AWS控制台之后,现在当我执行上面的代码时,我得到:
ClientError: An error occurred (UnrecognizedClientException) when calling the Query operation: The security token included in the request is invalid.我为RDB使用隧道,是可以用于连接到dynamodb,还是有访问dynamodb所需的权限?
发布于 2020-06-23 01:51:32
当您启用MFA时,SDK 不会自动知道如何使用MFA。您的常规IAM用户API和秘密密钥已经不够了。相反,您需要使用仅为MFA会话创建的临时凭据。
要使MFA与boto3一起工作,您必须显式地调用令牌
启用MFA的IAM用户将需要调用GetSessionToken并提交与其MFA设备相关联的MFA代码。使用调用返回的临时安全凭据,IAM用户可以对需要MFA身份验证的API操作进行编程调用。
使用get_session_token,您可以调用sts服务,该服务将根据您的MFA详细信息为您提供临时凭据:
sts = boto3.client('sts')
mfa_response = sts.get_session_token(
DurationSeconds=123,
SerialNumber='string',
TokenCode='string'
)调用将返回mfa_response中的凭据,您可以使用它来创建一个新的boto3会话。例如:
mfa_session = boto3.session.Session(
aws_access_key_id=mfa_session['Credentials']['AccessKeyId'],
aws_secret_access_key=mfa_session['Credentials']['SecretAccessKey'],
aws_session_token=mfa_session['Credentials']['SessionToken'])
dynamo = mfa_session.resource('dynamodb', ...)
# and the rest of the codehttps://stackoverflow.com/questions/62525930
复制相似问题