cdk_resource_stack.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from aws_cdk import core
  2. import aws_cdk.aws_s3 as s3
  3. import aws_cdk.aws_dynamodb as ddb
  4. import aws_cdk.aws_sqs as sqs
  5. import aws_cdk.aws_ssm as ssm
  6. import json
  7. import aws_cdk.aws_s3_notifications as s3n
  8. import aws_cdk.aws_s3_deployment as s3d
  9. ssm_parameter_credentials = 's3_migration_credentials'
  10. class CdkResourceStack(core.Stack):
  11. def __init__(self, scope: core.Construct, _id: str, bucket_para, **kwargs) -> None:
  12. super().__init__(scope, _id, **kwargs)
  13. self.ddb_file_list = ddb.Table(self, "s3_migrate_ddb",
  14. partition_key=ddb.Attribute(name="Key", type=ddb.AttributeType.STRING),
  15. billing_mode=ddb.BillingMode.PAY_PER_REQUEST)
  16. self.sqs_queue_DLQ = sqs.Queue(self, "s3_migrate_sqs_DLQ",
  17. visibility_timeout=core.Duration.hours(1),
  18. retention_period=core.Duration.days(14)
  19. )
  20. self.sqs_queue = sqs.Queue(self, "s3_migrate_sqs_queue",
  21. visibility_timeout=core.Duration.hours(1),
  22. retention_period=core.Duration.days(14),
  23. dead_letter_queue=sqs.DeadLetterQueue(
  24. max_receive_count=24,
  25. queue=self.sqs_queue_DLQ
  26. )
  27. )
  28. self.ssm_bucket_para = ssm.StringParameter(self, "s3_migrate_bucket_para",
  29. string_value=json.dumps(bucket_para, indent=4)
  30. )
  31. # You need to manually setup ssm_credential_para in SSM Parameter Store before deploy CDK
  32. # Here import ssm_credential_para, MIND THE VERSION NUMBER MUST BE EXACT THE SAME !!!
  33. # 你需要先手工配置了一个ssm_credential_para,然后在这里导入,注意版本号一致!!!
  34. self.ssm_credential_para = ssm.StringParameter.from_secure_string_parameter_attributes(
  35. self, "ssm_parameter_credentials",
  36. parameter_name=ssm_parameter_credentials,
  37. version=1
  38. )
  39. # New a S3 bucket, new object in this bucket will trigger SQS jobs
  40. # This is not for existing S3 bucket. Jobsender will scan the existing bucket and create sqs jobs.
  41. # 这里新建一个S3 bucket,里面新建Object就会触发SQS启动搬迁工作。
  42. # 对于现有的S3 bucket,不在这里配置,由jobsender进行扫描并生成SQS Job任务。
  43. self.s3bucket = s3.Bucket(self, "s3_migrate_bucket")
  44. self.s3bucket.add_event_notification(s3.EventType.OBJECT_CREATED,
  45. s3n.SqsDestination(self.sqs_queue))
  46. # Deploy code
  47. self.s3_deploy = s3.Bucket(self, "s3_migrate_deploybucket")
  48. s3d.BucketDeployment(self, "deploy_code",
  49. sources=[s3d.Source.asset("./code")],
  50. destination_bucket=self.s3_deploy)
  51. core.CfnOutput(self, 'NewS3Bucket_MigrateObjects', value=self.s3bucket.bucket_name)
  52. core.CfnOutput(self, 'NewS3Bucket_deploy_code', value=self.s3_deploy.bucket_name)