<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Serverless Framework | Men of Letters（メン・オブ・レターズ） &#8211; 論理的思考/業務改善/プログラミング</title>
	<atom:link href="https://laboratory.kazuuu.net/category/serverless-framework/feed/" rel="self" type="application/rss+xml" />
	<link>https://laboratory.kazuuu.net</link>
	<description></description>
	<lastBuildDate>Sun, 30 Oct 2022 20:42:22 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://laboratory.kazuuu.net/wp-content/uploads/2021/02/cropped-L-32x32.png</url>
	<title>Serverless Framework | Men of Letters（メン・オブ・レターズ） &#8211; 論理的思考/業務改善/プログラミング</title>
	<link>https://laboratory.kazuuu.net</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Serverless Frameworkを用いてAWSにFlask appをデプロイする（Windows10）</title>
		<link>https://laboratory.kazuuu.net/deploy-flask-app-on-aws-using-serverless-framework-windows10/</link>
					<comments>https://laboratory.kazuuu.net/deploy-flask-app-on-aws-using-serverless-framework-windows10/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Sun, 30 Oct 2022 20:42:22 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Python【初歩から現場実務的なもの】]]></category>
		<category><![CDATA[Serverless Framework]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12556</guid>

					<description><![CDATA[Serverless Frameworkを用いてAWSにFlask appをデプロイしてみます。 目次 ■PC環境■フォルダ・ディレクトリを作成する■仮想環境の作成■スクリプト（.pyファイル）を作成する■コード■ser [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Serverless Frameworkを用いてAWSにFlask appをデプロイしてみます。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■フォルダ・ディレクトリを作成する</a></li><li><a href="#toc3" tabindex="0">■仮想環境の作成</a></li><li><a href="#toc4" tabindex="0">■スクリプト（.pyファイル）を作成する</a><ol><li><a href="#toc5" tabindex="0">■コード</a></li></ol></li><li><a href="#toc6" tabindex="0">■serverless.ymlを作成する</a><ol><li><a href="#toc7" tabindex="0">■コード</a></li></ol></li><li><a href="#toc8" tabindex="0">■Flaskのインストール等を行う</a></li><li><a href="#toc9" tabindex="0">■デプロイする</a><ol><li><a href="#toc10" tabindex="0">■コード（変更前）</a></li><li><a href="#toc11" tabindex="0">■コード（変更後）</a></li></ol></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>&gt;Windows 10</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img fetchpriority="high" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>&gt;npm &#8211;version<br />
8.3.1</p>
<p>&gt;node &#8211;version<br />
v16.14.0</p>
<p>&gt;nodeserverless &#8211;version</p>
<p>Framework Core: 3.19.0<br />
Plugin: 6.2.2<br />
SDK: 4.3.2</p>
<p>&gt;aws &#8211;version<br />
aws-cli/2.5.6 Python/3.9.11 Windows/10 exe/AMD64 prompt/off</p>
<p>&gt;仮想環境<br />
Python 3.8.5</p>
<h2><span id="toc2">■フォルダ・ディレクトリを作成する</span></h2>
<p>まずは、フォルダ・ディレクトリを作成します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-133533-1.png"><img decoding="async" class="alignnone size-medium wp-image-12557" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-133533-1-300x165.png" alt="" width="300" height="165" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-133533-1-300x165.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-133533-1-1024x562.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-133533-1-768x422.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-133533-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回は「C:\Users\user_（フォルダパス）」内に「test-flask-application」というフォルダを作成しました。</p>
<h2><span id="toc3">■仮想環境の作成</span></h2>
<p>作成後、Windows10のコマンドプロンプトを起動します。</p>
<pre>C:\Users\user_&gt;py -3.8 -m venv venv</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを押します。venvを使用し、仮想環境を作ります。今回は、環境名を「venv」にします。なお、今回はPythonのバージョンを「3.8」に指定しています。</p>
<p>Enterキーを押すと、何も出力されませんが、仮想環境が作成されます。</p>
<pre>C:\Users\user_&gt;.\venv\Scripts\activate</pre>
<p>作成後、上記のコマンドを入力し、Enterキーを押します。仮想環境への切り替えを行います。</p>
<pre>(.venv) C:\Users\user_&gt;</pre>
<p>Enterキーを押すと「.venv」と表示されます。これで仮想環境への切り替えが完了しました。</p>
<pre>(.venv) C:\Users\user_&gt;cd test-flask-application</pre>
<p>切り替え後、上記のコマンドを入力し、Enterキーを押します。先程作成した「test-flask-application」に移動します。</p>
<pre>(.venv) C:\Users\user_&gt;npm init -f</pre>
<p>移動後、上記のコマンドを入力し、Enterキーを押します。初期化処理を行い、package.jsonを生成します。</p>
<pre>npm WARN using --force Recommended protections disabled.
Wrote to C:\Users\user_\test-flask-application\package.json:

{
  "name": "test-flask-application",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}</pre>
<p>package.jsonの内容が出力されます。</p>
<pre>(.venv) C:\Users\user_&gt;npm install --save-dev serverless-wsgi serverless-python-requirements</pre>
<p>出力後、上記のコマンドを入力し、Enterキーを押します。フレームワークプラグインのserverless-wsgiと、依存関係をrequirements.txtから自動的にバンドルし、PYTHONPATHで利用できるようにするサーバーレスプラグインであるserverless-python-requirementsを「&#8211;save-dev」コマンドを用いて、ローカルインストールします。</p>
<pre>npm WARN deprecated appdirectory@0.1.0: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated formidable@1.2.6: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated superagent@3.8.3: Please upgrade to v7.0.2+ of superagent.  We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing.  See the releases tab for more information at &lt;https://github.com/visionmedia/superagent/releases&gt;.

added 427 packages, and audited 428 packages in 44s

56 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities</pre>
<p>Enterキーを押すと、インストールが開始され、完了となります。</p>
<h2><span id="toc4">■スクリプト（.pyファイル）を作成する</span></h2>
<p>完了後、スクリプト（.pyファイル）を作成します。コードエディタを開きます。</p>
<h3><span id="toc5">■コード</span></h3>
<pre>from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "こんにちは、世界!"</pre>
<p>上記のコードを記述後、「main.py」という名前で、「test-flask-application」フォルダ内に保存します。</p>
<h2><span id="toc6">■serverless.ymlを作成する</span></h2>
<p>スクリプト（.pyファイル）を作成後、コードエディタを開きます。今度はアプリケーションをデプロイするために、serverless.ymlを作成します。</p>
<h3><span id="toc7">■コード</span></h3>
<pre>service: serverless-flask

plugins:
  - serverless-python-requirements
  - serverless-wsgi

custom:
  wsgi:
    app: main.app
    packRequirements: false
  pythonRequirements:
    dockerizePip: non-linux

provider:
  name: aws
  runtime: python3.8
  stage: dev
  region: ap-northeast-1

functions:
  app:
    handler: wsgi.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'</pre>
<p>コードを記述後、「serverless.yml」という名前で、「test-flask-application」フォルダ内に保存します。</p>
<h2><span id="toc8">■Flaskのインストール等を行う</span></h2>
<p>保存後、再び起動しているWindows10のコマンドプロンプト上に戻ります。</p>
<pre>(.venv) C:\Users\user_\test-flask-application&gt;pip install flask</pre>
<p>戻った後に、上記のコマンドを入力し、Enterキーを押します。pipを経由してflaskをインストールします。</p>
<pre>Collecting flask
  Using cached Flask-2.1.2-py3-none-any.whl (95 kB)
Collecting itsdangerous&gt;=2.0
  Using cached itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting importlib-metadata&gt;=3.6.0; python_version &lt; "3.10" Using cached importlib_metadata-4.11.4-py3-none-any.whl (18 kB) Collecting Jinja2&gt;=3.0
  Using cached Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting click&gt;=8.0
  Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting Werkzeug&gt;=2.0
  Using cached Werkzeug-2.1.2-py3-none-any.whl (224 kB)
Collecting zipp&gt;=0.5
  Using cached zipp-3.8.0-py3-none-any.whl (5.4 kB)
Collecting MarkupSafe&gt;=2.0
  Using cached MarkupSafe-2.1.1-cp38-cp38-win_amd64.whl (17 kB)
Collecting colorama; platform_system == "Windows"
  Using cached colorama-0.4.4-py2.py3-none-any.whl (16 kB)
Installing collected packages: itsdangerous, zipp, importlib-metadata, MarkupSafe, Jinja2, colorama, click, Werkzeug, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.1.2 click-8.1.3 colorama-0.4.4 flask-2.1.2 importlib-metadata-4.11.4 itsdangerous-2.1.2 zipp-3.8.0
WARNING: You are using pip version 20.1.1; however, version 22.1.2 is available.
You should consider upgrading via the 'c:\users\user_\.venv\scripts\python.exe -m pip install --upgrade pip' command.</pre>
<p>「Successfully installed」と出力されれば、インストールは完了です。「WARNING」が出力されていますが、警告でありエラーではないので、一旦無視します。</p>
<pre>(.venv) C:\Users\user_\test-flask-application&gt;pip freeze &gt; requirements.txt</pre>
<p>インストール後、上記のコマンドを入力し、Enterキーを押します。pipを経由してfreezeをインストールし、内容をrequirements.txtに出力します。</p>
<p>Enterキーを押すと何も出力されませんが、これでインストールは完了となります。</p>
<h2><span id="toc9">■デプロイする</span></h2>
<pre>(.venv) C:\Users\user_\test-flask-application&gt;sls deploy</pre>
<p>完了後、上記のコマンドを入力し、Enterキーを押します。ServerlessFrameworkコマンドを用いてdeployを行います。</p>
<pre>Running "serverless" from node_modules

Warning: Invalid configuration encountered
at 'functions.app.events.1.http': value 'ANY {proxy+}' does not satisfy pattern /^(?:\*|(GET|POST|PUT|PATCH|OPTIONS|HEAD|DELETE|ANY) (\/\S*))$/i

Learn more about configuration validation here: http://slss.io/configuration-validation

Deploying serverless-flask to stage dev (ap-northeast-1)
Warning: Please change "wsgi.handler" to "wsgi_handler.handler" in serverless.yml
Warning: Using "wsgi.handler" still works but has been deprecated and will be removed
Warning: More information at https://github.com/logandk/serverless-wsgi/issues/84
Python executable not found for "runtime": python3.8
Using default Python executable: python
Packaging Python WSGI handler...

✔ Service deployed to stack serverless-flask-dev (151s)

endpoints:
ANY - https://****.execute-api.ap-northeast-1.amazonaws.com/dev
ANY - https://****.execute-api.ap-northeast-1.amazonaws.com/dev/{proxy+}
functions:g (0s)
app: serverless-flask-dev-app (1.6 MB)

1 deprecation found: run 'serverless doctor' for more details

Toggle on monitoring with the Serverless Dashboard: run "serverless"</pre>
<p>Enterキーを押すと、デプロイが開始され、しばらくすると完了となります。完了後、「https://****.execute-api.ap-northeast-1.amazonaws.com/dev」といったURLが出力されますので、このURLをコピーします。</p>
<p>コピー後、Webブラウザを起動し、アドレスバーにURLを貼り付けてアクセスします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-150534-1.png"><img decoding="async" class="alignnone size-medium wp-image-12558" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-150534-1-300x131.png" alt="" width="300" height="131" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-150534-1-300x131.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-150534-1-1024x447.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-150534-1-768x335.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-11-150534-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>アクセスしてみると、スクリプト（.pyファイル）内のdef文で定義した関数が呼び出され、returnで設定した日本語の文字列を表示させることができました。</p>
<p>なお、ServerlessFrameworkコマンドを用いてdeployを行う際に「Deployment bucket has been removed manually. Please recreate it or remove your service and attempt to deploy it again」というエラーが出力される場合は、serverless.yml内の「service: ***」の「***」を変更することで、デプロイができる。</p>
<p>また、今回ServerlessFrameworkコマンドを用いてdeployを行う際に、下記の警告が出力されました。</p>
<pre>Warning: Invalid configuration encountered
at 'functions.app.events.1.http': value 'ANY {proxy+}' does not satisfy pattern /^(?:\*|(GET|POST|PUT|PATCH|OPTIONS|HEAD|DELETE|ANY) (\/\S*))$/i

Learn more about configuration validation here: http://slss.io/configuration-validation</pre>
<p>この警告を改善するには、serverless.yml内の下記のコードに変更しデプロイすると、警告が出力されなくなる。</p>
<h3><span id="toc10">■コード（変更前）</span></h3>
<pre>functions:
  app:
    handler: wsgi.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'</pre>
<h3><span id="toc11">■コード（変更後）</span></h3>
<pre>functions:
  app:
    handler: wsgi.handler
    events:
      - http: ANY /
      - http: 'ANY /{proxy+}'</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/deploy-flask-app-on-aws-using-serverless-framework-windows10/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Serverless Frameworkテンプレート内にプログラムを追加し更新する</title>
		<link>https://laboratory.kazuuu.net/add-and-update-programs-in-the-serverless-framework-template/</link>
					<comments>https://laboratory.kazuuu.net/add-and-update-programs-in-the-serverless-framework-template/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Mon, 24 Oct 2022 20:41:45 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Python【初歩から現場実務的なもの】]]></category>
		<category><![CDATA[Serverless Framework]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12545</guid>

					<description><![CDATA[Serverless Frameworkテンプレート内にプログラムを追加し更新してみます。 前回、Serverless Frameworkを用いてテンプレートを作成しPython関数を呼び出すという記事を公開しましたが、 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Serverless Frameworkテンプレート内にプログラムを追加し更新してみます。</p>
<p>前回、<a href="https://laboratory.kazuuu.net/create-templates-and-call-python-functions-using-serverless-framework">Serverless Frameworkを用いてテンプレートを作成しPython関数を呼び出す</a>という記事を公開しましたが、今回はServerless Frameworkを用いて作成したテンプレート内のプログラムを追加し更新してみます。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-4" checked><label class="toc-title" for="toc-checkbox-4">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■プロジェクトディレクトリを作成する</a><ol><li><a href="#toc3" tabindex="0">■コード（変更前）</a></li><li><a href="#toc4" tabindex="0">■コード（変更後）</a></li></ol></li><li><a href="#toc5" tabindex="0">■追加したプログラムを更新する</a></li><li><a href="#toc6" tabindex="0">■関数を呼び出す</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>&gt;Windows 10</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>&gt;npm &#8211;version<br />
8.3.1</p>
<p>&gt;node &#8211;version<br />
v16.14.0</p>
<p>&gt;nodeserverless &#8211;version</p>
<p>Framework Core: 3.19.0<br />
Plugin: 6.2.2<br />
SDK: 4.3.2</p>
<p>&gt;aws &#8211;version<br />
aws-cli/2.5.6 Python/3.9.11 Windows/10 exe/AMD64 prompt/off</p>
<h2><span id="toc2">■プロジェクトディレクトリを作成する</span></h2>
<p>前回、Serverless Frameworkを用いて「C:\Users\user_\project_test（フォルダパス）」内に「project_test」ディレクトリを作成し、その中に「.gitignore」、「handler.py」、「serverless.yml」という3つのファイルが生成されましたが、プログラムを追加してみますので、「handler.py」のファイルをコードエディタで開きます。</p>
<h3><span id="toc3">■コード（変更前）</span></h3>
<pre>import json


def hello(event, context):
    body = {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "input": event
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    return response

    # Use this code if you don't use the http event with the LAMBDA-PROXY
    # integration
    """
    return {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "event": event
    }
    """
</pre>
<p>コードエディタで開くと上記のコードが表示されます。</p>
<h3><span id="toc4">■コード（変更後）</span></h3>
<pre>import json

def hello(event, context):

    a = 10

    b = 5

    return a+b</pre>
<p>表示後、コードを上記のように変更します。def文を用いてhelloという関数を定義し、関数を呼び出した時に実行される処理として、a変数とb変数に格納した数値を算術演算子&#8221;+&#8221;（プラス）を用いて加算し、加算した結果をreturnとして返すようにします。</p>
<p>コードを記述後、保存します。</p>
<h2><span id="toc5">■追加したプログラムを更新する</span></h2>
<p>保存後、追加（変更）したプログラムを更新するために、Windows10のコマンドプロンプトを起動します。</p>
<pre>C:\Users\user_&gt;cd project_test</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを入力します。cdコマンドで作成した「project_test」ディレクトリに移動します。</p>
<pre>C:\Users\user_\project_test&gt;cd project_test</pre>
<p>移動後、上記のコマンドを入力し、Enterキーを入力します。さらにServerless Frameworkを用いて作成した「project_test」ディレクトリに移動します。</p>
<pre>C:\Users\user_\project_test\project_test&gt;serverless deploy function -f hello</pre>
<p>移動後、上記のコマンドを入力し、Enterキーを入力します。「deploy function -f」コマンドで、def文を用いて定義したhello関数をデプロイします。</p>
<pre>Deploying function hello to stage dev (us-east-1)

✔ Function code deployed (2s)
Configuration did not change. Configuration update skipped. (2s)
</pre>
<p>Enterキーを押すと、上記のメッセージが出力されます。「Function code deployed」にチェックが入っているので、これでデプロイは完了となります。</p>
<h2><span id="toc6">■関数を呼び出す</span></h2>
<p>完了後、検証のために、プログラムを実行し、関数を呼び出してみます。</p>
<pre>C:\Users\user_\project_test\project_test&gt;serverless invoke -f hello -l</pre>
<p>関数を呼び出すために、コマンドプロンプト上で、上記のコマンドを入力し、Enterキーを入力します。「invoke」コマンドで関数を呼び出します。</p>
<pre>15
--------------------------------------------------------------------
START
END RequestId: *******-****-****-****-**********
END Duration: 1.10 ms (init: 110.79 ms) Memory Used: 36 MB
</pre>
<p>Enterキーを押すと、def文を用いてhelloという関数を定義しましたが、定義した関数を呼び出しreturnを返させることができました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/add-and-update-programs-in-the-serverless-framework-template/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Serverless Frameworkを用いてテンプレートを作成しPython関数を呼び出す</title>
		<link>https://laboratory.kazuuu.net/create-templates-and-call-python-functions-using-serverless-framework/</link>
					<comments>https://laboratory.kazuuu.net/create-templates-and-call-python-functions-using-serverless-framework/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Sat, 22 Oct 2022 20:39:59 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Python【初歩から現場実務的なもの】]]></category>
		<category><![CDATA[Serverless Framework]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12533</guid>

					<description><![CDATA[Serverless Frameworkを用いてテンプレートを作成しPython関数を呼び出してみます。 今回は、Serverless Frameworkを用いてテンプレートを作成しPython関数を呼び出してみますが、 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Serverless Frameworkを用いてテンプレートを作成しPython関数を呼び出してみます。</p>
<p>今回は、Serverless Frameworkを用いてテンプレートを作成しPython関数を呼び出してみますが、その前にシステムにawscliをインストールして設定しておく必要があります。さらに、AWSクレデンシャル（ユーザーの認証情報）を設定する必要があります。設定の際は、<a href="https://laboratory.kazuuu.net/using-amazon-sqs-with-boto3-in-python-to-create-a-queue">こちらが参考になるかと思います。</a></p>
<p>また、NodeとNPMをインストールする必要があります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-6" checked><label class="toc-title" for="toc-checkbox-6">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■プロジェクトディレクトリを作成する</a></li><li><a href="#toc3" tabindex="0">■Pythonベースのサーバーレステンプレートを作成する</a><ol><li><a href="#toc4" tabindex="0">■コード</a></li></ol></li><li><a href="#toc5" tabindex="0">■基本的なサービスをAWSにデプロイする</a><ol><li><a href="#toc6" tabindex="0">■コード</a></li></ol></li><li><a href="#toc7" tabindex="0">■Python関数を呼び出す</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>&gt;Windows 10</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>&gt;npm &#8211;version<br />
8.3.1</p>
<p>&gt;node &#8211;version<br />
v16.14.0</p>
<p>&gt;nodeserverless &#8211;version</p>
<p>Framework Core: 3.19.0<br />
Plugin: 6.2.2<br />
SDK: 4.3.2</p>
<p>&gt;aws &#8211;version<br />
aws-cli/2.5.6 Python/3.9.11 Windows/10 exe/AMD64 prompt/off</p>
<h2><span id="toc2">■プロジェクトディレクトリを作成する</span></h2>
<p>今回は、まずはディレクトリを作成します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-121214-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12534" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-121214-1-300x194.png" alt="" width="300" height="194" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-121214-1-300x194.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-121214-1-1024x661.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-121214-1-768x496.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-121214-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回は「C:\Users\user_（フォルダパス）」内に「project_test」というディレクトリを作成しました。</p>
<h2><span id="toc3">■Pythonベースのサーバーレステンプレートを作成する</span></h2>
<p>作成後、Windows10のコマンドプロンプトを起動します。</p>
<pre>C:\Users\user_&gt;cd project_test</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを入力します。cdコマンドで作成した「project_test」ディレクトリに移動します。</p>
<pre>C:\Users\user_\project_test&gt; serverless create --template aws-python --path project_test</pre>
<p>移動後、上記のコマンドを入力し、Enterキーを入力します。&#8211;templateコマンドでaws-pythonというPythonベースのサーバーレステンプレートを作成します。&#8211;pathコマンドでサーバーレステンプレートが置かれるディレクトリ「project_test」を新しく作成するために指定します。</p>
<p>Enterキーを押すと「Setting up a new project」と出力され、新しいプロジェクトの設定が開始されます。しばらくすると「Project successfully created in &#8220;project_test&#8221; from &#8220;aws-python&#8221; template」と出力され、プロジェクトの作成が完了します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122329-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12535" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122329-1-300x145.png" alt="" width="300" height="145" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122329-1-300x145.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122329-1-1024x496.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122329-1-768x372.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122329-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>完了後、「C:\Users\user_\project_test（フォルダパス）」を確認すると、Serverless Frameworkを用いて作成された「project_test」ディレクトリが確認できました。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122610-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12536" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122610-1-300x156.png" alt="" width="300" height="156" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122610-1-300x156.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122610-1-1024x531.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122610-1-768x398.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-122610-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>確認後、この「project_test」ディレクトリ内を確認してみます。確認すると、「.gitignore」、「handler.py」、「serverless.yml」という3つのファイルが生成されていることが確認できました。handler.pyは、ラムダにデプロイされるPythonコードが含まれており、serverless.ymlは、与えられたラムダ関数にどのように、どんなイベントを関連付けるかをServerless Frameworkに伝える設定が含まれています。</p>
<p>なお、handler.pyはどのようなコードが含まれているのかについては下記になります。</p>
<h3><span id="toc4">■コード</span></h3>
<pre>import json


def hello(event, context):
    body = {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "input": event
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    return response

    # Use this code if you don't use the http event with the LAMBDA-PROXY
    # integration
    """
    return {
        "message": "Go Serverless v1.0! Your function executed successfully!",
        "event": event
    }
    """
</pre>
<p>def文を用いてhelloという関数が定義されており、関数を呼び出した時に、bodyのmessageなどがreturnとして返されるものになっています。</p>
<h2><span id="toc5">■基本的なサービスをAWSにデプロイする</span></h2>
<p>「project_test」ディレクトリ内を確認後、基本的なサービスをAWSにデプロイします。</p>
<pre>C:\Users\user_\project_test&gt;cd project_test</pre>
<p>Windows10のコマンドプロンプト上で、「C:\Users\user_\project_test（フォルダパス）」から、さらに「project_test」ディレクトリ内に移動するために、上記のコマンドを入力し、Enterキーを押します。</p>
<pre>C:\Users\user_\project_test&gt;serverless deploy</pre>
<p>移動後、上記のコマンドを入力し、Enterキーを入力します。deployコマンドでAWSにデプロイします。</p>
<pre>Warning: Invalid configuration encountered
  at 'provider.runtime': must be equal to one of the allowed values [dotnet6, dotnetcore3.1, go1.x, java11, java8, java8.al2, nodejs12.x, nodejs14.x, nodejs16.x, provided, provided.al2, python3.6, python3.7, python3.8, python3.9, ruby2.7]

Learn more about configuration validation here: http://slss.io/configuration-validation

Deploying project-test to stage dev (us-east-1)

× Stack project-test-dev failed to deploy (98s)
Environment: win32, node 16.14.0, framework 3.19.0, plugin 6.2.2, SDK 4.3.2
Credentials: Local, "default" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
CREATE_FAILED: HelloLambdaFunction (AWS::Lambda::Function)
Resource handler returned message: "The runtime parameter of python2.7 is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (python3.9) while creating or updating functions. (Service: Lambda, Status Code: 400, Request ID: f14c699d-c997-4179-9490-398d81f85285)" (RequestToken: e6ffe68e-e871-9b88-1b2b-35bb91d196a5, HandlerErrorCode: InvalidRequest)

View the full error: https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#/stack/detail?stackId=arn%3Aaws%3Acloudformation%3Aus-east-1%*********

1 deprecation found: run 'serverless doctor' for more details</pre>
<p>Enterキーを押すと、デプロイが開始されましたが、上記のエラーが出力されました。出力された内容を確認し、問題を解決してみます。</p>
<p>Serverless Frameworkを用いて作成された「project_test」ディレクトリ内に「serverless.yml」が存在しますので、こちらをコードエディタなどで開きます。</p>
<h3><span id="toc6">■コード</span></h3>
<pre>provider:
  name: aws
  runtime: python2.7</pre>
<p>開くと、上記のコードが存在しますので「runtime: python2.7」から「runtime: python3.9」にコードを変更しファイルを保存します。</p>
<pre>C:\Users\user_\project_test&gt;serverless deploy</pre>
<p>保存後、再度上記のコマンドを入力し、Enterキーを入力します。deployコマンドでAWSにデプロイします。</p>
<pre>✔ Service deployed to stack project-test-dev (71s)

functions:
  hello: project-test-dev-hello (389 B)

Toggle on monitoring with the Serverless Dashboard: run "serverless"

</pre>
<p>Enterキーを押すと、「Service deployed to stack project-test-dev」と出力されました。先程のようなエラーは出力されず、デプロイが完了となりました。</p>
<h2><span id="toc7">■Python関数を呼び出す</span></h2>
<p>完了後、Python関数を呼び出してみます。</p>
<pre>C:\Users\user_\project_test\project_test&gt;serverless invoke -f hello -l</pre>
<p>Windows10のコマンドプロンプト上で、上記のコマンドを入力し、Enterキーを押します。「invoke -f」コマンドで呼び出す関数を指定します。今回はhandler.py内のhello関数を指定します。</p>
<pre>{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
--------------------------------------------------------------------
START
END RequestId: *****-****-***-***-*****
END Duration: 0.92 ms Memory Used: 36 MB

Toggle on monitoring with the Serverless Dashboard: run "serverless"

</pre>
<p>Enterキーを押すと、デプロイした関数を呼び出し、returnを出力させることができました。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-130347.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12538" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-130347-300x135.png" alt="" width="300" height="135" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-130347-300x135.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-130347-1024x462.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-130347-768x347.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-08-130347.png 1300w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>なお、「AWS マネジメントコンソール」にログインし、サービスから「Lambda」を選択すると、今回AWSにデプロイした「project-test-dev-hello」という関数が確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/create-templates-and-call-python-functions-using-serverless-framework/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Serverless Frameworkをインストールする（Windows10）</title>
		<link>https://laboratory.kazuuu.net/install-serverless-framework-windows10/</link>
					<comments>https://laboratory.kazuuu.net/install-serverless-framework-windows10/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Thu, 20 Oct 2022 20:41:16 +0000</pubDate>
				<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Serverless Framework]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12527</guid>

					<description><![CDATA[Serverless Frameworkのインストールについて解説しています。 Serverless Frameworkは、Node.jsを使用して作成された無料のオープンソースWebフレームワークとなり、Amazonが [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Serverless Frameworkのインストールについて解説しています。</p>
<p>Serverless Frameworkは、Node.jsを使用して作成された無料のオープンソースWebフレームワークとなり、AmazonがAmazon Web Servicesの一部として提供するサーバーレスコンピューティングプラットフォーム「AWS Lambda」上でアプリケーションを構築するために開発されたフレームワークでもあります。</p>
<p>なお、NPMモジュールであるため、 NodeとNPMをインストールする必要があります。</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-8" checked><label class="toc-title" for="toc-checkbox-8">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■ServerlessFrameworkのインストール</a></li><li><a href="#toc3" tabindex="0">■ServerlessFrameworkのバージョン確認</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>&gt;Windows 10</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>&gt;npm &#8211;version<br />
8.3.1</p>
<p>&gt;node &#8211;version<br />
v16.14.0</p>
<h2><span id="toc2">■ServerlessFrameworkのインストール</span></h2>
<p>それでは、ServerlessFrameworkのインストールを行っていきますので、Windows10のコマンドプロンプトを起動します。</p>
<pre>C:\Users\user_&gt;npm install -g serverless</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを押します。</p>
<pre>npm WARN deprecated formidable@1.2.6: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau
npm WARN deprecated querystring@0.2.1: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated superagent@3.8.3: Please upgrade to v7.0.2+ of superagent.  We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing.  See the releases tab for more information at &lt;https://github.com/visionmedia/superagent/releases&gt;.

added 391 packages, and audited 392 packages in 37s

53 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.3.1 -&gt; 8.12.1
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.12.1
npm notice Run npm install -g npm@8.12.1 to update!
npm notice</pre>
<p>Enterキーを押すと、インストールが開始されます。その後、インストールが完了となります。</p>
<h2><span id="toc3">■ServerlessFrameworkのバージョン確認</span></h2>
<p>完了後、バージョンを確認してみます。</p>
<pre>C:\Users\user_&gt;serverless --version</pre>
<p>確認するために、上記のコマンドを入力し、Enterキーを押します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07_2-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12529" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07_2-1-300x204.png" alt="" width="300" height="204" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07_2-1-300x204.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07_2-1-1024x696.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07_2-1-768x522.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07_2-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<pre>Framework Core: 3.19.0
Plugin: 6.2.2
SDK: 4.3.2
</pre>
<p>Enterキーを押すと、バージョンが出力されました。これでインストールがされていることが確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/install-serverless-framework-windows10/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AWS Lambdaを介してSeleniumを実行するAPIを作成する</title>
		<link>https://laboratory.kazuuu.net/create-an-api-to-run-selenium-via-awslambda/</link>
					<comments>https://laboratory.kazuuu.net/create-an-api-to-run-selenium-via-awslambda/#respond</comments>
		
		<dc:creator><![CDATA[小林]]></dc:creator>
		<pubDate>Wed, 29 Jun 2022 06:00:05 +0000</pubDate>
				<category><![CDATA[Amazon/AWS]]></category>
		<category><![CDATA[Python【初歩から現場実務的なもの】]]></category>
		<category><![CDATA[Serverless Framework]]></category>
		<guid isPermaLink="false">https://laboratory.kazuuu.net/?p=12790</guid>

					<description><![CDATA[AWS Lambdaを介してSeleniumを実行するAPIを作成してみます。（Windows10） 目次 ■PC環境■Selenium Lambdaレイヤーを作成する■Chromedriver Lambdaレイヤーを作 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>AWS Lambdaを介してSeleniumを実行するAPIを作成してみます。（Windows10）</p>

  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-10" checked><label class="toc-title" for="toc-checkbox-10">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">■PC環境</a></li><li><a href="#toc2" tabindex="0">■Selenium Lambdaレイヤーを作成する</a></li><li><a href="#toc3" tabindex="0">■Chromedriver Lambdaレイヤーを作成する</a></li><li><a href="#toc4" tabindex="0">■Lambda関数を作成する</a><ol><li><a href="#toc5" tabindex="0">■コード</a></li><li><a href="#toc6" tabindex="0">■コード(修正前)</a></li></ol></li><li><a href="#toc7" tabindex="0">■コード(修正後その1)</a></li><li><a href="#toc8" tabindex="0">■コード(修正後その2)</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">■PC環境</span></h2>
<p>&gt;Windows 10</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12528" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png" alt="" width="300" height="282" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-300x282.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-1024x963.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1-768x723.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/2022-06-07-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>&gt;sam &#8211;version<br />
SAM CLI, version 1.52.0</p>
<p>&gt;npm &#8211;version<br />
8.3.1</p>
<p>&gt;node &#8211;version<br />
v16.14.0</p>
<p>&gt;nodeserverless &#8211;version</p>
<p>Framework Core: 3.19.0<br />
Plugin: 6.2.2<br />
SDK: 4.3.2</p>
<p>&gt;aws &#8211;version<br />
aws-cli/2.5.6 Python/3.9.11 Windows/10 exe/AMD64 prompt/off</p>
<p>&gt;Python バージョン<br />
Python 3.6.8</p>
<h2><span id="toc2">■Selenium Lambdaレイヤーを作成する</span></h2>
<p>AWSLambdaを介してSeleniumを実行するAPIの作成を行いますが、その前にSelenium Lambdaレイヤーを作成します。作成のためにコマンドプロンプトを起動します。</p>
<pre>&gt;pip3.6 install -t selenium/python/lib/python3.6/site-packages selenium==3.8.0</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを押します。pipを経由してseleniumバージョン3.8.0をインストールします。「-t」オプションで、インストールされるディレクトリを「selenium/python/lib/python3.6/site-packages」に指定します。</p>
<pre>WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Collecting selenium==3.8.0
  Using cached selenium-3.8.0-py2.py3-none-any.whl (941 kB)
Installing collected packages: selenium
Successfully installed selenium-3.8.0
WARNING: Target directory C:\Users\user_\selenium\python\lib\python3.6\site-packages\selenium already exists. Specify --upgrade to force replacement.
WARNING: You are using pip version 21.1.2; however, version 21.3.1 is available.
You should consider upgrading via the 'c:\program files\python36\python.exe -m pip install --upgrade pip' command.</pre>
<p>Enterキーを押すと、インストールが開始されます。「Successfully installed」が表示されれば、正常にインストールが完了したことになります。今回はWARNING（警告）が表示されていますが、エラーではないので一旦、無視します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-072212-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12791" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-072212-1-300x231.png" alt="" width="300" height="231" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-072212-1-300x231.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-072212-1-1024x787.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-072212-1-768x590.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-072212-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>作業ディレクトリ（カレントディレクトリ）内に「selenium」というディレクトリが作成されます。</p>
<p>コマンドプロンプトを起動します。</p>
<pre>&gt;cd selenium</pre>
<p>起動後、上記のコマンドを入力し、Enterキーを押します。cdコマンドでseleniumディレクトリに移動します。</p>
<pre>zip -r python.zip python/</pre>
<p>移動後、上記のコマンドを入力し、Enterキーを押します。Enterキーを押すと、「&#8217;zip&#8217; は、内部コマンドまたは外部コマンド、 操作可能なプログラムまたはバッチ ファイルとして認識されていません。」と出力される場合は、zip.exeのダウンロード（<a href="http://gnuwin32.sourceforge.net/packages/zip.htm">http://gnuwin32.sourceforge.net/packages/zip.htm</a>）を行う必要がある。Binariesのzipファイルをダウンロードし任意のフォルダに解凍。解凍後、「システム環境変数の編集」から環境変数の編集を行います。zipコマンドを用いてファイルを圧縮しpython.zipというzipファイルにします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073436-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12792" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073436-1-300x193.png" alt="" width="300" height="193" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073436-1-300x193.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073436-1-1024x659.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073436-1-768x494.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073436-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、「selenium」に「python.zip」というファイルが生成されます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12793" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-300x104.png" alt="" width="300" height="104" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-300x104.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-1024x355.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-768x266.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>生成後「AWS マネジメントコンソール」にログインし、「Lambda」サービスから左メニューの「レイヤー」をクリックします。クリック後、「レイヤーの作成」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074134-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12794" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074134-1-300x165.png" alt="" width="300" height="165" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074134-1-300x165.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074134-1-1024x564.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074134-1-768x423.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074134-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると「レイヤー設定」が表示されますので、今回は「名前」を「selenium」にし、「説明 &#8211; オプション」を「Selenium layer」と入力します。入力後「.zip ファイルをアップロード」に選択し、「アップロード」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074428-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12795" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074428-1-300x220.png" alt="" width="300" height="220" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074428-1-300x220.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074428-1-1024x751.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074428-1-768x563.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074428-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「開く」ウインドウが表示されますので、先程圧縮したzipファイル（python.zip）を選択し、「開く」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074647-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12796" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074647-1-300x177.png" alt="" width="300" height="177" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074647-1-300x177.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074647-1-1024x604.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074647-1-768x453.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074647-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「アップロード」ボタンの右側に選択したzipファイルが表示されます。この状態で次の設定に移ります。「互換性のあるアーキテクチャ &#8211; オプション」とありますが、今回これは使用しませんので、チェックはなしで、「互換性のあるランタイム &#8211; <i>オプション」で「ランタイム」と表示された入力欄をクリックします。</i></p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074900-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12797" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074900-1-300x172.png" alt="" width="300" height="172" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074900-1-300x172.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074900-1-1024x586.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074900-1-768x439.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074900-1-120x68.png 120w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074900-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックするとメニューが表示されますので「Python 3.6」を選択します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074954-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12798" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074954-1-300x181.png" alt="" width="300" height="181" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074954-1-300x181.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074954-1-1024x619.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074954-1-768x464.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-074954-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>選択後、「ライセンス &#8211; オプション」は使用しませんので、未入力で「作成」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-075131-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12799" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-075131-1-300x156.png" alt="" width="300" height="156" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-075131-1-300x156.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-075131-1-1024x532.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-075131-1-768x399.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-075131-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「レイヤー ***** のバージョン * が正常に作成されました。」と表示されますので、これが表示されればレイヤーの作成は完了となります。</p>
<h2><span id="toc3">■Chromedriver Lambdaレイヤーを作成する</span></h2>
<p>今後は、Chromedriver Lambdaレイヤーを作成します。作成のために、Chromeドライバーをダウンロードしますので、コマンドプロンプト上に戻ります。</p>
<pre>&gt;mkdir -p chromedriver</pre>
<p>戻った後に、上記のコマンドを入力し、Enterキーを押します。「selenium」内に「chromedriver」というディレクトリを作成します。</p>
<pre>&gt;cd chromedriver</pre>
<p>作成後、上記のコマンドを入力し、Enterキーを押します。cdコマンドでchromedriverディレクトリに移動します。</p>
<pre>&gt;curl -SL https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip &gt; chromedriver.zip</pre>
<p>作成後、上記のコマンドを入力し、Enterキーを押します。curlコマンドでchromedriverをダウンロードし、それをchromedriver.zipにします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12800" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-1-300x76.png" alt="" width="300" height="76" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-1-300x76.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-1-1024x260.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-1-768x195.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、「%」が「100」となり、ダウンロードが完了します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-080258-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12801" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-080258-1-300x155.png" alt="" width="300" height="155" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-080258-1-300x155.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-080258-1-1024x528.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-080258-1-768x396.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-080258-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>完了後、「chromedriver」内に「chromedriver.zip」が生成されました。</p>
<pre>&gt;unzip chromedriver.zip</pre>
<p>生成後、コマンドプロンプトで上記のコマンドを入力し、Enterキーを押します。unzipコマンドで、zip形式で圧縮されたアーカイブを展開します。なお「&#8217;unzip.exe&#8217;は、内部コマンドまたは外部コマンド、操作可能なプログラムまたはバッチ ファイルとして認識されていません。」と出力される場合は、unzip.exeをダウンロード（<a href="http://gnuwin32.sourceforge.net/packages/unzip.htm">http://gnuwin32.sourceforge.net/packages/unzip.htm</a>）し、ファイルを解凍。解凍後、「システム環境変数の編集」から環境変数の編集を行う必要があります。</p>
<pre>Archive:  chromedriver.zip
  inflating: chromedriver</pre>
<p>Enterキーを押すと、上記のように出力され、展開が完了します。展開後、chromedriver.zipは削除します。</p>
<pre>&gt;curl -SL https://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-41/stable-headless-chromium-amazonlinux-2017-03.zip &gt; headless-chromium.zip</pre>
<p>これでChromeドライバーのダウンロードは完了ですが、次にChromeバイナリをダウンロードする必要がありますので、コマンドプロンプトで上記のコマンドを入力し、Enterキーを押します。curlコマンドでChromeバイナリをダウンロードし、それをheadless-chromium.zipにします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_2-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12803" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_2-1-300x54.png" alt="" width="300" height="54" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_2-1-300x54.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_2-1-1024x186.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_2-1-768x139.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_2-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、「%」が「100」となり、ダウンロードが完了します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-082243-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12804" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-082243-1-300x160.png" alt="" width="300" height="160" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-082243-1-300x160.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-082243-1-1024x545.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-082243-1-768x409.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-082243-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>完了後、「chromedriver」内に「headless-chromium.zip」が生成されました。</p>
<pre>&gt;unzip headless-chromium.zip</pre>
<p>生成後、コマンドプロンプトで上記のコマンドを入力し、Enterキーを押します。unzipコマンドで、zip形式で圧縮されたアーカイブを展開します。</p>
<pre>Archive:  headless-chromium.zip
  inflating: headless-chromium</pre>
<p>Enterキーを押すと、上記のように出力され、展開が完了します。展開後、headless-chromium.zipは削除します。</p>
<pre>&gt;zip -r chromedriver.zip chromedriver headless-chromium</pre>
<p>削除後、上記のコマンドを入力し、Enterキーを押します。zipコマンドを用いてchromedriverとheadless-chromiumを圧縮しchromedriver.zipというzipファイルにします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12805" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1-300x171.png" alt="" width="300" height="171" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1-300x171.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1-1024x583.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1-768x437.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1-120x68.png 120w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1-160x90.png 160w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_3-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Enterキーを押すと、「chromedriver」内に「chromedriver.zip」が生成されました。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12793" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-300x104.png" alt="" width="300" height="104" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-300x104.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-1024x355.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1-768x266.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-073850-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>生成後、Chromedriver Lambdaレイヤーを作成するために、「AWS マネジメントコンソール」にログインし、「Lambda」サービスから左メニューの「レイヤー」をクリックします。クリック後、「レイヤーの作成」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083141-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12806" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083141-1-300x157.png" alt="" width="300" height="157" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083141-1-300x157.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083141-1-1024x537.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083141-1-768x403.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083141-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「レイヤー設定」が表示されますので、今回は「名前」を「chromedriver」とし、「説明 &#8211; オプション」を「chrome driver and binary layer」と入力します。入力後「.zip ファイルをアップロード」が選択された状態となっていますが、今回アップロードするchromedriver.zipが10 MBより大きいファイルとなるので、このような場合は、アップロードが遅くなる可能性があるので、「Amazon S3 からファイルをアップロードする」を選択します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083526.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12807" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083526-300x115.png" alt="" width="300" height="115" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083526-300x115.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083526-1024x392.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083526-768x294.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083526.png 1283w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>選択後、今回「Lambda」サービスを利用していますが、このサービスがどこの場所であるかを確認します。今回は「米国東部（バージニア北部）「us-east-1」」となっています。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083920-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12808" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083920-1-300x158.png" alt="" width="300" height="158" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083920-1-300x158.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083920-1-1024x538.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083920-1-768x404.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-083920-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>確認後、「S3」サービスへ移動し、今回は「バケット」から「バケットを作成」ボタンをクリックし、新しいバケットを作成します。作成する際に、「AWS リージョン」を、「Lambda」サービスの「米国東部（バージニア北部）「us-east-1」」と同じに設定します。この設定を行わないと、ファイルがアップロードできません。「Error occurred while GetObject. S3 Error Code: PermanentRedirect. S3 Error Message: The bucket is in this region: us-west-2. Please use this region to retry the request」といったエラーが発生します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084158-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12809" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084158-1-300x150.png" alt="" width="300" height="150" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084158-1-300x150.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084158-1-1024x514.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084158-1-768x385.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084158-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>S3バケットを作成後、「オブジェクト」から「アップロード」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084304-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12810" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084304-1-300x154.png" alt="" width="300" height="154" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084304-1-300x154.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084304-1-1024x526.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084304-1-768x394.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084304-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「アップロード」が表示されますので、点線部分にファイルをドラッグアンドドロップするか、「ファイルを追加」ボタンで、「chromedriver」内に「chromedriver.zip」を追加します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084528-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12811" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084528-1-300x225.png" alt="" width="300" height="225" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084528-1-300x225.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084528-1-1024x768.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084528-1-768x576.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084528-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>今回は「ファイルを追加」ボタンで、「chromedriver」内に「chromedriver.zip」を追加し、「アップロード」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084650-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12812" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084650-1-300x136.png" alt="" width="300" height="136" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084650-1-300x136.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084650-1-1024x463.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084650-1-768x347.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084650-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、アップロードが開始されます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_4.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12813" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_4-300x95.png" alt="" width="300" height="95" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_4-300x95.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_4-1024x326.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_4-768x244.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_4.png 1301w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>開始後、しばらくすると、アップロードが完了します。完了後、アップロード画面の右側の「閉じる」ボタンをクリックして、画面を閉じます。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084926-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12814" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084926-1-300x166.png" alt="" width="300" height="166" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084926-1-300x166.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084926-1-1024x565.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084926-1-768x424.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084926-1-160x90.png 160w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-084926-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>閉じると、「オブジェクト」にアップロードしたファイルが追加されますので、このファイルの名前をクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085107-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12815" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085107-1-300x130.png" alt="" width="300" height="130" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085107-1-300x130.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085107-1-1024x443.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085107-1-768x332.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085107-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「オブジェクトの概要」が表示されますので、「オブジェクト URL」をコピーします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085259-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12816" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085259-1-300x203.png" alt="" width="300" height="203" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085259-1-300x203.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085259-1-1024x692.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085259-1-768x519.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085259-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>コピー後、「Lambda」の「レイヤー」の「レイヤー設定」に戻り、「Amazon S3 からファイルをアップロードする」を選択し、「Amazon S3 のリンク URL」の入力欄に、URLを貼り付けます。貼り付けた後に、「互換性のあるアーキテクチャ &#8211; オプション」は選択せずに、「互換性のあるランタイム &#8211; オプション」で入力欄の「ランタイム」をクリックし、「Python 3.6」を選択します。</p>
<p>選択後「ライセンス &#8211; オプション」は未入力のまま、「作成」ボタンをクリックします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085643.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12817" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085643-300x133.png" alt="" width="300" height="133" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085643-300x133.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085643-1024x453.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085643-768x340.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-085643.png 1240w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>クリックすると、「レイヤー ***** のバージョン * が正常に作成されました。」と表示されます。これでChromedriver Lambdaレイヤーの完成となります。</p>
<h2><span id="toc4">■Lambda関数を作成する</span></h2>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115447.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12819" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115447-300x187.png" alt="" width="300" height="187" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115447-300x187.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115447-768x479.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115447.png 794w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>完了後、Lambda関数を作成しますが、その前に「selenium」内に「lambda」ディレクトリを作成します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12820" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1-300x167.png" alt="" width="300" height="167" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1-300x167.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1-1024x571.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1-768x428.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1-120x68.png 120w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1-160x90.png 160w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-115800-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>作成後、「lambda」ディレクトリに「handler.py」というファイルを作成します。これはLambda関数がかかれたものになります。このファイルをコードエディタで開き、Lambda関数を作成します。今回、コードはこちらのものを参考にさせていただきました（<a href="https://dev.to/awscommunity-asean/creating-an-api-that-runs-selenium-via-aws-lambda-3ck3">https://dev.to/awscommunity-asean/creating-an-api-that-runs-selenium-via-aws-lambda-3ck3</a>）。</p>
<h3><span id="toc5">■コード</span></h3>
<pre>from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def main(event, context):
    options = Options()
    options.binary_location = '/opt/headless-chromium'
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--single-process')
    options.add_argument('--disable-dev-shm-usage')

    driver = webdriver.Chrome('/opt/chromedriver',chrome_options=options)
    driver.get('https://laboratory.kazuuu.net/')
    body = f"Headless Chrome Initialized, Page title: {driver.title}"
    driver.close();
    driver.quit();

    response = {
        "statusCode": 200,
        "body":body
    }

    return response</pre>
<p>今回はdriver.get()を用い、括弧内に引数,パラメータとして当サイト（https://laboratory.kazuuu.net/）のURLを渡します。これでURLを開き、driver.titleでWebサイトのタイトルを取得。取得後、body変数に格納し、さらにresponse変数にbody変数を格納。最後にreturnでresponse変数の情報を返すというものです。</p>
<p>コードを記述後、保存します。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-120800-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12821" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-120800-1-300x153.png" alt="" width="300" height="153" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-120800-1-300x153.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-120800-1-1024x522.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-120800-1-768x391.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-120800-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>保存後、「lambda」ディレクトリに「serverless.yaml」というファイルを作成します。このファイルにサーバーレスなアプリケーションを簡単に開発、デプロイするためのツールである「Serverless Framework」の設定を記述します。記述のために、コードエディタで開きます。</p>
<p>今回、コードはこちらのものを参考にさせていただきました（<a href="https://dev.to/awscommunity-asean/creating-an-api-that-runs-selenium-via-aws-lambda-3ck3">https://dev.to/awscommunity-asean/creating-an-api-that-runs-selenium-via-aws-lambda-3ck3</a>）。</p>
<h3><span id="toc6">■コード(修正前)</span></h3>
<pre>service: selenium-lambda

provider:
  name: aws
  runtime: python3.6
  region: ap-southeast-2
  timeout: 900

functions:
  main:
    memorySize: 1000
    handler: handler.main
    events:
      - http:
          path: test
          method: get

    layers:
      - arn:aws:lambda:ap-southeast-2:{}:layer:chromedriver:2
      - arn:aws:lambda:ap-southeast-2:{}:layer:selenium:2

resources:
  Resources:
    ApiGatewayRestApi:
      Properties:
        BinaryMediaTypes:
          - "*/*"</pre>
<p>このコードを記述し、保存後、コマンドプロンプトを起動し、「lambda」ディレクトリ内に移動し、「sls deploy」コマンドを用いてデプロイを行っても、まず「Error:<br />
Deployment bucket has been removed manually. Please recreate it or remove your service and attempt to deploy it again」というエラーが発生するので、コードを変更する。</p>
<h2><span id="toc7">■コード(修正後その1)</span></h2>
<pre>service: web-selenium-lambda-test

provider:
  name: aws
  runtime: python3.6
  region: ap-southeast-2
  timeout: 900

functions:
  main:
    memorySize: 1000
    handler: handler.main
    events:
      - http:
          path: test
          method: get

    layers:
      - arn:aws:lambda:ap-southeast-2:{}:layer:chromedriver:2
      - arn:aws:lambda:ap-southeast-2:{}:layer:selenium:2

resources:
  Resources:
    ApiGatewayRestApi:
      Properties:
        BinaryMediaTypes:
          - "*/*"</pre>
<p>「service: selenium-lambda」を「service: web-selenium-lambda-test」に変更し、保存する。保存後、デプロイする。デプロイするが、下記のエラーが発生する。</p>
<pre>Error:
CREATE_FAILED: MainLambdaFunction (AWS::Lambda::Function)
Resource handler returned message: "1 validation error detected: Value '[arn:aws:lambda:ap-southeast-2:{}:layer:chromedriver:2, arn:aws:lambda:ap-southeast-2:{}:layer:selenium:2]' at 'layers' failed to satisfy constraint: Member must satisfy constraint: [Member must have length less than or equal to 140, Member must have length greater than or equal to 1, Member must satisfy regular expression pattern: (arn:[a-zA-Z0-9-]+:lambda:[a-zA-Z0-9-]+:\d{12}:layer:[a-zA-Z0-9-_]+:[0-9]+)|(arn:[a-zA-Z0-9-]+:lambda:::awslayer:[a-zA-Z0-9-_]+), Member must not be null] (Service: Lambda, Status Code: 400, Request ID: a54e6e64-7feb-4694-9417-a5b8b346872f)" (RequestToken: 048675f6-cb82-1fea-047e-b3464ad19e14, HandlerErrorCode: InvalidRequest)</pre>
<p>このエラーが発生したので、問題の箇所を修正する。</p>
<h2><span id="toc8">■コード(修正後その2)</span></h2>
<pre>service: web-selenium-lambda-test

provider:
  name: aws
  runtime: python3.6
  region: ap-southeast-2
  timeout: 900

functions:
  main:
    memorySize: 1000
    handler: handler.main
    events:
      - http:
          path: test
          method: get

    layers:
      - arn:aws:lambda:us-east-1:*******:layer:chromedriver:2
      - arn:aws:lambda:us-east-1:*******:layer:selenium:2

resources:
  Resources:
    ApiGatewayRestApi:
      Properties:
        BinaryMediaTypes:
          - "*/*"</pre>
<p>まず、providerの「region: ap-southeast-2」を「region: us-east-1 （米国東部（バージニア北部））」に変更します。今回、「Lambda」サービスはus-east-1 （米国東部（バージニア北部））を利用している。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-122758-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12822" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-122758-1-300x147.png" alt="" width="300" height="147" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-122758-1-300x147.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-122758-1-1024x503.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-122758-1-768x377.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-122758-1.png 1250w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>※これは「chromedriver」のレイヤーで、参考として表示しています。</p>
<p>さらに「functions:」の「layers:」を下記のコードに変更する。このコードは、今回作成した「Lambda」の「レイヤー」の「selenium」、「chromedriver」をクリックすると、「バージョン」で「バージョン ARN」が表示されているので、こちらの情報をコピーして、コピーしたもので、上記にも記載していますが、下記のように変更します。</p>
<pre>    layers:
      - arn:aws:lambda:us-east-1:****:layer:chromedriver:2
      - arn:aws:lambda:us-east-1:****:layer:selenium:2</pre>
<p>変更後、保存します。保存後、、コマンドプロンプトを起動し、「lambda」ディレクトリ内に移動し、「sls deploy」コマンドを用いてデプロイを行います。</p>
<pre>Warning: Invalid configuration encountered
at 'resources.Resources.ApiGatewayRestApi': must have required property 'Type'

Learn more about configuration validation here: http://slss.io/configuration-validation

Deploying web-selenium-lambda-test to stage dev (us-east-1)
Warning: Function main has timeout of 900 seconds, however, it's attached to API Gateway so it's automatically limited to 30 seconds.

✔ Service deployed to stack web-selenium-lambda-test-dev (138s)

endpoint: GET - https://****.execute-api.us-east-1.amazonaws.com/dev/test
functions:
main: web-selenium-lambda-test-dev-main (551 B)

1 deprecation found: run 'serverless doctor' for more details

Toggle on monitoring with the Serverless Dashboard: run "serverless"</pre>
<p>デプロイを行うと、デプロイが完了し、上記のメッセージが出力されます。出力内容を確認すると「Warning（警告）」が出力されていますが、「エラー」ではないので、今回は一旦無視します。</p>
<p>「endpoint:」で「GET &#8211; https://****.execute-api.us-east-1.amazonaws.com/dev/test」」と出力されていますので、このURLをコピーします。コピー後、Webブラウザを起動し、アドレスバーに貼り付けて、アクセスします。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-131436-1.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12823" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-131436-1-300x123.png" alt="" width="300" height="123" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-131436-1-300x123.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-131436-1-1024x420.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-131436-1-768x315.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-131436-1.png 1249w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>アクセスすると、AWSLambdaを介してSeleniumを実行し、指定したWebサイトのページタイトルを出力させることができました。</p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1.png"><img loading="lazy" decoding="async" class="alignnone wp-image-12824 size-medium" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-300x108.png" alt="" width="300" height="108" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-300x108.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-1024x370.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1-768x278.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29_1.png 1317w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><a href="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-132030.png"><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-12825" src="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-132030-300x103.png" alt="" width="300" height="103" srcset="https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-132030-300x103.png 300w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-132030-1024x352.png 1024w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-132030-768x264.png 768w, https://laboratory.kazuuu.net/wp-content/uploads/2022/06/スクリーンショット-2022-06-29-132030.png 1273w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>出力後、「AWS マネジメントコンソール」から「CloudFormation」を確認すると新しいスタックが追加されていることと、「S3」で新しいS3バケットが作成されていることが確認できました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://laboratory.kazuuu.net/create-an-api-to-run-selenium-via-awslambda/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
