我正在尝试使用 Python 代码触发 Lambda 函数,如下所示:
import boto3
from botocore.exceptions import NoCredentialsError, PartialCredentialsError
def get_lambda_client():
return boto3.client('lambda')
def invoke_lambda():
lambda_client = get_lambda_client()
if lambda_client:
try:
response = lambda_client.invoke(
FunctionName='MyLambdaFunctionName',
InvocationType='RequestResponse', # or 'event for async invocation'
Payload=b'{}' #Not sending any payload
)
print(f" the response from the aws = {response}")
except Exception as e:
print(f" Error invoking Lambda function: {e}")
invoke_lambda()
我正在使用角色附加的以下策略:
触发 Lambda 函数的策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": "< arn of my lambda function>"
},
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "< arn of the role I created for lambda function which intern
will trigger aws step function>"
}
]
}
我为此 Lambda 函数触发器创建的角色的可信策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com",
"AWS": "<arn for the iam user>"
},
"Action": "sts:AssumeRole"
}
]
}
如果这里缺少任何内容,请告诉我。当我尝试从 python 代码触发 lambda 函数时遇到的错误是:
Error invoking Lambda function: An error occurred (ExpiredTokenException) when calling the Invoke operation: The security token included in the request is expired
考虑到我没有获取
AccessKey
、SecreteKey
和 SessionToken
的权限,建议一个可以在此处使用的解决方案,可以通过假设 sts 角色来使用。
该过程应按如下方式进行:
~/.aws/credentials
配置文件中的 IAM User凭证(通常使用 AWS CLI 中的
aws configure
命令创建)当您执行