ã¯ããã«
ä»å¹´(2022å¹´)1ææ«ã«ãServerless Frameworkãã¡ã¸ã£ã¼ãã¼ã¸ã§ã³ã¢ãããããversion3ããªãªã¼ã¹ããã¾ããã
ä»ã¾ã§ãªããªã触ãæ©ä¼ããªãã£ãã®ã§ãããå æ¥å°ã触ãæ©ä¼ããã£ãã®ã§ãversion3ã®æ°æ©è½ãè¨è¼ãããã¨æãã¾ãã
ã¾ããversion2ããåå¨ããæ©è½ã«ã¤ãã¦ããç§ãç¥ããªãã£ããã®ãè¨è¼ãããã¨æãã¾ãã
注æ
- å
¨æ©è½ãè¨è¼ãã¦ããããã§ã¯ãªãã§ãã
- ããã¾ã§ãèªåãæ°ã¥ããç¹ããä¸å¿ã§ãã
- ãã¨ãéè¦ãããªãã®ãæ¸ãã¦ãã¾ãã
åèãµã¤ã
version3ã®æ°æ©è½
ãã©ã¡ã¼ã¿ã®stageæ¯è¨å®
stageæ¯ã«ç°ãªããã©ã¡ã¼ã¿(=å¤æ°)ãããå ´å(ãã¡ã¤ã³åããã¬ãã£ãã¯ã¹ãªã©)ãããããstageåä½ã§åå¥ã«è¨å®ãããã«ãªãã¾ããã
params: prd: domain: hogehoge.com stg: domain: stg.hogehoge.com dev: domain: dev.hogehoge.com
ä»ã¾ã§ã ã¨custom
ã»ã¯ã·ã§ã³ã«stageæ¯ã®å¤æ°ãè¨å®ãã¦ãã¾ããããããã使ããã¨ã§æå®ãã·ã³ãã«ã«åºæ¥ãããã«ãªãã¾ãã
# ä¾ # ä¾ãã°ãversion2ã¾ã§ã¯ããã ã£ããã© custom: stages: dev: domain: dev.hogehoge.com stg: domain: stg.hogehoge.com prd: domain: hogehoge.com provider: environment: APP_DOMAIN: ${self:custom.stages.${sls:stage}.domain} # version3ã§ã¯ããã§ãããªã params: prd: domain: hogehoge.com stg: domain: stg.hogehoge.com dev: domain: dev.hogehoge.com provider: environment: APP_DOMAIN: ${param:domain}
ã¾ãä¸è¨ã®serverless.yamlã®éããself:
ãä¸è¦ã«ãªã£ãã¿ããã§ãã
(ã¡ãªã¿ã«ver3.22.0ãããself:
ã¯å®å
¨ã«åé¤ãããã¿ããã§ã)
CLIã®ãã¶ã¤ã³æ´æ°
Version3ã«ãªã£ã¦ãCLIã®ãã¶ã¤ã³ãæ´æ°ãããã·ã³ãã«ã«ãªãã¾ããã
(ã¨ãã£ã¦ããèªåã¯ããã¾ã§æèãã¦ãªãã¦ãä¸è¨å
¬å¼ããã°ãè¦ã¦åãã¦ç¥ãã¾ãã...)
åèï¼https://www.serverless.com/blog/serverless-framework-v3-is-live (ä¸è¨ãã¼ã¸ã®ãRedesigned CLI experienceããåç §)
å ¬å¼ããã°ãè¦ãéããå ¨è¬çã«version2ããã·ã³ãã«ã«ãªã£ãæãããã¾ãã
API Gateway v1(Rest API)ã®ãã©ã¼ãããå¤æ´
API Gateway v1(Rest API)ã«ã¤ãã¦ãapiGateway
ã¨ããé
ç®ãproviderç´ä¸ã«æå®ããªãã¨ãããªããªãã¾ããã
(ä»ã¾ã§ã¿ããã«ãproviderç´ä¸ã«ãã©ã¡ã¼ã¿ãæå®ã§ããªã)
API Gateway v2(HTTP API)ã追å ããããã¨ã«ããå¤æ´ã ã¨æããã¾ãã
åèï¼ API Gateway
# 詳細ãªè¨å®é ç®ã¯å ¬å¼ãµã¤ã(https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml?prod_DOCS_SEARCH%5Bquery%5D=self%3A#provider)ãåç § provider: # version2ã®å®ç¾© # providerç´ä¸ã«è¨å®ãè¨è¼ãã apiKeys: ... resourcePolicy: ... usagePlan: ... .. # version3ã®å®ç¾© # providerç´ä¸apiGatewayãã¼ãå®ç¾©ãããã®ä¸ã«è¨å®ãè¨è¼ãã apiGateway: apiKeys: ... resourcePolicy: ... usagePlan: ... ...
packageãåã³IAMã®å®ç¾©å 容å¤æ´
packageã»ã¯ã·ã§ã³åã³providerã»ã¯ã·ã§ã³ã®IAMå®ç¾©ã«ã¤ãã¦ãv2ã®è¨è¼ãdeprecated(éæ¨å¥¨)ã¨ãªããv3ç¬èªã®æ¸ãæ¹ãæ¨å¥¨ãããããã«ãªãã¾ããã
(ãã ãRest APIã¨éãããã¡ãã¯deprecatedãªã ãã§ãv3ã§ãv2ã®è¨æ³èªä½ã¯ä½¿ãã¾ã)
åç
§ï¼Deprecated features that will be kept in v3
詳細ã¯ä¸è¨serverless.ymlãåç §ãã¦ãã ããã
# å ¬å¼ãµã¤ããã # v2ã§ã®æ¸ãæ¹ provider: role: ... rolePermissionsBoundary: ... iamRoleStatements: ... iamManagedPolicies: ... cfnRole: ... package: exclude: - 'src/**' include: - src/function/handler.js # v3ã§ã®æ¸ãæ¹ provider: # IAMã«ã¤ãã¦ãiamãã¼ã®åå«è¦ç´ ã¨ãã¦åè¨å®ãå®ç¾©ããããã«ãªã£ã iam: role: name: ... permissionsBoundary: ... statements: ... managedPolicies: ... deploymentRole: ... # packageã«ã¤ãã¦ã¯ãexclude/includeãå»æ¢ããã # æ£è¦è¡¨ç¾ã®ã«ã¼ã«ã«æ²¿ã£ã¦æ¸ãããã«ãªã£ã package: patterns: - '!src/**' - src/function/handler.js
v2ãããããã©ç¥ããªãã£ãæ©è½
.envãã¡ã¤ã«ä½¿ç¨
dotenv(=npmã¢ã¸ã¥ã¼ã«)ã®æ©è½ãç¨ãã¦ãå種ã®å¤.env(ã¾ãã¯.env.{stageå})ãã¡ã¤ã«ããèªã¿è¾¼ãã§ä½¿ç¨ãããã¨ãå¯è½ã«ãªãã¾ããã
使ç¨ããã«ã¯useDotenv
ããããã£ãtrueã«ããã°OKã§ãã(ããã©ã«ãã¯false)
useDotEnv: true
ã¾ã.env.{stageå}ãã¡ã¤ã«ãç¨æããå ´åã該å½ã¹ãã¼ã¸ã®deployãªã©ãå®æ½ãããã¨ããã®ãã¡ã¤ã«ãèªã¿è¾¼ã¾ãã¾ãã(ããã©ã«ãã¯stageå=dev
)
API Gateway以å¤ã®Lambdaããªã¬ç¨ãªã½ã¼ã¹ãæå®å¯è½ã«
ä»ã¾ã§(Lambdaããªã¬ç¨ã¨ãã¦)providerã»ã¯ã·ã§ã³ã«å®ç¾©ã§ããã®ã¯API Gatewayã ãã§ãããããã以å¤ã®ãªã½ã¼ã¹ãproviderã§å®ç¾©å¯è½ã«ãªãã¾ããã(ä»ã¾ã§ã¯resources
ã»ã¯ã·ã§ã³ã«ã¾ã¨ãã¦å®ç¾©ãã¦ãã)
â»IAMãå®ç¾©å¯è½ã§ãããLambdaããªã¬ç¨ã§ã¯ãªãã§ãã
ä¸è¨ãªã½ã¼ã¹ãå®ç¾©å¯è½ã§ãã
- ALB
- ECR
- CloudFront
- VPC
- S3ãã±ãã
- Cloudwatch Logs(ä¸è¨ã«é¢ãããã°)
詳細ãªå®ç¾©ã¯å ¬å¼ãµã¤ãã®åã»ã¯ã·ã§ã³ãåèã«ãã¦ãã ããã(å ¬å¼ãµã¤ããããªã詳ããï¼åãããããæ¸ãã¦ããã®ã§ããã¡ããè¦ãã®ããã¹ã¹ã¡ã§ãã®ã§...)
providerã§API Gateway v2(HTTP API)ãæå®å¯è½ã«
API Gatewayã®v2(=以ä¸HTTP API)ãproviderã»ã¯ã·ã§ã³ã§æå®å¯è½ã«ãªãã¾ããã
providerç´ä¸ã«httpApi
ã¨ããé
ç®ãè¨å®ãããã®ä¸ã«å種ãã©ã¡ã¼ã¿ã®å¤ãè¨å®ããã°OKã§ãã
å
ç¨v1(Rest API)ãapiGateway
ã¨ããé
ç®ãproviderç´ä¸ã«æå®ããªãã¨ãããªããªã£ãã¨æ¸ãã¾ããããHTTP API追å ã®å½±é¿ãããã§ããããã
åèï¼ API Gateway
# 詳細ãªè¨å®é ç®ã¯å ¬å¼ãµã¤ã(https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml?prod_DOCS_SEARCH%5Bquery%5D=self%3A#provider)ãåç § provider: # v2(HTTP APIã®å®ç¾©) httpApi: name: hogehoge ... # v1(Rest APIã®å®ç¾©) apiGateway: apiName: fugafufa ...
functions(Lambdaå®ç¾©)ã§ãAPI Gateway v2(HTTP API)ãæå®å¯è½ã«
provideråæ§ãLambdaé¢æ°ã®ããªã¬å ã¨ãã¦HTTP APIãæå®å¯è½ã«ãªãã¾ããã
ä¸è¨ã®ããã«ãHTTP APIã®å ´åeventsã«ãhttpApiããæå®ãã¾ãã
# å ¬å¼ãµã¤ãããæç² functions: hello: handler: index.handler events: # ãã¡ãã¯HTTP API - httpApi: method: GET path: /some-get-path/{param} # ãã£ã¡ã¯Rest API(ãã¡ãã¯å¤æ´ãªã) - http: path: users/create method: get
AWS Lambda Function URL対å¿
ä»å¹´4æã«çºè¡¨ããããAWS Lambda Function URL(=Lambdaé¢æ°URL)ã«å¯¾å¿ãã¾ããã
åã«AWS Lambda Function URLã«å¯¾å¿ãããå ´åãurlãã¼ã«trueãè¨å®ããã ãã§OKã§ãã
ã¾ããauthorizer
åã³cors
ãè¨å®ããã¨ããªã¼ã½ã©ã¤ã¶ã¼ãCORSã®ç´°ããè¨å®ãã§ãã¾ãã(詳細ã¯ä¸è¨ãµã¤ããåç
§)
åèï¼Lambda Function URLs
# å ¬å¼ãµã¤ãããæç² functions: hello: handler: index.handler #åã«å¯¾å¿ãããã ããªããurl:trueãè¨å®ããã ãã§OK url: true hello2: handler: index2.handler url: # 追å é ç®ã¨ãã¦ããªã¼ã½ã©ã¤ã¶ãCORSãè¨å®å¯è½ authorizer: aws_iam cors:true
ã¾ã¨ã
以ä¸ãversion3ã®æ°æ©è½ï¼ç¥ããªãã£ãæ©è½(ã®ä¸é¨)ã«ãªãã¾ãã
ãã¡ãã«ç ´å£çå¤æ´(Breaking Changes)ã®ä¸è¦§ãè¨è¼ããã¦ãã¾ãããè¦ãéãããã¾ã§è´å½çãªå¤æ´ã¯ç¡ãããã«æãã¾ããã(serviceãAPI Gateway v1ãããã§ãããä¿®æ£ã¯ãã£ã¨ããéã«çµãããã)
ã¾ããä»åã¯ç´¹ä»ããããªãã£ãæ©è½ãããã¾ãã®ã§ãèå³ãããæ¹ã¯ãã²èª¿ã¹ã¦ã¿ã¦ãã ããã
ããã§ã¯ãä»åã¯ãã®è¾ºã§ã