我正在使用代码管道来部署 Cloudformation 模板。问题是这个 Cloudformation 模板有一些嵌套堆栈。嵌套堆栈模板需要位于 S3 存储桶中。所以在触发主(父)CF 模板之前,我需要将 CF 嵌套堆栈上传到 S3。
我没有找到使用代码管道来做到这一点的方法。
有什么建议么?
一种方法是使用 Git 钩子将嵌套堆栈复制到 S3,例如 post-receive 钩子。
另一种方法是在管道中添加另一个阶段来调用 Lambda 函数。你可以按照这篇文章来配置这一步。当您设置“输入工件”字段时,CodePipeline 会传递工件 zip 文件的路径作为事件的一部分。然后 Lambda 函数提取 zip 文件并将您的堆栈上传到您的存储桶。
以下是将工件下载并提取到 /tmp 的示例 Python 代码:
import boto3
import zipfile
def lambda_handler(event, context):
s3 = boto3.resource('s3')
codepipeline = boto3.client('codepipeline')
artifacts_location = event["CodePipeline.job"]["data"]["inputArtifacts"][0]["location"]["s3Location"]
jobId = event["CodePipeline.job"]["id"]
try:
print("Downloading artifacts")
s3.Bucket(artifacts_location["bucketName"]).download_file(artifact_location["objectKey"], '/tmp/artifacts.zip')
zip_ref = zipfile.ZipFile('/tmp/artifacts.zip', 'r')
zip_ref.extractall('/tmp')
zip_ref.close()
except ClientError as e:
print("Cannot process the artifacts: {}".format(str(e)))
codepipeline.put_job_failure_result(
jobId=jobId,
failureDetails={"type": 'JobFailed', "message": str(e)}
)
return
# Perform the steps to copy your files from /tmp folder.
codepipeline.put_job_success_result(jobId=jobId)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句