Skip to content

Lambda Function Error - Session.create_client() got an unexpected keyword argument 'aws_account_id

0

Hello,

Lambda function running fine and all of sudden I get error: Session.create_client() got an unexpected keyword argument 'aws_account_id

When creating client = boto3.client('lambda')

Lambda Function Python 3.12 x64 architecture Using packages: pandas, requests, s3fs, fsspec

Problem seems related to IAM role, as code works on new lambda function with new standard IAM role, or a change to boto3 library.

Your help is very much appreciated.

2 Answers
0
Accepted Answer

This error suggests there's an issue with how the boto3 client is being initialized in your Lambda function. The error message "Session.create_client() got an unexpected keyword argument 'aws_account_id'" indicates you're passing a parameter to the boto3.client() method that it doesn't accept.

The standard way to create a boto3 client for Lambda is:

client = boto3.client('lambda')

If you're trying to access resources in another AWS account, you should use the AWS STS (Security Token Service) to assume a role in that account first, rather than passing an aws_account_id parameter directly to the client constructor.

Here's the correct approach for cross-account access:

# First assume role in the other account
sts_connection = boto3.client('sts')
assumed_role = sts_connection.assume_role(
RoleArn="arn:aws:iam::<ACCOUNT_NUMBER>:role/<ROLE_NAME>",
RoleSessionName="cross_account_session"
)

# Then create a client using the temporary credentials
credentials = assumed_role['Credentials']
client = boto3.client(
'lambda',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken']
)

This issue could also be related to:

  1. A version mismatch between the boto3 library in your development environment and the one in Lambda
  2. Custom code that's incorrectly passing the aws_account_id parameter
  3. A dependency that's wrapping boto3 and using incorrect parameters

Since you mentioned the code works with a new Lambda function and IAM role, it's possible that:

  • Your current Lambda function is using an outdated version of boto3
  • There might be permission issues with the current IAM role
  • The Lambda environment might have corrupted dependencies

To resolve this:

  1. Check your code for any instances where boto3.client() is called with an aws_account_id parameter
  2. Update your Lambda function's dependencies, especially boto3
  3. If you're using Lambda layers, ensure they contain compatible versions of your dependencies
  4. Consider recreating your Lambda function with the correct dependencies
    Sources
    Lambda function to copy in S3 accross-accounts - Python code problem | AWS re:Post
    Unable to import module 'lambda_function': No module named 'requests | AWS re:Post

answered a year ago

0

You have 2 options:

  1. Redeploy your lambda with the latest version of the boto3 SDK
  2. Manually roll back lambda runtime version to the one that worked. The process is described (badly) in this article: https://docs.aws.amazon.com/lambda/latest/dg/runtime-management-rollback.html#runtime-management-rollback-manual

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.