When deploying serverless Lambda it will create a bucket with the code and CloudFormation template. It is totally fine, but the newly created bucket will be associated with the stack and will prevent us from purging the whole stack.
When do we want to delete a stack?
During development there could be many reasons to completely remove one, few ideas:
- our Lambda’s type changed, thus it cannot be just updated
- we had to experiment with roles and want to test them after clean serverless installation
- none of the resources are required anymore
According to the default AWS policy an S3 bucket cannot be deleted if it is not empty, which exactly is preventing us from purging the stack.
What can one do?
It is really straightforward: create a bucket for serverless deployment. If we manually add a new S3 bucket and point our serverless installer to that, it won’t be interfering with our stack.
When the bucket is there we can add these two lines under provider to our serverless to make it use that for deployment:
deploymentBucket:
name: serverless-dep-bucket-dev
Updating the utc-time-teller’s serverless it looks like this:
service: BlogDavidPythonLambdaDeploy
provider:
name: aws
region: eu-west-1
deploymentBucket:
name: serverless-dep-bucket-dev
functions:
utc-time-teller:
name: utc-time-teller
handler: src/lambda_handler.handler
memorySize: 128
timeout: 30
runtime: python3.8
events:
- http:
path: /{proxy+}
method: any
- http:
path: /
method: any