I'm using CloudFormation to deploy a stack, whereby a file upload to an existing S3 bucket and object triggers a Lambda function. The stack deploys without errors, however in the UI the connection between the S3 and Lambda resource is not shown, therefore a triggering cannot take place.
I've seen quite a few posts already regarding this topic, all with different flavours to the setup I require. In addition, I have seen the !ImportValue to use, as seen here, however the parameter Bucket is not recognised.
As mentioned already, the S3 bucket and object exist already, so I need to somehow reference an already existing resource in my template.yml. The current status is:
MyTrigger:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Ref LambdaModuleName
CodeUri: src/my_module
Handler: app.lambda_handler
Runtime: python3.9
MemorySize: 7500
Timeout: 600
ReservedConcurrentExecutions: 1
Policies:
- AWSLambdaExecute
- AWSLambdaVPCAccessExecutionRole
- Statement:
- Sid: StagingS3DeleteCreate
Effect: Allow
Action:
- s3:DeleteObject*
- s3:PutObject*
Resource:
- arn:aws:s3:::bucket1/folder1/folder2/*
- Sid: StagingS3List
Effect: Allow
Action:
- s3:List*
Resource:
- arn:aws:s3:::*
# Permissions
AllowS3ToCallLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !Ref MyTrigger
Principal: s3.amazonaws.com
SourceArn: arn:aws:s3:::bucket1/folder1/folder2/
My question is how can deploy a stack and reference this existing bucket and object, so that it triggers the Lambda upon a file upload?
UPDATE
Added:
StagingBucket:
Type: "AWS::S3::Bucket"
DeletionPolicy: Retain
Properties:
BucketName: !Ref S3SourceBucket
where:
S3SourceBucket:
Type: String
Default: "mybucket"