![](/img/trans.png)
[英]Accessing AWS DocumentDB from different VPC in a another account using Lambda?
[英]How to invoke AWS Lambda function in account B (this Lambda in VPC) from Lambda in account A (Lambda in VPC)
我做了什么:
情況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.
但是,當我在另一個賬戶(賬戶 B)中創建類似的 VPC(作為 VPC B)時,我收到以下錯誤:
"errorMessage": "調用 Invoke 操作時發生錯誤 (AccessDeniedException):用戶:arn:aws:sts::Account-A:assumed-role/role-for-vpc-peering-test/lambda1_in_vpc 無權執行: lambda:InvokeFunction on resource: arn:aws:lambda:us-east-1:Account-B:ZC1C425268E68385D1AB5074C17A94F14
我的足跡:
我在賬戶 B 中為賬戶 A 創建了一個跨賬戶 IAM 角色,具有以下權限:
然后我為在賬戶 A 中使用 Lambda 的角色添加了一個內聯策略:
僅向上述角色添加了策略:
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::Account B:role/role-for-Account-A"
}
}
所以我的問題是我應該做些什么來從賬戶 A 中的 Lambda 調用賬戶 B 中的 Lambda 嗎?
我想我只在跨賬戶角色中遺漏了一些東西( access denied error
)。
lambda- 一個代碼:
進口 json 進口 boto3
客戶端 = 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')
有什么建議么?
假設賬戶 b 中的角色是:具有以下策略: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"
}
]
}.
執行角色 - 附加此內聯策略:
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::Account-b-id:role/role-for-7691-4701-2358"
}
}
並用下面提到的代碼更新了我的 lambda function。
但仍然遇到同樣的錯誤
現在 lambdas 不在 vpc 中。
以下 AWS 博客中列出了在賬戶 A 中擔任角色以在賬戶 B 中執行某些操作所需的一般步驟(例如,在 B 中調用 lambda function):
The important thing to note is that the lambda function in Account A has to assume role in Account B. The assumable role must allow execution role of function A to invoke function in B.
我根據上面鏈接中的示例修改了賬戶 A 中 lambda 的代碼。
要關注的關鍵要素是:
假設您的所有角色都已正確設置,您的代碼應該看起來像打擊。 顯然,您必須完全根據您的需要對其進行調整,但它應該讓您大致了解如何在賬戶 A 中的 lambda 中做什么。
import json
import boto3
client = boto3.client('lambda')
sts = boto3.client('sts')
def lambda_handler(event, context):
inputForInvoker = {'CustomerId': '123', 'Amount': 50 }
account_b = sts.assume_role(
RoleArn="<arn-of-assumbale-role-in-acccount-b>",
RoleSessionName="cross_acct_lambda"
)
ACCESS_KEY = account_b['Credentials']['AccessKeyId']
SECRET_KEY = account_b['Credentials']['SecretAccessKey']
SESSION_TOKEN = account_b['Credentials']['SessionToken']
# create service client using the assumed role credentials
# from account B
account_b_client = boto3.client(
'lambda',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN)
response = account_b_client.invoke(FunctionName='arn:aws:lambda:us-east-1:AccountB-id:function:lambda-vpc-peering', InvocationType='RequestResponse')
responseJson = json.load(response['Payload'])
print('\n') print(responseJson) print('\n')
ps
- 我在這些賬戶之間創建了一個 VPC 對等連接
這不是必需的。 從 function A 調用 function B 無論如何都會通過 Internet 調用 go。 因此,如果您不將對等連接用於其他任何用途,則它不會用於 lambda。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.