我在 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}}
The 无服务器框架文档 https://serverless.com/framework/docs/providers/aws/cli-reference/remove/说以下关于sls remove
:
The sls remove
命令将删除已部署的服务,定义在
您当前的工作目录,来自提供商。
So 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/ https://virtualbonzo.com/2017/12/11/did-you-manually-delete-a-resource-created-by-aws-cloudformation/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)