selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 104
Current browser version is 106.0.5249.119 with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
それでは実際にSAMの初期設定をしていきます。 画面下部のコンソールに sam init のコマンドを実行して初期設定します。
$ pwd
/home/ec2-user/environment
$ sam init
You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Multi-step workflow
3 - Serverless API
4 - Scheduled task
5 - Standalone function
6 - Data processing
7 - Infrastructure event management
8 - Lambda EFS example
9 - Machine Learning
Template: 1
Use the most popular runtime and package type? (Python and zip) [y/N]: y
Project name [sam-app]: sam-example
Cloning from https://github.com/aws/aws-sam-cli-app-templates (process may take a moment)
-----------------------
Generating application:
-----------------------
Name: sam-example
Runtime: python3.9
Architectures: x86_64
Dependency Manager: pip
Application Template: hello-world
Output Directory: .
Next steps can be found in the README file at ./sam-example/README.md
Commands you can use next
=========================
[*] Create pipeline: cd sam-example && sam pipeline init --bootstrap
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
SAM CLI update available (1.53.0); (1.40.1 installed)
To download: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
$
実際の入力値は以下の4つ
Choice: 1(1 - AWS Quick Start Templates)
Template: 1(1 - Hello World Example)
Use the most popular runtime and package type? (Python and zip) [y/N]: y
Project name [sam-app]: sam-example
sam-example というプロジェクト名で、実行環境 python3.9、samテンプレート Hello World Example を選択(テンプレートはすぐに自分で編集するのでなんでも構いません)しています。
sam init コマンドが完了するとひと通り必要なフォルダ・ファイル群が作成されます。
この中で重要なのが、
app.py:Lambdaのソースプログラムが記載されているファイル
template.yml:Lambdaの設定が記載されているファイル
の二つになります。
それぞれ中身を簡単に見てみます。 まずは template.yml から。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-example
Sample SAM Template for sam-example
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
Hello World Example のテンプレートの場合、LambdaとAPIGatewayが含まれています。 Resources: 部がそれらの記述に該当しますが
では、ここからはコンソール操作になるので、Cloud9画面下部のコンソール部分でコマンド実行します。 まず、カレントフォルダを ~/environment/sam-example/ (自身で入力したプロジェクトフォルダ)に移動して、ビルドコマンド sam build --use-container を実行します。
実際の実行コマンドは以下の2つ
cd ~/environment/sam-example/ sam build --use-container
では、やってみます。
$ cd ~/environment/sam-example/
$ sam build --use-container
Starting Build inside a container
Building codeuri: /home/ec2-user/environment/sam-example runtime: python3.9 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
Fetching public.ecr.aws/sam/build-python3.9:latest-x86_64 Docker container image...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Mounting /home/ec2-user/environment/sam-example as /tmp/samcli/source:ro,delegated inside runtime container
Traceback (most recent call last):
File "samcli/__main__.py", line 12, in <module>
File "click/core.py", line 829, in __call__
File "click/core.py", line 782, in main
File "click/core.py", line 1259, in invoke
File "click/core.py", line 1066, in invoke
File "click/core.py", line 610, in invoke
File "click/decorators.py", line 73, in new_func
File "click/core.py", line 610, in invoke
File "samcli/lib/telemetry/metric.py", line 153, in wrapped
File "samcli/lib/telemetry/metric.py", line 122, in wrapped
File "samcli/lib/utils/version_checker.py", line 42, in wrapped
File "samcli/cli/main.py", line 90, in wrapper
File "samcli/commands/build/command.py", line 228, in cli
File "samcli/commands/build/command.py", line 318, in do_cli
File "samcli/lib/build/app_builder.py", line 172, in build
File "samcli/lib/build/build_strategy.py", line 42, in build
File "samcli/lib/build/build_strategy.py", line 53, in _build_functions
File "samcli/lib/build/build_strategy.py", line 129, in build_single_function_definition
File "samcli/lib/build/app_builder.py", line 572, in _build_function
File "samcli/lib/build/app_builder.py", line 705, in _build_function_on_container
File "samcli/local/docker/container.py", line 308, in wait_for_logs
RuntimeError: Container does not exist. Cannot get logs for this container
[19020] Failed to execute script __main__
おそらく、 RuntimeError: Container does not exist. Cannot get logs for this container エラーが発生します。 ビルドコマンドのオプションで --use-container(実行環境をコンテナ上に作成するオプション)を付けているのですが、Cloud9の初期ディスクだと足りません。。。 (非常にわかりにくいエラーメッセージですね。。。)
$ cd ~/environment/sam-example/
$ sam build --use-container
Starting Build inside a container
Building codeuri: /home/ec2-user/environment/sam-example runtime: python3.9 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
Fetching public.ecr.aws/sam/build-python3.9:latest-x86_64 Docker container image...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Mounting /home/ec2-user/environment/sam-example as /tmp/samcli/source:ro,delegated inside runtime container
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
requirements.txt file not found. Continuing the build without dependencies.
Running PythonPipBuilder:CopySource
$
では、いよいよデプロイです。 sam deploy --guided --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND デプロイ時のガイド付き、ロール等は自動で作成するオプションを付けています。
実行コマンドは以下の1つ。
sam deploy --guided --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND
実行します。
$ sam deploy --guided --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND
Configuring SAM deploy
======================
Looking for config file [samconfig.toml] : Not found
Setting default arguments for 'sam deploy'
=========================================
Stack Name [sam-app]: sam-sample
AWS Region [ap-northeast-1]:
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [y/N]: y
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]: y
Save arguments to configuration file [Y/n]: y
SAM configuration file [samconfig.toml]:
SAM configuration environment [default]:
Looking for resources needed for deployment:
Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-bncdmvrv3w3n
A different default S3 bucket can be set in samconfig.toml
Saved arguments to config file
Running 'sam deploy' for future deployments will use the parameters saved above.
The above parameters can be changed by modifying samconfig.toml
Learn more about samconfig.toml syntax at
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
Uploading to sam-sample/1c2f1111422c1a9a068bc3d12653d3c4 7913 / 7913 (100.00%)
Deploying with following values
===============================
Stack name : sam-sample
Region : ap-northeast-1
Confirm changeset : True
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-bncdmvrv3w3n
Capabilities : ["CAPABILITY_IAM", "CAPABILITY_AUTO_EXPAND"]
Parameter overrides : {}
Signing Profiles : {}
Initiating deployment
=====================
Uploading to sam-sample/cc7378c1e1ecf6a93a04bc81b3006223.template 768 / 768 (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Add HelloWorldFunctionRole AWS::IAM::Role N/A
+ Add HelloWorldFunction AWS::Lambda::Function N/A
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:XXXXXXXXXXXX:changeSet/samcli-deploy1651456975/245e03ef-407d-b1c6-43702e5cd1b3-e61b
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y
2022-05-02 02:03:09 - Waiting for stack create/update to complete
CloudFormation events from changeset
-------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole -
CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole Resource creation Initiated
CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole -
CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction -
CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction Resource creation Initiated
CREATE_COMPLETE AWS::Lambda::Function HelloWorldFunction -
CREATE_COMPLETE AWS::CloudFormation::Stack sam-sample -
-------------------------------------------------------------------------------------------------------------------------------------------------------------
CloudFormation outputs from deployed stack
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Outputs
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Key HelloWorldFunctionIamRole
Description Implicit IAM Role created for Hello World function
Value arn:aws:iam::XXXXXXXXXXXX:role/sam-sample-HelloWorldFunctionRole-11AUZUABC6ASS
Key HelloWorldFunction
Description Hello World Lambda Function ARN
Value arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:sam-sample-HelloWorldFunction-y2SU9rABc52s
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Successfully created/updated stack - sam-sample in ap-northeast-1
$
ガイド付きなので何回か質問があるので確認して選択しましょう。 Stack Name は、自分で決める任意の名称になります。
ちなみに次回以降の更新の時は選択内容が保持されるため sam deploy コマンドでサクッとデプロイできます。
$ sam deploy
Uploading to sam-sample/287342c7c37cb1d8020451b36647bd1f 452838 / 452838 (100.00%)
Deploying with following values
===============================
Stack name : sam-sample
Region : ap-northeast-1
Confirm changeset : True
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-bncdmvrv3w3n
Capabilities : ["CAPABILITY_IAM", "CAPABILITY_AUTO_EXPAND"]
Parameter overrides : {}
Signing Profiles : {}
Initiating deployment
=====================
Uploading to sam-sample/17c228278a608cc7c3f55da042cf1ae7.template 768 / 768 (100.00%)
Waiting for changeset to be created..
CloudFormation stack changeset
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Operation LogicalResourceId ResourceType Replacement
-------------------------------------------------------------------------------------------------------------------------------------------------------------
* Modify HelloWorldFunction AWS::Lambda::Function False
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:XXXXXXXXXXXX:changeSet/samcli-deploy1651457546/3cc5876f-4d81-a290-7cbc821840ed-5d26
Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y
2022-05-02 02:12:41 - Waiting for stack create/update to complete
CloudFormation events from changeset
-------------------------------------------------------------------------------------------------------------------------------------------------------------
ResourceStatus ResourceType LogicalResourceId ResourceStatusReason
-------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction -
UPDATE_COMPLETE AWS::Lambda::Function HelloWorldFunction -
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS AWS::CloudFormation::Stack sam-sample -
UPDATE_COMPLETE AWS::CloudFormation::Stack sam-sample -
-------------------------------------------------------------------------------------------------------------------------------------------------------------
CloudFormation outputs from deployed stack
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Outputs
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Key HelloWorldFunctionIamRole
Description Implicit IAM Role created for Hello World function
Value arn:aws:iam::XXXXXXXXXXXX:role/sam-sample-HelloWorldFunctionRole-11AUZUANR6ASS
Key HelloWorldFunction
Description Hello World Lambda Function ARN
Value arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:sam-sample-HelloWorldFunction-yrJDo52s2SU9
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Successfully created/updated stack - sam-sample in ap-northeast-1
$