What I did:
- I created a VPC Peering connection between these accounts
- Internet Gateways also attached to each VPC
- Route Tables also configured (to allow traffic from both sides)
Case 1:
I successfully tested invoking Lambda function (in VPC B) from another Lambda function (in VPC A) when these 2 VPCs are in same account.
However, when I created a similar VPC (as VPC B) in another account (account B) I got the following error:
"errorMessage": "An error occurred (AccessDeniedException) when calling the Invoke operation: User: arn:aws:sts::Account-A:assumed-role/role-for-vpc-peering-test/lambda1_in_vpc is not authorized to perform: lambda:InvokeFunction on resource: arn:aws:lambda:us-east-1:Account-B:function:lambda-vpc
My trails:
I created a cross-account IAM Role for account A in account B with the following permissions:
Then I added an inline policy for the role which is using Lambda in account A:
Added policy to above role only:
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::Account B:role/role-for-Account-A"
}
}
So my questions is should I do anything else to invoke Lambda in account B from Lambda in account A?
I think I am missing something in the cross-account role only (access denied error
).
lambda- A code:
import json import boto3
client = boto3.client('lambda')
def lambda_handler(event, context): inputForInvoker = {'CustomerId': '123', 'Amount': 50 }
response = client.invoke(
FunctionName='arn:aws:lambda:us-east-1:AccountB-id:function:lambda-vpc-peering',
InvocationType='RequestResponse', # Event
Payload=json.dumps(inputForInvoker)
)
responseJson = json.load(response['Payload'])
print('\n')
print(responseJson)
print('\n')
Any suggestions?
Assume role- in Account b is : have following Policies : 1. AWSLambdaBasicExecutionRole 2.trust-policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::Account-id-A:role/role-for-vpc-peering-test"
},
"Action": "sts:AssumeRole"
}
]
}.
execution role - attached this inline policy :
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::Account-b-id:role/role-for-7691-4701-2358"
}
}
and updated my lambda function with below mention code also.
but still getting same error
now lambdas are not in vpc.
sts.assume_role(...)
boto3 call. – Marcin