<?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>PHP &#8211; Few Steps &#8211; ก้าวสั้นๆ แต่ไปเรื่อยๆ</title>
	<atom:link href="https://myifew.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://myifew.com</link>
	<description></description>
	<lastBuildDate>Sun, 08 Mar 2020 04:24:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://myifew.com/wp-content/uploads/2018/07/cropped-logo6-ts-32x32.png</url>
	<title>PHP &#8211; Few Steps &#8211; ก้าวสั้นๆ แต่ไปเรื่อยๆ</title>
	<link>https://myifew.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>ลองทำ PHP Serverless ง่ายๆ ด้วย bref</title>
		<link>https://myifew.com/5628/build-php-serverless-by-bref/</link>
					<comments>https://myifew.com/5628/build-php-serverless-by-bref/#respond</comments>
		
		<dc:creator><![CDATA[iFew]]></dc:creator>
		<pubDate>Sat, 07 Mar 2020 16:30:08 +0000</pubDate>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[AWS Lambda]]></category>
		<category><![CDATA[Microservices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Serverless]]></category>
		<guid isPermaLink="false">https://myifew.com/?p=5628</guid>

					<description><![CDATA[ไหนๆ ก็เขียน PHP อยู่แล้ว เลยมาลองทำเป็น Serverless ทดสอบบน AWS Lambda สักหน่อย ถ้าวิธีปกติคือทำเป็น Custom Runtime แต่ขั้นตอนอาจจะยุ่งยากนิดนึง เลยมีคนทำเครื่องมือชื่อ bref ขึ้นมา ใช้ง่ายมากครับ&#8230;]]></description>
										<content:encoded><![CDATA[
<p>ไหนๆ ก็เขียน PHP อยู่แล้ว เลยมาลองทำเป็น Serverless ทดสอบบน AWS Lambda สักหน่อย ถ้าวิธีปกติคือทำเป็น <a rel="noreferrer noopener" aria-label="PHP Custom Runtime (opens in a new tab)" href="https://aws.amazon.com/blogs/apn/aws-lambda-custom-runtime-for-php-a-practical-example/" target="_blank">Custom Runtime</a> แต่ขั้นตอนอาจจะยุ่งยากนิดนึง เลยมีคนทำเครื่องมือชื่อ <a rel="noreferrer noopener" href="https://bref.sh/" target="_blank">bref</a> ขึ้นมา ใช้ง่ายมากครับ ลองมาดูวิธีทำกัน</p>



<span id="more-5628"></span>



<h2 class="wp-block-heading">bref คืออะไร</h2>



<p><a rel="noreferrer noopener" href="https://bref.sh/" target="_blank">bref</a> เป็นเครื่องมือ open source หนึ่งที่ไว้ deploy และ run serverless สำหรับภาษา PHP โดยมันไป integrate กับ <a rel="noreferrer noopener" href="https://serverless.com/" target="_blank">Serverless Framework</a> อีกที (แน่นอน ต้องติดตั้ง Serverless Framework ก่อน) ซึ่งปัจจุบันใช้ได้เฉพาะ AWS Lambda เท่านั้น</p>



<h2 class="wp-block-heading">สิ่งที่ต้องมีในเครื่อง</h2>



<ul class="wp-block-list"><li>PHP 7.2+</li><li>Composer</li><li>AWS account (ต้องมี permission อย่างน้อย: CloudFormation, CloudWatch, Lambda, S3, API Gateway)</li><li>Node.js</li></ul>



<h2 class="wp-block-heading">จัดแจงเตรียมเครื่องมือทำงาน</h2>



<p><strong>ตรียม AWS Credential Key และ Secret Code</strong> (ถ้ามีแล้ว ข้ามได้ครับ)</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="663" height="294" src="https://myifew.com/wp-content/uploads/2020/03/AWS_Security_Credentials9-1.png" alt="" class="wp-image-5629"/></figure>



<p><strong>Install serverless</strong> (ถ้ามีแล้ว ข้ามได้ครับ)</p>



<pre class="wp-block-code"><code>npm install -g serverless</code></pre>



<p><strong>Config serverless with AWS</strong> (ถ้าเคยทำแล้ว ข้ามได้ครับ)</p>



<pre class="wp-block-code"><code>serverless config credentials --provider aws --key &lt;key> --secret &lt;secret></code></pre>



<p><strong>สร้างโฟลเดอร์ที่จะใช้งาน</strong></p>



<pre class="wp-block-code"><code>mkdir php-bref-function</code></pre>



<p><strong>Install bref</strong></p>



<pre class="wp-block-code"><code>composer require bref/bref</code></pre>



<h2 class="wp-block-heading">ทำ PHP Function Serverless ตัวแรก</h2>



<p><strong>Create PHP Lambda</strong></p>



<pre class="wp-block-code"><code>vendor/bin/bref init</code></pre>



<p>เมื่อ Enter จะแสดงข้อความให้เลือกประเภท Lambda</p>



<pre class="wp-block-code"><code>What kind of lambda do you want to create? (you will be able to add more functions later by editing `serverless.yml`) [PHP function]:
  [0] PHP function
  [1] HTTP application
  [2] Console application</code></pre>



<p>ในที่นี้เราจะสร้าง PHP Function สามารถกด Enter ผ่านไป หรือกดคีย์หมายเลข 0 ก็ได้ จะได้ไฟล์ประมาณนี้</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1752" height="680" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-init.png" alt="" class="wp-image-5630"/></figure>



<p><strong>Config PHP Lambda</strong></p>



<p>เมื่อสร้าง Application เสร็จ ให้แก้ไขไฟล์ serverless.yml ดังนี้</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1258" height="900" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-config.png" alt="" class="wp-image-5631"/></figure>



<p>โดย</p>



<p><strong>provider &gt; region</strong> : คือทวีปที่เราใช้ AWS โดยทั่วไปที่ไทยใช้เป็น ap-southeast-1 <br><strong>functions &gt; php-bref-function</strong> : โดย php-bref-function ชื่อ function เราแก้ตามที่ต้องการได้เลย<br><strong>functions &gt; handler</strong> : ช่องทางแรกที่ Lambda จะเรียกทำงาน<br><strong>functions &gt; layers</strong> : เป็น custom runtime แต่ในที่นี้ bref ได้สร้างเป็น layer เพื่อเรียกใช้งาน ไม่เอามาผสมกับ core code ของเรา (ทำความเข้าใจ <a rel="noreferrer noopener" aria-label="AWS Lambda Layer (opens in a new tab)" href="https://myifew.com/5189/working-with-aws-lambda-layers-and-csharp-net-core2/" target="_blank">AWS Lambda Layer คืออะไร</a>)</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong>อธิบายเพิ่มเติมเกี่ยวกับ Layers</strong><br><strong>สำหรับ PHP functions ให้ระบุเป็น php-74 หรือ php-73 </strong><br>ใช้สำหรับสร้าง PHP function เพื่อประมวลผลเท่านั้น ไม่ใช่  HTTP Application<br><br><strong>สำหรับ HTTP applications ให้ระบุเป็น php-74-fpm หรือ php-73-fpm </strong><br>โดย runtime ตัวนี้จะใช้ PHP-FPM เพื่อทำงานแบบ HTTP applications จะเหมือนกับที่เราเขียน PHP ปกติ คือเขียนเสร็จแล้วต้องเปิดผ่าน PHP hosting หรือพวก Nginx, Apache (สามารถใช้ได้กับ Symfony และ Laravel ได้ด้วยนะ)<br>(Read more at <a href="https://bref.sh/docs/runtimes/#bref-runtimes" target="_blank" rel="noreferrer noopener" aria-label="https://bref.sh/docs/runtimes/#bref-runtimes (opens in a new tab)">https://bref.sh/docs/runtimes/#bref-runtimes</a>)</p></blockquote>



<h2 class="wp-block-heading">ทดสอบ PHP Function Serverless บน localhost</h2>



<p>เมื่อสร้างเสร็จแล้ว ลองทดสอบบนเครื่องตัวเองได้นะครับ ด้วยคำสั่งนี้</p>



<pre class="wp-block-code"><code>serverless invoke local --docker -f &lt;function-name></code></pre>



<p>เปลี่ยน &lt;function-name&gt; ให้เป็นชื่อ function ของเรา ซึ่งที่นี้คือ <strong>php-bref-function</strong> โดยผลลัพธ์ &#8220;Hello World&#8221;</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1754" height="288" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-invocation-localhost.png" alt="" class="wp-image-5632"/></figure>



<p>แต่ถ้าอยากส่ง data payload เข้าไป ให้ใช้คำสั่งนี้</p>



<pre class="wp-block-code"><code>serverless invoke local --docker -f &lt;function-name> --data '{"name": "iFew"}'</code></pre>



<p>ผลลัพธ์ที่ได้คือ &#8220;Hello iFew&#8221;</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1754" height="292" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-invocation-localhost-payload.png" alt="" class="wp-image-5633"/></figure>



<h2 class="wp-block-heading">ทำการ Deploy PHP Function Serverless เพื่อทดสอบบน AWS Lambda</h2>



<p>จะลอง Deploy ขึ้นไปทดสอบบน AWS Lambda ด้วยคำสั่ง</p>



<pre class="wp-block-code"><code>serverless deploy</code></pre>



<p>จะได้ผลลัพธ์ประมาณนี้</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1380" height="762" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-deploy.png" alt="" class="wp-image-5635"/></figure>



<p>สังเกตว่ามีจะมีการ pack file และทำการ upload ขึ้นไปไว้บน S3 เพื่อพักไว้ก่อน และ Deploy ไปที่ Lambda อีกที</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2644" height="1122" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-on-aws-lambda.png" alt="" class="wp-image-5636"/></figure>



<p>ทดสอบอีกรอบว่า บน AWS Lambda ใช้งานได้จริงหรือไม่</p>



<pre class="wp-block-code"><code>serverless invoke -f &lt;function-name></code></pre>



<p>ถ้าใช้งานได้ ก็จะแสดง output ออกมาตามที่โค้ดเราเขียนไว้ </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1148" height="94" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-invocation.png" alt="" class="wp-image-5637"/></figure>



<h2 class="wp-block-heading">ผลการทดสอบความเร็ว ของ PHP Function Serverless ด้วย bref</h2>



<p>ทำงานด้วย Memory 1024MB (ค่ามาตรฐาน) ได้ประมาณ​ 14-15ms</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2604" height="1268" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-memory1024mb.png" alt="" class="wp-image-5638"/></figure>



<p>ทำงานด้วย Memory 256MB ได้ประมาณ​ 75-80ms</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2590" height="1218" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-memory256mb.png" alt="" class="wp-image-5639"/></figure>



<p>ทำงานด้วย Memory 128MB ได้ประมาณ​ 270-290ms</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2590" height="1234" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-function-memory128mb.png" alt="" class="wp-image-5640"/></figure>



<h2 class="wp-block-heading">ลองทำ PHP HTTP Application Serverless ดูบ้าง</h2>



<p>วิธีการทำแบบเดิมเลยครับ ต่างกันตอนเลือกประเภท Lambda ให้เลือกประเภท [1] HTTP application แทน พร้อมกับแก้ serverless.yml ในหัวข้อ function &gt; layer ให้เป็น php-74-fpm หรือ php-73-fpm แทน</p>



<p>วิธีการทดสอบบน localhost ให้ใช้คำสั่ง ดังนี้</p>



<pre class="wp-block-code"><code>php -S localhost:8000 index.php</code></pre>



<p>จากนั้นเปิด browser ไปที่ http://localhost:8000 จะเจอหน้าตาแบบนี้</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2866" height="1492" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-http-invocation-localhost.png" alt="" class="wp-image-5641"/></figure>



<p>ซึ่งเมื่อเรา Deploy ไปอยู่บน AWS Lambda ขั้นตอนของ bref จะทำการ upload ไป S3 และ deploy ไป Lambda เหมือนกับกรณี PHP Function แต่เพิ่มเติมคือ จะไปทำการสร้าง API Gateway ให้ด้วย เพื่อเป็นช่องทางการ access เข้าไปใช้งาน ไม่ว่าจะเป็น Website หรือ API (ให้ดูที่ endpoints)</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1428" height="820" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-http-deploy.png" alt="" class="wp-image-5642"/></figure>



<p>ถ้าลองเปิด url ดูแล้วก็จะได้เหมือนกับที่ลองใน localhost</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-http-on-aws-lambda.png" alt="" class="wp-image-5643" width="580" height="301"/></figure>



<p>ส่วนผลการทดสอบความเร็ว จะไม่สามารถ invocation ได้นะครับ ต้องเปิดผ่าน API Gateway เท่านั้น</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2602" height="1248" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-http-cant-invocation.png" alt="" class="wp-image-5644"/></figure>



<p>ผมลองดู SPeed Network ด้วย Chrome แต่ความเร็วที่ได้ มันผ่าน stack ชั้น API Gateway ก่อน เลยทำให้ไม่รู้ว่า AWS Lambda จริงๆทำงานเร็วแค่ไหน แต่ผลลัพธ์ที่ได้ อยู่ราวๆ 80-100ms ถือว่าเร็วเลยหละ</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="2880" height="1238" src="https://myifew.com/wp-content/uploads/2020/03/php-bref-http-test-on-chrome-1.png" alt="" class="wp-image-5650"/></figure>



<h2 class="wp-block-heading">วิธีการลบ PHP Serverless Application บน AWS Lambda</h2>



<p>ผมแนะนำว่าให้ใช้คำสั่งแทนนะครับ เพราะการไปลบบน AWS Lambda อย่างเดียว อาจจะไม่ครบ อาจจะต้องไปตามหาใน S3, CloudFormation, API Gateway และอาจจะหลายสิ่ง จึงแนะนำให้ใช้คำสั่งของ serverless แทน ดังนี้ครับ</p>



<pre class="wp-block-code"><code>serverless remove</code></pre>



<p>อยากลบตัวไหน ให้เข้าไปที่ folder ของ application นั้น มันจะทำการลบตามที่เรา config ในไฟล์ serverless.yml ครับ</p>



<h2 class="wp-block-heading">สรุป</h2>



<p>ถ้าใครอยากทำ Serverless ด้วย PHP และ AWS Lambda ผมแนะนำให้ใช้ <a rel="noreferrer noopener" href="https://bref.sh/" target="_blank">bref</a> เลยครับ ค่อนข้างสะดวก ส่วนเรื่องความเร็วที่ได้ถือว่าประทับใจ เมื่อเทียบกับ .NET Core  (ใน Memory เท่ากัน) แต่ก็ยังช้ากว่า Node.js, Go, Python</p>



<p><strong>ข้อสังเกต</strong></p>



<ol class="wp-block-list"><li>Default Memory คือ 1024MB ระวังให้ดีว่าจะเผลอเสียเงินเยอะโดยใช้เหตุครับ</li><li>Default Timeout คือ 6 วินาที</li><li>ไม่พบ Cold Start จากการ run ครั้งแรก เพราะครั้งต่อๆ ไป ก็ยังทำงานด้วยเวลาพอกัน</li></ol>



<p><strong>ตัวอย่างโค้ด</strong></p>



<ul class="wp-block-list"><li>PHP HTTP Application : <a href="https://github.com/ifew/php-bref-http">https://github.com/ifew/php-bref-http</a></li><li>PHP Function : <a href="https://github.com/ifew/php-bref-function">https://github.com/ifew/php-bref-function</a></li></ul>
]]></content:encoded>
					
					<wfw:commentRss>https://myifew.com/5628/build-php-serverless-by-bref/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
