aws-cloudformation - 使用 Code Pipeline 通过 NestedStack 部署 Cloudformation

标签 aws-cloudformation aws-codepipeline

我正在使用 Code Pipeline 部署 Cloudformation 模板。问题是这个 Cloudformation 模板有一些嵌套堆栈。嵌套堆栈模板需要位于 S3 存储桶中。因此,在触发主(父)CF 模板之前,我需要将 CF 嵌套堆栈上传到 S3。

我没有找到使用 Code Pipeline 来做到这一点的方法。

有什么建议吗?

最佳答案

一种方法是使用 Git Hook 将嵌套堆栈复制到 S3,例如接收后 Hook 。

另一种方法是在管道中添加另一个阶段来调用 Lambda 函数。您可以关注这个article配置此步骤。当您设置“输入工件”字段时,CodePipeline 会将工件 zip 文件的路径作为事件的一部分传递。然后 Lambda 函数提取 zip 文件并将您的堆栈上传到您的存储桶。

下面是一个示例 Python 代码,它将工件下载并提取到/tmp:

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)

关于aws-cloudformation - 使用 Code Pipeline 通过 NestedStack 部署 Cloudformation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510574/

相关文章:

amazon-web-services -/usr/bin/env : 'ruby' : No such file or directory userdata in codedeploy ubuntu

amazon-web-services - 如何清理 AWS CDKToolkit 堆栈的资源

amazon-web-services - AWS Code Pipeline Stage 最适合放置 ECR Docker 镜像更新

amazon-web-services - CodePipeline、CodeBuild、CloudFormation、Lambda : build multiple lambdas in a single build and assign their code correctly

amazon-web-services - AWS CodePipeline 能否在不劫持 CodeBuild 的工件设置的情况下触发 AWS CodeBuild?

amazon-web-services - AWS CodePipeLine :Execute deploy action in diffent region than the one codepipeline is triggered in

regex - 在 AWS Cloudwatch Logs 中过滤电子邮件地址?

amazon-web-services - AWS Fargate : unable to pull secrets or registry auth

amazon-web-services - 以 YAML 格式将 !Sub 与 !GetAtt 组合

amazon-web-services - AWS Codepipeline 中的失败步骤通知