カテゴリー
Serverless Framework v2で作った古いプロジェクトを深く考えずにv3に移行してみる
※ 当ページには【広告/PR】を含む場合があります。
2023/03/13
1. Serverless Framework v2からv3への移行はさほど難しくない
2. AWSへのデプロイの場合にCloudFormation用のアクセスポリシーが新たに必要
3. Serverless Frameworkプラグインはv3移行後もほぼそのまま使える(と思って良い)
4. doctorサブコマンドを使ってserverless.ymlの非推奨項目を診断する
Serverless Framework v2からv3へのバージョン引き上げ
package.json
{
//...
"dependencies": {
//...
"aws-serverless-express": "^3.4.0",
//...
},
"devDependencies": {
//...
"serverless": "^2.53.1",
"serverless-apigw-binary": "^0.4.4",
"serverless-domain-manager": "^6.0.0",
//...
$ npx sls --version
Framework Core: 2.72.3 (local)
Plugin: 5.5.4
SDK: 4.3.2
Components: 3.18.2
{
//...
"devDependencies": {
//...
"serverless": "^3.28.1",
//....
$ yarn install
$ npx sls --version
Framework Core: 3.28.1 (local)
Plugin: 6.2.3
SDK: 4.3.2
2.72 -> 3.28
5.5 -> 6.2
serverless.yml
$ npx sls deploy -v
Framework Core: 3.28.1 (local)
Plugin: 6.2.3
SDK: 4.3.2
-v
--verbose
--version
--verbose
$ npx sls deploy --verbose
Environment: linux, node 16.17.1, framework 3.28.1 (local), plugin 6.2.3, SDK 4.3.2
Docs: docs.serverless.com
Support: forum.serverless.com
Bugs: github.com/serverless/serverless/issues
Error:
The Serverless version (3.28.1) does not satisfy the "frameworkVersion" (2) in serverless.yml
serverless.yml
serverless.yml
Serverless Frameworkのv3対応
「serverless cli」
$ npx serverless --help
Serverless Framework v3.28.1
Usage
serverless <command> <options>
sls <command> <options>
Get started
Run serverless to interactively setup a project.
Use --help-interactive to display the interactive setup help.
Monitoring
Enable performance and error monitoring with the Serverless Dashboard.
Learn more: https://serverless.com/monitoring
Plugins
Extend the Serverless Framework with plugins.
Explore plugins: https://serverless.com/plugins
Options
--help / -h Show this message
--version / -v Show version info
--verbose Show verbose logs
--debug Namespace of debug logs to expose (use "*" to display all)
Main commands
deploy Deploy a Serverless service
deploy function Deploy a single function from the service
info Display information about the service
invoke Invoke a deployed function
invoke local Invoke function locally
logs Output the logs of a deployed function
Other commands
deploy list List deployed version of your Serverless Service
deploy list functions List all the deployed functions and their versions
metrics Show metrics for a specific function
remove Remove Serverless service and all resources
rollback Rollback the Serverless service to a specific deployment
rollback function Rollback the function to the previous version
test Run HTTP tests
package Packages a Serverless service
plugin install Install and add a plugin to your service
plugin uninstall Uninstall and remove a plugin from your service
print Print your compiled and resolved config file
config Configure Serverless
config credentials Configures a new provider profile for the Serverless Framework
create Create new Serverless service
dashboard Open the Serverless dashboard
doctor Print status on reported deprecations triggered in the last command run
generate-event Generate event
help Show this help
install Install a Serverless service from GitHub or a plugin from the Serverless registry
login Login or sign up for Serverless
logout Logout from Serverless
output get Get value of dashboard deployment profile parameter
output list List all dashboard deployment profile parameters
param get Get value of dashboard service output
param list List all dashboard deployment profile parameters
plugin list Lists all available plugins
plugin search Search for plugins
slstats Enable or disable stats
serverless.yml
service: hoge
frameworkVersion: "2"
plugins:
- serverless-apigw-binary
- serverless-domain-manager
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
memorySize: 192
timeout: 10
stage: ${opt:stage,"dev"}
region: ap-northeast-1
apiGateway:
disableDefaultEndpoint: true
package:
exclude:
- '**'
include:
- 'dist/**'
- 'lambda.js'
- 'package.json'
- 'node_modules/@vendia/**'
- 'node_modules/aws-serverless-express/**'
- 'node_modules/binary-case/**'
- 'node_modules/type-is/**'
- 'node_modules/media-typer/**'
- 'node_modules/mime-types/**'
- 'node_modules/mime-db/**'
custom:
apigwBinary:
types:
- '*/*'
customDomain:
domainName: 'hoge.hoge.com'
basePath: ''
stage: ${self:provider.stage}
createRoute53Record: true
functions:
api:
handler: lambda.universal
events:
- http:
path: /
method: GET
cors:
origin: '*'
headers:
- Content-Type
- X-Amz-Date
- Authorization
- X-Api-Key
- X-Amz-Security-Token
- X-Amz-User-Agent
allowCredentials: false
AWS APIGateway
AWS Lambda
serverless.yml
frameworkVersionタグ
serverless.yml
#...
#frameworkVersion: "2"
frameworkVersion: "3"
#...
$ npx sls deploy --verbose
Deploying hoge to stage dev (ap-northeast-1)
Packaging
Excluding development dependencies for service package
Retrieving CloudFormation stack
Uploading
Uploading CloudFormation file to S3
Uploading State file to S3
Uploading service hoge.zip file to S3 (26.74 MB)
Updating CloudFormation stack
Creating new change set
Waiting for new change set to be created
Change Set did not reach desired state, retrying
Executing created change set
UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - hoge-dev
UPDATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
UPDATE_COMPLETE - AWS::IAM::Role - IamRoleLambdaExecution
UPDATE_IN_PROGRESS - AWS::Lambda::Function - ApiLambdaFunction
UPDATE_COMPLETE - AWS::Lambda::Function - ApiLambdaFunction
CREATE_IN_PROGRESS - AWS::Lambda::Version - ApiLambdaVersionJxxxxxxxxxxxxxxxxxxxxxxxxxxxx4
CREATE_IN_PROGRESS - AWS::Lambda::Version - ApiLambdaVersionJxxxxxxxxxxxxxxxxxxxxxxxxxxxx4
CREATE_COMPLETE - AWS::Lambda::Version - ApiLambdaVersionJxxxxxxxxxxxxxxxxxxxxxxxxxxxx4
CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1000000000004
CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1000000000004
CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1000000000004
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - hoge-dev
DELETE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1000000000005
DELETE_SKIPPED - AWS::Lambda::Version - ApiLambdaVersionoerJxxxxxxxxxxxxxxxxxxxxxxxk
DELETE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1000000000005
UPDATE_COMPLETE - AWS::CloudFormation::Stack - hoge-dev
Retrieving CloudFormation stack
V1 - Updated API mapping from '(none)'
to '(none)' for 'hoge.hoge.com'
Removing old service artifacts from S3
✔ Service deployed to stack hoge-dev (140s)
endpoints:
GET - https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/
GET - https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/{proxy+}
functions:
api: hoge-dev-api (27 MB)
Stack Outputs:
DomainName: hoge.hoge.com
HostedZoneId: XXXXXXXXXXXXXX
ApiLambdaFunctionQualifiedArn: arn:aws:lambda:ap-northeast-1:000000000000:function:hoge-dev-api:18
DistributionDomainName: xxxxxxxxxxxxxx.cloudfront.net
ServiceEndpoint: https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev
ServerlessDeploymentBucketName: hoge-dev-serverlessdeploymentbucket-xxxxxxxxxxxx
Serverless Domain Manager:
Domain Name: hoge.hoge.com
Target Domain: xxxxxxxxxxxxxx.cloudfront.net
Hosted Zone Id: XXXXXXXXXXXXXX
2 deprecations found: run 'serverless doctor' for more details
Improve API performance – monitor it with the Serverless Console: run "serverless --console"
【AWSの場合】ポリシーの追加
- cloudformation:CreateChangeSet
- cloudformation:DeleteChangeSet
- cloudformation:DescribeChangeSet
- cloudformation:ExecuteChangeSet
ChangeSet
serverless.ymlの非推奨項目を診断する・「sls doctor」コマンド
$ npx sls deploy list --verbose
2023-03-12 03:29:31 UTC
Timestamp: 1678591771043
Files:
- compiled-cloudformation-template.json
#...中略
2 deprecations found: run 'serverless doctor' for more details
$ npx sls doctor --verbose
2 deprecations triggered in the last command:
Support for "package.include" and "package.exclude" will be removed in the next major release. Please use "package.patterns" instead
More info: https://serverless.com/framework/docs/deprecations/#PACKAGE_PATTERNS
Setting "20201221" for "provider.lambdaHashingVersion" is no longer effective as new hashing algorithm is now used by default. You can safely remove this property from your configuration.
More info: https://serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_PROPERTY
まとめ
1. Serverless Framework v2からv3への移行はさほど難しくない
2. AWSへのデプロイの場合にCloudFormation用のアクセスポリシーが新たに必要
3. Serverless Frameworkプラグインはv3移行後もほぼそのまま使える(と思って良い)
4. doctorサブコマンドを使ってserverless.ymlの非推奨項目を診断する
記事を書いた人
ナンデモ系エンジニア
主にAngularでフロントエンド開発することが多いです。 開発環境はLinuxメインで進めているので、シェルコマンドも多用しております。 コツコツとプログラミングするのが好きな人間です。
カテゴリー