AWS CloudFormation — API Gateway & Lambda

Mart — Tanathip Viriya
2 min readJun 16, 2020

--

ก่อนจะเริ่มเรียนรู้ CloudFormation ระหว่าง APIGateway และ Lambda เราควรจะมาเข้าใจ component ต่างๆๆของ APIGateway ก่อน เพื่อที่จะได้หยิบ resources แต่ละตัวมาใช้งานให้ถูกต้องนั้นเอง

API Gateway 101

การจะใช้งาน API Gateway จริงๆแล้วมีส่วนหลักๆแค่ไม่กี่ส่วนที่ต้องเข้าใจ

  • เราต้องรู้ว่าเราจะใช้ API แบบไหน? เช่น WebSocket API , REST API หรือ HTTP API (โดยบทความนี้ตัวอย่างที่ให้ไปคือ REST API)
  • หลังจากนั้นเราก็ต้องสร้างสิ่งที่เรียกว่า Resource ซึ่งถ้าให้อธิบายง่ายๆ Resource คือแบ่ง layer ของการคุม resource ต่างๆ โดยเราสามารถระบุ resource path ได้ ยกตัวอย่างว่า www.example.com/petstore/pets เจ้า pets คือ resource ของเรา (เจ้า petstore มันคือ Stage ที่เราไว้ Ref deployment ของเรา)
  • หลังจากสร้าง resource สิ่งที่ต้องรู้ตัวต่อไปคือ Method ซึ่งจริงๆคือการระบุ Method ที่เราจะใช้กับ resource นี้นั้นเอง เช่น GET, POST, ANY ใช้กับ pets นั้นเอง
  • สุดท้ายเราก็สร้าง Deployment ที่ทําหน้าที่เป็นเหมือน snapshot ของ API ที่เราสร้างไว้เพื่อให้คนเข้ามาใช้งานจาก deployment ของเรา
  • โดยสิ่งที่เราจะ Deploy ขึ้นไปบน Deployment ก็คือ Stage ที่เป็น Reference ของ deployment นั้นๆๆ

สรุปสร้าง API Gateway — Resource — Deployment — Stage ก็จะพร้อมใช้งาน เพราะฉะนั้นเวลาเราใช้ CloudFormation ก็จะต้องสร้างสิ่งเหล่านี้เพื่อให้มันใช้งานได้นั้นเอง

ทีเหลือก็คือแล้วแต่เราว่า เราจะสร้างสิ่งที่เรียกว่า Model มั้ย? อารมณ์ Object ใน POJO ที่ทําการ setter getter มาให้เรา ใช้ในการ request หรือ response

CloudFormation API Gateway & Lambda

File: https://hta-cloudformation-bucket.s3.amazonaws.com/serverless/lambda-apigateway.json

สิ่งแรกที่อยากจะให้ลองทําและทําแบบเดียวกันทุกๆบทความของ CloudFormation เลยคือการลอง run Cfn ก่อน เพื่อดูว่ามันสร้างอะไรบ้าง จะได้เห็นภาพรวมแล้วค่อยลงมาอ่านทีละจุเอีกที

Sample Method of API gateway

หลังจากที่เรารู้จัก Resource แบบต่างๆของ API Gateway แล้วเราก็แค่สร้างตามที่มัน required ตามด้านบน แต่จุดที่จะมีการงงคือ

  • เวลาเราสร้าง Method (ใน resource) เราจะต้องทําการ Map Integration Response และหลังจากได้ Integration response กลับมาแล้วเราก็ต้อง config Method Response กลับมาด้วย ซึ่งมันจะ Map กันด้วย HTTP status code เช่น 200,400,404,500 เป็นต้น
  • การให้ Permission ระหว่าง Lambda และ Apigateway เราสามารถทําได้สองแบบคือการสร้าง Credential ใน Method แล้วก็กําหรด IAM ให้กับ Method นั้นๆ แต่ถ้าเราไม่ใช้ IAM เราจะใช้ resource-based เราสามารถใช้ Lambda Permission ที่กําหนดให้ API Gateway มา Invoke Lambda function แทนได้
  • ใน Method เวลาเราใส่ URI เพื่อไป Integration กับ Lambda function นั้นๆ มันจะมี Pattern ที่ชัดเจนอยู่ซึ่งก็คือ รูปข้างล่างนี้
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration.html#cfn-apigateway-method-integration-uri

Troubleshooting

  • เวลาทํา Method Integration HTTP ต้องเป็น POST เสมอ เนื่องจากเรื่องของ Invoke บังคับ request เป็น POST
  • circular dependency errors เมื่อใช้ Stage ใน Cloudformation

https://forums.aws.amazon.com/thread.jspa?threadID=236830

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response