2017年12月26日火曜日

雑誌のハンズオンコードをデプロイ(ServerlessFramework)メモ

Software Design 2017年10月号にServerlessFrameworkのハンズオンが乗っていたのでやってみた。

最初は、記事の通り真似すれば直ぐできるかと思いきや、いくつか問題があって手こずっていた。

S3 Bucketが既に存在している
デプロイすると以下のエラーになっていた。

Serverless: Operation failed!

Serverless Error ---------------------------------------

An error occurred: S3BucketSampleuploads - sample-thumbnails already exists.

aws console -> S3で調べても同じ名前のBucketは存在しない。

yml, jsをgrepして、該当Bucket名を検索->rename

<pre>
sample-thumbnails  ->  sample-thumbnails-park
sample-uploads     ->  sample-uploads-park
</pre>

失敗したデプロイはCloudFormationからスタックを削除


リージョン合わせが必要

Bucket名を変えてもデプロイに失敗。
us-west-2になっているjsをgrepし、ap-northeast-1に変更


成功したデプロイのログ
[park@12b4c2e1d102 my-services]$ sls deploy -v

Serverless: Packaging service...

Serverless: Excluding development dependencies...

Serverless: Creating Stack...

Serverless: Checking Stack create progress...

CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - my-services-dev

CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket

CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket

CloudFormation - CREATE_COMPLETE - AWS::S3::Bucket - ServerlessDeploymentBucket

CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - my-services-dev

Serverless: Stack create finished...

Serverless: Uploading CloudFormation file to S3...

Serverless: Uploading artifacts...

Serverless: Uploading service .zip file to S3 (42.63 KB)...

Serverless: Validating template...

Serverless: Updating Stack...

Serverless: Checking Stack update progress...

CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - my-services-dev

CloudFormation - UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - my-services-dev

CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - ImagesTable

CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ThumbBucket

CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution

CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - UploadLogGroup

CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - AfterUnderscoreuploadLogGroup

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi

CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - ImagesTable

CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - AfterUnderscoreuploadLogGroup

CloudFormation - CREATE_IN_PROGRESS - AWS::Logs::LogGroup - UploadLogGroup

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::RestApi - ApiGatewayRestApi

CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - UploadLogGroup

CloudFormation - CREATE_COMPLETE - AWS::Logs::LogGroup - AfterUnderscoreuploadLogGroup

CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::RestApi - ApiGatewayRestApi

CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - ThumbBucket

CloudFormation - CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceUpload

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Resource - ApiGatewayResourceUpload

CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Resource - ApiGatewayResourceUpload

CloudFormation - CREATE_COMPLETE - AWS::IAM::Role - IamRoleLambdaExecution

CloudFormation - CREATE_COMPLETE - AWS::S3::Bucket - ThumbBucket

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - UploadLambdaFunction

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - AfterUnderscoreuploadLambdaFunction

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - UploadLambdaFunction

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Function - AfterUnderscoreuploadLambdaFunction

CloudFormation - CREATE_COMPLETE - AWS::Lambda::Function - AfterUnderscoreuploadLambdaFunction

CloudFormation - CREATE_COMPLETE - AWS::Lambda::Function - UploadLambdaFunction

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - UploadLambdaVersionu0H2U6ikL2ClcXUwRAcyUaHlZRIAG5U2eJ7Ea4zYs

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - AfterUnderscoreuploadLambdaPermissionSampleuploadsparkS3

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodUploadPost

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - AfterUnderscoreuploadLambdaVersionZASWO2NsLCCWHRmHfriXS2o4eo3ilM0gZtnhR0tthUc

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - UploadLambdaPermissionApiGateway

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - AfterUnderscoreuploadLambdaPermissionSampleuploadsparkS3

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - UploadLambdaVersionu0H2U6ikL2ClcXUwRAcyUaHlZRIAG5U2eJ7Ea4zYs

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Method - ApiGatewayMethodUploadPost

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Permission - UploadLambdaPermissionApiGateway

CloudFormation - CREATE_IN_PROGRESS - AWS::Lambda::Version - AfterUnderscoreuploadLambdaVersionZASWO2NsLCCWHRmHfriXS2o4eo3ilM0gZtnhR0tthUc

CloudFormation - CREATE_COMPLETE - AWS::Lambda::Version - UploadLambdaVersionu0H2U6ikL2ClcXUwRAcyUaHlZRIAG5U2eJ7Ea4zYs

CloudFormation - CREATE_COMPLETE - AWS::Lambda::Version - AfterUnderscoreuploadLambdaVersionZASWO2NsLCCWHRmHfriXS2o4eo3ilM0gZtnhR0tthUc

CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Method - ApiGatewayMethodUploadPost

CloudFormation - CREATE_COMPLETE - AWS::DynamoDB::Table - ImagesTable

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1514106981709

CloudFormation - CREATE_IN_PROGRESS - AWS::ApiGateway::Deployment - ApiGatewayDeployment1514106981709

CloudFormation - CREATE_COMPLETE - AWS::ApiGateway::Deployment - ApiGatewayDeployment1514106981709

CloudFormation - CREATE_COMPLETE - AWS::Lambda::Permission - AfterUnderscoreuploadLambdaPermissionSampleuploadsparkS3

CloudFormation - CREATE_COMPLETE - AWS::Lambda::Permission - UploadLambdaPermissionApiGateway

CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - S3BucketSampleuploadspark

CloudFormation - CREATE_IN_PROGRESS - AWS::S3::Bucket - S3BucketSampleuploadspark

CloudFormation - CREATE_COMPLETE - AWS::S3::Bucket - S3BucketSampleuploadspark

CloudFormation - UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - my-services-dev

CloudFormation - UPDATE_COMPLETE - AWS::CloudFormation::Stack - my-services-dev

Serverless: Stack update finished...

Service Information

service: my-services

stage: dev

region: ap-northeast-1

stack: my-services-dev

api keys:

  None

endpoints:

  POST - https://xxxxhzz7x1.execute-api.ap-northeast-1.amazonaws.com/dev/upload

functions:

  upload: my-services-dev-upload

  after_upload: my-services-dev-after_upload



Stack Outputs

AfterUnderscoreuploadLambdaFunctionQualifiedArn: arn:aws:lambda:ap-northeast-1:999953934258:function:my-services-dev-after_upload:2

ServiceEndpoint: https://xxxxhzz7x1.execute-api.ap-northeast-1.amazonaws.com/dev

ServerlessDeploymentBucketName: my-services-dev-serverlessdeploymentbucket-1en41wewgq1bs

UploadLambdaFunctionQualifiedArn: arn:aws:lambda:ap-northeast-1:999953934258:function:my-services-dev-upload:2


修正したコードはgithubにpush済み
image_upload_serverless