销毁通过Serverless创建的资源,而不会破坏Lambda端点

时间:2017-11-20 06:13:32

标签: amazon-web-services aws-lambda aws-api-gateway serverless-framework serverless

我在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}}

3 个答案:

答案 0 :(得分:4)

Serverless Framework documentationsls remove说以下话:

  

sls remove命令将删除在   您当前的工作目录(来自提供商)。

因此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/

答案 1 :(得分:1)

无服务器框架会生成CloudFormation模板。

首次运行CloudFormation模板时,它将创建所有资源。

将来运行它时,它会将新版本与先前版本进行比较,并生成一个弥补差异的计划。 那可能是增加或删除资源。 对此有一些例外,通常是数据资源类型,例如S3存储桶,需要强制删除。

因此,要回答您的问题,应从serverless.yml文件中删除不再需要的资源,然后再次执行sls deploy

答案 2 :(得分:1)

将不需要的资源在删除堆栈时将DeletionPolicy设置为“保留”,并且在运行sls remove时将删除其余资源。

resources:
  Resources:
    uploadBucket:
      Type: AWS::S3::Bucket
      DeletionPolicy: Retain
      ...

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html