Is there a way to do this using chalice with CDK integration? when I try deploying using cdk deploy command I get the following error:
Unable to package chalice apps that @app.on_s3_event decorator. CloudFormation does not support modifying the event notifications of existing buckets. You can deploy this app using chalice deploy.
With current version , we can not use on_s3_event with cdk deploy. The only option i think is s3 event -> sqs/sns -> chalice.on_sqs_message . There is one cons is that we need to filter the prefix/suffix by ourself in on_sqs_message handler .
This is example chalice stack infrastructure/stacks/chaliceapp.py
from aws_cdk import (
aws_s3,
aws_sns, ## you can notify sns
aws_sqs, ## you can notify sqs
aws_s3_notifications as aws_s3_noti,
)
from chalice.cdk import Chalice
RUNTIME_SOURCE_DIR = os.path.join(
os.path.dirname(os.path.dirname(__file__)), os.pardir, "runtime"
)
class ChaliceApp(cdk.Stack):
def __init__(self, scope, id, **kwargs):
self.sqs_generic = aws_sqs.Queue(self, "mysqs")
self.bucket = s3.Bucket(self, "MyBucket")
self.chalice = Chalice(
self,
"BaoTranBackend",
source_dir=RUNTIME_SOURCE_DIR,
stage_config={
"lambda_memory_size": 256,
"environment_variables": {
"S3_MAIN_BUCKET": self.bucket.bucket_name,
"SQS_GENERIC" : self.sqs_generic.queue_name,
}
},
)
self.chalice_role = self.chalice.get_role("DefaultRole")
self.bucket.grant_read_write(self.chalice_role)
self.bucket.add_event_notification(aws_s3.EventType.OBJECT_CREATED_PUT, aws_s3_noti.SqsDestination(self.sqs_generic))
Then in your runtime/app.py , you can receive the sqs message :
from chalice import Chalice
app = Chalice(app_name='chalice-backend')
@sqs_events.on_sqs_message(
queue=os.environ.get("SQS_GENERIC", ""),
batch_size=1)
def handle_sqs_message(event):
print('Trigger generic')
print("dict ", event.to_dict())
for record in event:
print(record, 'in event')
logger.info(f" in even ! Detail {record} ")
## with the bucket and the key , you can open boto3 session and implement logic here
app.register_blueprint(sqs_events) ## remember to register events
And after deployment, upload a file to s3 then in cloudwatch , there would be something like this
Is there a way to do this using chalice with CDK integration? when I try deploying using cdk deploy command I get the following error:
Unable to package chalice apps that @app.on_s3_event decorator. CloudFormation does not support modifying the event notifications of existing buckets. You can deploy this app using
chalice deploy
.With current version , we can not use
on_s3_event
withcdk deploy
. The only option i think iss3 event -> sqs/sns -> chalice.on_sqs_message
. There is one cons is that we need to filter the prefix/suffix by ourself in on_sqs_message handler .I assume you follow the cdk deploy tutorial
This is example chalice stack infrastructure/stacks/chaliceapp.py
Then in your
runtime/app.py
, you can receive the sqs message :And after deployment, upload a file to s3 then in
cloudwatch
, there would be something like thisUnfortunately I migrated all my chalice projects to fully CDK :)