Table of Contents
使用serverless部署一个dynamodb的common表
在dto子模块的根目录下创建serverless.yml文件
向dto子模块中添加aws dynamodb以及一些工具包的依赖
向service子模块中添加spring和lombok包的依赖
使用serverless部署一个dynamodb的common表
创建一个deploy bucket
这里注意指定一个自己的bucket name,因为s3的bucket名称要求是唯一的
$aws s3api create-bucket --bucket develop-social-network-deploy --region ap-northeast-1 --create-bucket-configuration LocationConstraint=ap-northeast-1
回显如下内容表示创建成功:
{
"Location": "http://develop-social-network-deploy.s3.amazonaws.com/"
}
在父模块的根目录下创建config.yml文件
social-network-serverless/config.yml
使用第一步中创建的bucket name
这里配置文件的bucket name引入了一个stage的变量,主要是用于多环境部署,也可以直接写成第一步创建的名字。
variant: jessica
stage: develop
# The region you will deploy your aws service to
region: ap-northeast-1
# The bucket your upload your resource to, specify your own bucket name
deploymentBucket: ${self:provider.stage}-social-network-deploy
在dto子模块的根目录下创建serverless.yml文件
social-network-serverless/social-network-serverless-dto/serverless.yml
service: social-network-dynamodb
frameworkVersion: ">=1.2.0 <2.0.0"
custom:
configFile: ${file(../config.yml)}
provider:
name: aws
stage: ${self:custom.configFile.stage}
region: ${self:custom.configFile.region}
variant: ${self:custom.configFile.variant}
stackName: ${self:provider.stage}-${self:provider.variant}-${self:service}
deploymentBucket: ${self:custom.configFile.deploymentBucket}
resources:
Parameters:
HashKeyElementName:
Description: HashType PrimaryKey Name
Type: String
Default: hashKey
AllowedPattern: '[a-zA-Z0-9]*'
MinLength: '1'
MaxLength: '2048'
ConstraintDescription: must contain only alphanumberic characters
HashKeyElementType:
Description: HashType PrimaryKey Type
Type: String
Default: S
AllowedPattern: S
MinLength: '1'
MaxLength: '1'
ConstraintDescription: must be S
RangeKeyElementName:
Description: RangeType PrimaryKey Name
Type: String
Default: rangeKey
AllowedPattern: '[a-zA-Z0-9]*'
MinLength: '1'
MaxLength: '2048'
ConstraintDescription: must contain only alphanumberic characters
RangeKeyElementType:
Description: RangeType PrimaryKey Type
Type: String
Default: S
AllowedPattern: S
MinLength: '1'
MaxLength: '1'
ConstraintDescription: must be S
ReadCapacityUnits:
Description: Provisioned read throughput
Type: Number
Default: '1'
MinValue: '1'
MaxValue: '2'
ConstraintDescription: must be between 1 and 2
WriteCapacityUnits:
Description: Provisioned write throughput
Type: Number
Default: '1'
MinValue: '1'
MaxValue: '2'
ConstraintDescription: must be between 1 and 2
Resources:
CommonTable:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: ${self:provider.stage}.common
AttributeDefinitions:
- AttributeName: !Ref HashKeyElementName
AttributeType: !Ref HashKeyElementType
- AttributeName: !Ref RangeKeyElementName
AttributeType: !Ref RangeKeyElementType
KeySchema:
- AttributeName: !Ref HashKeyElementName
KeyType: HASH
- AttributeName: !Ref RangeKeyElementName
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: !Ref ReadCapacityUnits
WriteCapacityUnits: !Ref WriteCapacityUnits
# Print out the name of the table that is created
Outputs:
TableName:
Value: !Ref CommonTable
Description: Table name of the newly created DynamoDB table
该模版定义了一个DynamoDB的通用表,表的名字为develop.common,表的primary key由partion key和sort key两部分组成,partion key的类型为字符串,名字为hashKey, sort key的类型为字符串,名字为rangeKey.
使用serverless命令部署表
$ sls deploy -v
有如下回显表示创建成功
sls deploy -v
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Validating template...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - develop-jessica-social-network-dynamodb
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - CommonTable
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - CommonTable
CloudFormation - CREATE_COMPLETE - AWS::DynamoDB::Table - CommonTable
CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - develop-jessica-social-network-dynamodb
Serverless: Stack create finished...
Service Information
service: social-network-dynamodb
stage: develop
region: ap-southeast-1
stack: develop-jessica-social-network-dynamodb
resources: 1
api keys:
None
endpoints:
None
functions:
None
layers:
None
Stack Outputs
TableName: develop.common
ServerlessDeploymentBucketName: develop-social-network-deploy
配置aws sdk、spirng以及工具包
在父模块中添加aws sdk以及一些工具包的版本管理
在dependencyManagement的dependencies中添加对com.amazonaws:aws-java-sdk-bom的依赖
social-network-serverless/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>