我用Go语言尝试了Serverless Framework的模板
总结
我已经尝试使用aws-go在Serverless Framework的模板创建功能中进行部署。
环境
-
- Node v8.11.1
-
- npm 5.6.0
-
- Go 1.10.3
- IAMユーザが作成されていること(AWS APIを使って操作するため)
Serverless Framework是什么
以下的步骤都会自动完成,可以节省开发时的工作量,非常方便。
– 生成Lambda函数
– 上传函数
– 设置触发事件的触发器
安装Serverless Framework。
请使用以下命令进行安装。
全局安装Serverless:npm install -g serverless
设置凭据
请输入已创建的IAM用户的访问密钥和秘密访问密钥。
❯ vim ~/.aws/credentials
[hoge]
aws_access_key_id = piyo
aws_secret_access_key = huga
如果需要访问多个环境并且需要使用不同的IAM用户,建议根据每个环境设置适当的命名。
配置設定 de
❯ vim ~/.aws/config
[hoge]
output = json
region = ap-northeast-1 //好きなリージョンで設定してください。
尝试使用模板创建新项目
当设置完成后,我们终于可以使用sls的模板来创建新项目了。
请务必在$GOPATH以下执行以下命令。
~/go/src/github.com/xxxxx/sls-sample目录下执行以下命令:
sls create –template aws-go
执行后,将会创建以下文件。
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
type Response struct {
Message string `json:"message"`
}
func Handler() (Response, error) {
return Response{
Message: "Go Serverless v1.0! Your function executed successfully!",
}, nil
}
func main() {
lambda.Start(Handler)
}
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
type Response struct {
Message string `json:"message"`
}
func Handler() (Response, error) {
return Response{
Message: "Okay so your other function also executed successfully!",
}, nil
}
func main() {
lambda.Start(Handler)
}
我觉得您可以确认已经创建了这种类型的模板文件。
请对已创建的文件中的serverless.yml进行部分编辑。
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!
service: hoge # 好きなプロジェクト名
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
provider:
name: aws
runtime: go1.x
# you can overwrite defaults here
# stage: dev
# region: us-east-1
region: ap-northeast-1 #リージョンを設定してください(今回は東京)
# you can add statements to the Lambda function's IAM Role here
# iamRoleStatements:
# - Effect: "Allow"
# Action:
# - "s3:ListBucket"
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ] }
# - Effect: "Allow"
# Action:
# - "s3:PutObject"
# Resource:
# Fn::Join:
# - ""
# - - "arn:aws:s3:::"
# - "Ref" : "ServerlessDeploymentBucket"
# - "/*"
# you can define service wide environment variables here
# environment:
# variable1: value1
package:
exclude:
- ./**
include:
- ./bin/**
functions:
hello:
handler: bin/hello
world:
handler: bin/world
# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
# events:
# events:
# - http:
# path: users/create
# method: get
# - s3: ${env:BUCKET}
# - schedule: rate(10 minutes)
# - sns: greeter-topic
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000
# - alexaSkill: amzn1.ask.skill.xx-xx-xx-xx
# - alexaSmartHome: amzn1.ask.skill.xx-xx-xx-xx
# - iot:
# sql: "SELECT * FROM 'some_topic'"
# - cloudwatchEvent:
# event:
# source:
# - "aws.ec2"
# detail-type:
# - "EC2 Instance State-change Notification"
# detail:
# state:
# - pending
# - cloudwatchLog: '/aws/lambda/hello'
# - cognitoUserPool:
# pool: MyUserPool
# trigger: PreSignUp
# Define function environment variables here
# environment:
# variable2: value2
# you can add CloudFormation resource templates here
#resources:
# Resources:
# NewResource:
# Type: AWS::S3::Bucket
# Properties:
# BucketName: my-new-bucket
# Outputs:
# NewOutput:
# Description: "Description for the output"
# Value: "Some output value"
部署和执行
当文件的设置完成后,让我们尝试实际部署的步骤。
/〜/go/src/github.com/xxxxx/sls-sample 可以被改写成:
$ make build
go get github.com/aws/aws-lambda-go/lambda
env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go
env GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go
请使用您在最初设置的 credentials 和 config 的名称(在此处为 hoge)来指定并执行以下命令:$ sls deploy –aws-profile hoge。
$ sls deploy --aws-profile hoge
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (4.5 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
........................
Serverless: Stack update finished...
Service Information
service: hoge
stage: dev
region: ap-northeast-1
stack: hello-dev
api keys:
None
endpoints:
None
functions:
hello: hello-dev-hello
world: hello-dev-world
如果出现了这样的显示,表示部署成功!
总结
我意外地很顺利地到达了这一点。
除了aws-go之外,还有很多其他模板可以使用,所以也可以尝试其他的。
附赠
总的来说,这是关于提到了有许多模板的描述的内容。
Template "true" is not supported. Supported templates are: "aws-nodejs", "aws-
nodejs-typescript", "aws-nodejs-ecma-script", "aws-python", "aws-python3", "aws-
groovy-gradle", "aws-java-maven", "aws-java-gradle", "aws-kotlin-jvm-maven", "aws-
kotlin-jvm-gradle", "aws-kotlin-nodejs-gradle", "aws-scala-sbt", "aws-csharp",
"aws-fsharp", "aws-go", "aws-go-dep", "azure-nodejs", "fn-nodejs", "fn-go",
"google-nodejs", "kubeless-python", "kubeless-nodejs", "openwhisk-java-maven",
"openwhisk-nodejs", "openwhisk-php", "openwhisk-python", "openwhisk-swift",
"spotinst-nodejs", "spotinst-python", "spotinst-ruby", "spotinst-java8",
"webtasks-nodejs", "plugin" and "hello-world".