用于将内容部署到s3存储桶的结构
aws-cdk.aws-s3-deployment的Python项目详细描述
aws s3部署构造库
This is a developer preview (public beta) module. Releases might lack important features and might have future breaking changes.
This API is still under active development and subject to non-backward compatible changes or removal in any future version. Use of the API is not recommended in production environments. Experimental APIs are not subject to the Semantic Versioning model.
Status: Experimental
这个库允许用.zip文件的内容填充s3存储桶 从另一个s3存储桶或本地磁盘。
下面的示例定义了一个带有web宿主的可公开访问的s3 bucket 启用并从磁盘上的本地目录填充它。
constwebsiteBucket=news3.Bucket(this,'WebsiteBucket',{websiteIndexDocument:'index.html',publicReadAccess: true});news3deploy.BucketDeployment(this,'DeployWebsite',{source: s3deploy.Source.asset('./website-dist'),destinationBucket: websiteBucket,destinationKeyPrefix:'web/static'// optional prefix in destination bucket});
这就是引擎盖下面发生的事情:
- 部署此堆栈(通过
cdk deploy
或通过ci/cd)时, 本地website-dist
目录的内容将被存档和上载 到中间资产桶。 - 构造
BucketDeployment
合成自定义云形成资源 在模板中输入Custom::CDKBucketDeployment
。源bucket/key 设置为指向资产桶。 - 自定义资源下载.zip存档文件,提取它并针对目标bucket发出
aws s3 sync --delete
(在本例中websiteBucket
)。
支持的源
bucket部署支持以下源类型:
- 本地.zip文件:
s3deploy.Source.asset('/path/to/local/file.zip')
- 本地目录:
s3deploy.Source.asset('/path/to/local/directory')
- 另一个桶:
s3deploy.Source.bucket(bucket, zipObjectKey)
删除时保留
默认情况下,当
BucketDeployment
资源从堆栈中删除,或者当目标是
改变。您可以使用选项retainOnDelete: true
来禁用此行为,
在这种情况下,内容将被保留。
云锋失效
您可以提供cloudfront分发和可选路径,以便在bucket部署完成后失效。
constbucket=news3.Bucket(this,'Destination');constdistribution=newcloudfront.CloudFrontWebDistribution(this,'Distribution',{originConfigs:[{s3OriginSource:{s3BucketSource: bucket},behaviors:[{isDefaultBehavior: true}]}]});news3deploy.BucketDeployment(this,'DeployWithInvalidation',{source: s3deploy.Source.asset('./website-dist'),destinationBucket: bucket,distribution,distributionPaths:['/images/*.png'],});
注释
- 这个库使用了一个aws cloudformation自定义资源,大约10mib in 大小。此资源的代码与此库捆绑在一起。
- aws lambda的执行时间限制为15分钟。这限制了 在此超时之前部署到bucket中。
- 当
BucketDeployment
从堆栈中移除时,将保留内容 在目标存储桶(#952)中。 - bucket部署只在堆栈创建/更新期间发生。这意味着 如果你想更新目的地的内容,你需要 更改源s3键(或bucket),以便更新资源。 这符合最佳实践。如果使用本地磁盘资源,这将 由于s3键基于 资产内容的散列。
开发
定制资源是在Python3.6中实现的,以便能够利用
用于“aws sync”的aws cli。代码在^{
这个包在构建期间需要Python3.6来创建自定义 资源lambda包并测试它。它还依赖于一些bash脚本,因此 在windows上构建可能很难。