我在serverless.yml文件中定义了以下资源。为我所有不同的开发阶段创建资源非常有用。
resources:
Resources:
uploadBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:service}-${self:custom.stage}-uploads
visitsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.visitsTable}
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: visitId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: visitId
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: ${self:custom.dynamoDbCapacityUnits.${self:custom.stage}}
WriteCapacityUnits: ${self:custom.dynamoDbCapacityUnits.${self:custom.stage}}
问题是...如果我在删除数据库时执行
sls remove
,它也会删除所有其他内容,包括lambda函数及其api网关端点,我需要保留它们,因为我已为它们明确设置了策略。 如何告诉无服务器我只想删除数据库或S3或其他任何东西,而不删除其余的东西? 我尝试过的事情:
我在AWS上手动删除,但是如果您这样做并进行sls部署,它将不会再次创建数据库! 所以不确定执行此操作的最佳方法...
整个Serverless.yml文件
service: mydomain-api
# Use serverless-webpack plugin to transpile ES6/ES7
plugins:
- serverless-webpack
- serverless-domain-manager
custom:
webpackIncludeModules: true
stage: ${opt:stage, self:provider.stage}
visitsTable: "${self:service}-visits-${self:custom.stage}"
domains:
prod: api.mydomain.com
staging: staging-api.mydomain.com
dev: dev-api.mydomain.com
dynamoDbCapacityUnits:
prod: 5
staging: 2
dev: 2
customDomain:
basePath: ""
domainName: ${self:custom.domains.${self:custom.stage}}
stage: "${self:custom.stage}"
certificateName: "mydomain.com"
createRoute53Record: true
provider:
name: aws
runtime: nodejs6.10
stage: prod
region: us-east-1
environment:
VISITS_TABLE: ${self:custom.visitsTable}
# 'iamRoleStatement' defines the permission policy for the Lambda function.
# In this case Lambda functions are granted with permissions to access DynamoDB.
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:us-east-1:*:*"
functions:
create:
handler: src/visits/create.main
events:
- http:
path: visits
method: post
cors: true
authorizer: aws_iam
get:
handler: src/visits/get.main
events:
- http:
path: visits/{id}
method: get
cors: true
authorizer: aws_iam
list:
handler: src/visits/list.main
events:
- http:
path: visits
method: get
cors: true
authorizer: aws_iam
update:
handler: src/visits/update.main
events:
- http:
path: visits/{id}
method: put
cors: true
authorizer: aws_iam
delete:
handler: src/visits/delete.main
events:
- http:
path: visits/{id}
method: delete
cors: true
authorizer: aws_iam
resources:
Resources:
uploadBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:service}-${self:custom.stage}-uploads
visitsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.visitsTable}
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: visitId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: visitId
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: ${self:custom.dynamoDbCapacityUnits.${self:custom.stage}}
WriteCapacityUnits: ${self:custom.dynamoDbCapacityUnits.${self:custom.stage}}
最佳答案
Serverless Framework documentation说了有关sls remove
的以下内容:
The
sls remove
command will remove the deployed service, defined in your current working directory, from the provider.
因此,
sls remove
不是要走的路。只需从visitsTable
中删除serverless.yaml
资源,然后再次运行sls deploy
。无服务器框架在后台使用AWS CloudFormation。因此,手动删除内容并不是一个好主意。请记住:必须通过堆栈更新来管理和修改作为AWS CloudFormation堆栈的一部分创建的资源。 也许这是一本好书:https://virtualbonzo.com/2017/12/11/did-you-manually-delete-a-resource-created-by-aws-cloudformation/
关于amazon-web-services - 销毁通过无服务器创建的资源,而不会销毁Lambda端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47385994/