| 12 | from constructs import Construct |
| 13 | |
| 14 | class MyCustomResource(Construct): |
| 15 | |
| 16 | def __init__(self, scope: Construct, id: str, bucket_name): |
| 17 | super().__init__(scope, id) |
| 18 | |
| 19 | res = AwsCustomResource( |
| 20 | scope=self, |
| 21 | id='AWSCustomResource', |
| 22 | policy=AwsCustomResourcePolicy.from_sdk_calls(resources=[f'arn:aws:s3:::{bucket_name}/*']), |
| 23 | log_retention=logs.RetentionDays.INFINITE, |
| 24 | on_create=self.create(bucket_name), |
| 25 | on_delete=self.delete(bucket_name), |
| 26 | resource_type='Custom::MyCustomResource' |
| 27 | ) |
| 28 | |
| 29 | def create(self, bucket_name): |
| 30 | |
| 31 | create_params = { |
| 32 | "Body": "Hello world", |
| 33 | "Bucket": bucket_name, |
| 34 | "Key": "helloWorld.txt" |
| 35 | } |
| 36 | |
| 37 | return AwsSdkCall( |
| 38 | action='putObject', |
| 39 | service='S3', |
| 40 | parameters=create_params, |
| 41 | physical_resource_id=PhysicalResourceId.of('myAutomationExecution') |
| 42 | ) |
| 43 | |
| 44 | def delete(self, bucket_name): |
| 45 | |
| 46 | delete_params = { |
| 47 | "Bucket": bucket_name, |
| 48 | "Key": "helloWorld.txt" |
| 49 | } |
| 50 | |
| 51 | return AwsSdkCall( |
| 52 | action='deleteObject', |
| 53 | service='S3', |
| 54 | parameters=delete_params, |
| 55 | physical_resource_id=PhysicalResourceId.of('myAutomationExecution') |
| 56 | ) |