Last active
September 18, 2019 01:02
-
-
Save shiro01/8b9fffa5cbb0d045d9887f6790d9dc8d to your computer and use it in GitHub Desktop.
外部Lambda呼び出し(Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html | |
| #呼び出されるLambda(関数名:sample-lambda) | |
| # Roleに必要なPolicy | |
| # ・AWSLambdaBasicExecutionRole | |
| def lambda_handler(event, context): | |
| return {'test':'test'} | |
| # 呼び出すLambda | |
| # Roleに必要なPolicy | |
| # ・AWSLambdaBasicExecutionRole | |
| # ・AWSLambdaRole | |
| import json | |
| import boto3 | |
| def lambda_handler(event, context): | |
| call_external_lambda2(event) | |
| result = call_external_lambda(event) | |
| print(result) | |
| return result['test'] | |
| def call_external_lambda(event): | |
| request = json.dumps(event) | |
| client = boto3.client('lambda') | |
| response = client.invoke( | |
| FunctionName='sample-lambda', | |
| InvocationType='RequestResponse', | |
| Payload=request | |
| ) | |
| return json.loads(response['Payload'].read()) | |
| # 非同期での実行。ARNを指定して実行もできる。 | |
| def call_external_lambda2(event): | |
| request = json.dumps(event) | |
| client = boto3.client('lambda') | |
| response = client.invoke( | |
| FunctionName='arn:aws:lambda:ap-northeast-1:xxxxx:function:sample-lambda:alias1', | |
| InvocationType='Event', | |
| Payload=request | |
| ) | |
| return 'ok' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment