cdk_resource_stack.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. )
  17. self.ddb_file_list.add_global_secondary_index(
  18. partition_key=ddb.Attribute(name="desBucket", type=ddb.AttributeType.STRING),
  19. index_name="desBucket-index",
  20. projection_type=ddb.ProjectionType.INCLUDE,
  21. non_key_attributes=["desKey", "versionId"]
  22. )
  23. self.sqs_queue_DLQ = sqs.Queue(self, "s3_migrate_sqs_DLQ",
  24. visibility_timeout=core.Duration.hours(1),
  25. retention_period=core.Duration.days(14)
  26. )
  27. self.sqs_queue = sqs.Queue(self, "s3_migrate_sqs_queue",
  28. visibility_timeout=core.Duration.hours(1),
  29. retention_period=core.Duration.days(14),
  30. dead_letter_queue=sqs.DeadLetterQueue(
  31. max_receive_count=12,
  32. queue=self.sqs_queue_DLQ
  33. )
  34. )
  35. self.ssm_bucket_para = ssm.StringParameter(self, "s3_migrate_bucket_para",
  36. string_value=json.dumps(bucket_para, indent=4)
  37. )
  38. # You need to manually setup ssm_credential_para in SSM Parameter Store before deploy CDK
  39. # Here import ssm_credential_para, MIND THE VERSION NUMBER MUST BE EXACT THE SAME !!!
  40. # 你需要先手工配置了一个ssm_credential_para,然后在这里导入,注意版本号一致!!!
  41. self.ssm_credential_para = ssm.StringParameter.from_secure_string_parameter_attributes(
  42. self, "ssm_parameter_credentials",
  43. parameter_name=ssm_parameter_credentials,
  44. version=1
  45. )
  46. # New a S3 bucket, new object in this bucket will trigger SQS jobs
  47. # This is not for existing S3 bucket. Jobsender will scan the existing bucket and create sqs jobs.
  48. # 这里新建一个S3 bucket,里面新建Object就会触发SQS启动搬迁工作。
  49. # 对于现有的S3 bucket,不在这里配置,由jobsender进行扫描并生成SQS Job任务。
  50. self.s3bucket = s3.Bucket(self, "s3_migrate_bucket")
  51. self.s3bucket.add_event_notification(s3.EventType.OBJECT_CREATED,
  52. s3n.SqsDestination(self.sqs_queue))
  53. # Deploy code
  54. self.s3_deploy = s3.Bucket(self, "s3_migrate_deploybucket")
  55. s3d.BucketDeployment(self, "deploy_code",
  56. sources=[s3d.Source.asset("./code")],
  57. destination_bucket=self.s3_deploy)
  58. core.CfnOutput(self, 'NewS3Bucket_MigrateObjects', value=self.s3bucket.bucket_name)
  59. core.CfnOutput(self, 'NewS3Bucket_deploy_code', value=self.s3_deploy.bucket_name)