AWSラムダ関数URL
12605 ワード
AWSは最近ラムダのためのすばらしい新機能をリリースしました:AWS Lambda Function URLs
関数URLは、単純な方法を許可するパターンを置き換えることを約束します
The Github repository can be found here.
私はCDKを設定し、環境をブートストラップすることはありません.あなたはその情報を見つけることができますhere.
CDKを設定したら、プロジェクトを設定する必要があります. オプション:ラムダ関数の追加ライブラリが必要な場合は、
このスタックは、
見ることができるhere APIゲートウェイが期待している応答形式の例.
スタックでURL出力を使用するか、または
問い合わせる
関数URLは、単純な方法を許可するパターンを置き換えることを約束します
HTTP GET
ラムダ関数の操作.The Github repository can be found here.
CDK initと配備
私はCDKを設定し、環境をブートストラップすることはありません.あなたはその情報を見つけることができますhere.
CDKを設定したら、プロジェクトを設定する必要があります.
mkdir CDK_Lambda_URL && cd CDK_Lambda_URL
cdk init --language python
source .venv/bin/activate
aws-cdk.aws-lambda-python-alpha
必要です.Dockerを使用してスタック展開中にカスタムビルドを行うには、TXTを使用します.pip install -r requirements.txt && pip install -r requirements-dev.txt
次に空のスタックをAWSに配備します.cdk deploy
スタックデザイン
このスタックは、
aws-lambda-python-alpha
Dockerコンテナを使用してすべての追加ライブラリを使用してその関数を構築するには、次の手順に従います.Decemonをインストールする前に実行してくださいcdk deploy
.from aws_cdk import CfnResource, Stack
from aws_cdk import aws_lambda as _lambda
from aws_cdk import aws_lambda_python_alpha as _lambda_python
from constructs import Construct
class LambdaStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# in __init__ I like to initialize the infrastructure I will be creating
self.example_lambda = None
self.build_infrastructure()
def build_infrastructure(self):
# For convenience, consolidate infrastructure construction
self.build_lambda()
def build_lambda(self):
self.example_lambda = _lambda_python.PythonFunction(
scope=self,
id="ExampleLambda",
# entry points to the directory
entry="lambda_funcs/LambdaURL",
# index is the file name
index="URL_lambda.py",
# handler is the function entry point name in the lambda.py file
handler="handler",
runtime=_lambda.Runtime.PYTHON_3_9,
# name of function on AWS
function_name="ExampleLambdaFunctionURL",
)
# Set up the Lambda Function URL
CfnResource(
scope=self,
id="lambdaFuncUrl",
type="AWS::Lambda::Url",
properties={
"TargetFunctionArn": self.example_lambda.function_arn,
"AuthType": "NONE",
"Cors": {"AllowOrigins": ["*"]},
},
)
# Give everyone permission to invoke the Function URL
CfnResource(
scope=self,
id="funcURLPermission",
type="AWS::Lambda::Permission",
properties={
"FunctionName": self.example_lambda.function_name,
"Principal": "*",
"Action": "lambda:InvokeFunctionUrl",
"FunctionUrlAuthType": "NONE",
},
)
# Get the Function URL as output
Output(
scope=self,
id="funcURLOutput",
value=cfnFuncUrl.get_att(attribute_name="FunctionUrl").to_string(),
)
最小加工ラムダ関数
見ることができるhere APIゲートウェイが期待している応答形式の例.
from logging import getLogger
logger = getLogger()
logger.setLevel(level="DEBUG")
def handler(event, context):
logger.debug(msg=f"Initial event: {event}")
response = {
"isBase64Encoded": False,
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
},
"body": f"Nice! You said {event['queryStringParameters']['q']}",
}
return response
今すぐ行うことができますcdk deploy
. ラムダ関数はDockerを使って構築され、ブートストラップされたECRリポジトリにアップロードされます.プロジェクトが構築されると、それはCloudFormation
テンプレートを開始し、インフラストラクチャを展開します.あなたのスタック展開を見ることができますAWS CloudFormation
- インフラが比較的単純であるので、それは速くなければなりません.スタックでURL出力を使用するか、または
AWS Console
見つけるLambda Function URL
関数URLを問い合わせる
問い合わせる
Function URL
そして、あなたのラムダ関数から返答を得るGET
利用依頼requests
or Postman
Reference
この問題について(AWSラムダ関数URL), 我々は、より多くの情報をここで見つけました https://dev.to/wesleycheek/aws-lambda-function-urls-with-aws-cdk-58ihテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol