# Serverless FrameworkでLambdaとS3を作成する

- URL: https://tech.fusic.co.jp/posts/serverless_framework_lambda_s3/
- 公開日: 2018-10-23
- 著者: shiro seike / せいけ しろう / 清家 史郎
- タグ: Go, serverless
- 掲載: Fusic Tech Blog（株式会社Fusic）

> 清家です。 私がAWSのServerless環境を…[続きを読むServerless FrameworkでLambdaとS3を作成する](https://tech.fusic.co.jp/cloud/serverless_framework_lambda_s3/)<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt -->

---

清家です。  
私がAWSのServerless環境を作成する時、基本的には[Serverless Framework](https://serverless.com)を使います。  
[AWS SAM](https://github.com/awslabs/serverless-application-model)でも良いのですが、Serverless Frameworkは開発が活発で便利なPluginも多く、  
機能追加も非常に早いためです。

インストールには `npm` か `yarm` を利用します。

- npm
```plain; title: 
npm install serverless
```
- 

yarn

```plain; title: 
yarn add serverless
```

`node_modules/serverless/bin/serverless`に実行ファイルがありますので、  
適宜パスを指定したり、 `package.json` の `scripts` に実行スクリプトを追加してください。

serverlessコマンドを使ってテンプレートを作成します。  
テンプレートに一覧は[こちら](https://github.com/serverless/serverless/tree/master/lib/plugins/create/templates)を御覧ください。  
以下はGoのテンプレート(aws-go-dep)を利用しています。

```plain; title: 
serverless create --template aws-go-dep
```

すると、最低限の設定を行なった`serverless.yml` が作成されますので、修正していきます。  
configを`stage`ごとに分けたいので、別途用意したconfigファイルを参照するようにしています。

[ドキュメントはこちら](https://serverless.com/framework/docs/providers/aws/guide/services/)

- serverless.yml
```plain; title: 
service: Save2S3 provider: name: aws runtime: go1.x stage: ${opt:stage, 'dev'} region: ap-northeast-1 timeout: 10 cfLogs: true iamRoleStatements: - Effect: "Allow" Action: - "s3:ListBucket" - "s3:GetObject" - "s3:PutObject" - "s3:DeleteObject" Resource: Fn::Join: - "" - - "arn:aws:s3:::" - ${self:provider.environment.Bucket} - "/\*" environment: Bucket: ${file(config/${self:provider.stage}.json):Bucket} DebugMode: ${file(config/${self:provider.stage}.json):DebugMode} package: exclude: - ./\*\* include: - ./bin/\*\* functions: api: handler: bin/api events: - http: path: save2S3/{Action} method: post resources: Resources: Bucket: Type: AWS::S3::Bucket Properties: BucketName: ${self:provider.environment.Bucket}
```
- 

config/dev.json

```plain; title: 
{ "Bucket":"seike460Save2S3", "DebugMode":"on" }
```

あとはdeployコマンドを実行すればおしまいです。  
CloudFormationをいい感じ作って、deployしてくれます。  
Goで書いてる関係で、makeしています。  
（Makeファイルはserverless frameworkが用意してくれます）

```plain; title: 
make && serverless deploy  Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service .zip file to S3 (7.87 MB)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress...  …省略…  endpoints:  POST - https://秘密.execute-api.ap-northeast-1.amazonaws.com/dev/save2S3/{Action}  Stack Outputs  …秘密…  Serverless: Removing old service versions... Done in 78.62s.
```

とても簡単ですね。  
Serverless　Resourcesをチャッチャか作りたい時にオススメです。

<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->
