使用 Cloudformation 进行代码部署

Using Cloudformation for Codedeploy

我正在尝试使用对流层制作云形成模板。模板制作的堆栈旨在成为 codedeploy

的一个简单示例

之前有对流层生成ec2、s3、iam和route53生成模板。我之前使 codedeploy 与 GUI AWS 控制台一起工作

我遇到了一个错误,我不明白为什么

这是我的对流层程序

    from troposphere import Template
    from troposphere.codedeploy import Application, DeploymentConfig, DeploymentGroup, MinimumHealthyHosts, S3Location


    #1
    #CreateApplication with an applicationName
    app=Application( 'test123')

    #2
    #CreateDeploymentConfig

    config=DeploymentConfig('config123',
                           MinimumHealthyHosts=MinimumHealthyHosts( Type='HOST_COUNT', Value=1 )
                           )

    #3
    #CreateDeploymentGroup using applicationName as input
    #and deploymentConfigName
    depgroup=DeploymentGroup("depgroup123",
                             ApplicationName='test123',
                             DeploymentConfigName='config123', 
                             ServiceRoleArn='arn:aws:iam::930517348825:role/isCompatibleWithAWSCodeDeploy')
)    
    t = Template()
    t.add_description(
    """
    Test of making a code deploy setup from a template
    """)
    t.add_resource(app)
    t.add_resource(config)
    t.add_resource(depgroup)

    print(t.to_json())

这是它制作的 Cloudformation 模板

{
    "Description": "\nTest of making a code deploy setup from a template\n",
    "Resources": {
        "config123": {
            "Properties": {
                "MinimumHealthyHosts": {
                    "Type": "HOST_COUNT",
                    "Value": 1
                }
            },
            "Type": "AWS::CodeDeploy::DeploymentConfig"
        },
        "depgroup123": {
            "Properties": {
                "ApplicationName": "test123",
                "DeploymentConfigName": "config123",
                "ServiceRoleArn": "arn:aws:iam::930517348825:role/isCompatibleWithAWSCodeDeploy"
            },
            "Type": "AWS::CodeDeploy::DeploymentGroup"
        },
        "test123": {
            "Type": "AWS::CodeDeploy::Application"
        }
    }
}

当我将模板加载到 cloudformation GUI 时,这发生在回滚之前

09:08:50 UTC+0000   CREATE_FAILED   AWS::CodeDeploy::DeploymentConfig   config123   Resource creation cancelled
09:08:48 UTC+0000   CREATE_IN_PROGRESS  AWS::CodeDeploy::DeploymentConfig   config123   
09:08:48 UTC+0000   CREATE_FAILED   AWS::CodeDeploy::DeploymentGroup    depgroup123     No application found for name: test123
09:08:47 UTC+0000   CREATE_COMPLETE     AWS::CodeDeploy::Application    test123     

我不明白的是堆栈已经创建了 AWS::CodeDeploy::Application "test123" 但是下一条消息说 "No application found for name: test123"

role/isCompatibleWithAWSCodeDeploy 已经存在。
我还需要添加其他内容吗?

TLDR

只需使用 Refs:

            "ApplicationName": {"Ref": "test123"},                                                                                                                                                        
            "DeploymentConfigName": {"Ref": "config123"},

详细解答

从你的cf模板来看,创建的应用程序没有名称。为了让它有一个名字,你的 cf 模板的适当部分应该如下所示:

    "test123": {                                                                                                                                                                                          
        "Type": "AWS::CodeDeploy::Application",
        "Properties" : {
            "ApplicationName" : "test123"
        }                                                                                                                                                           
    }

同样适用于配置。配置应该有 DeploymentConfigName 属性。 但即使在添加 ApplicationName 启动后也可能会失败。因为你的模板不保证 depgroup 是在 application 和 deploymentconfig 之后创建的。

为了保证正确的创建顺序,您应该使用 Refs:

{                                                                                                                                                                                                             
    "Description": "\nTest of making a code deploy setup from a template\n",                                                                                                                                  
    "Resources": {                                                                                                                                                                                            
        "config123": {                                                                                                                                                                                        
            "Properties": {                                                                                                                                                                                   
                "MinimumHealthyHosts": {                                                                                                                                                                      
                    "Type": "HOST_COUNT",                                                                                                                                                                     
                    "Value": 1                                                                                                                                                                                
                }                                                                                                                                                                                             
            },                                                                                                                                                                                                
            "Type": "AWS::CodeDeploy::DeploymentConfig"                                                                                                                                                       
        },                                                                                                                                                                                                    
        "depgroup123": {                                                                                                                                                                                      
            "Properties": {                                                                                                                                                                                   
                "ApplicationName": {"Ref": "test123"},                                                                                                                                                        
                "DeploymentConfigName": {"Ref": "config123"},                                                                                                                                                 
                "ServiceRoleArn": "arn:aws:iam::930517348825:role/isCompatibleWithAWSCodeDeploy"                                                                                                                     
            },                                                                                                                                                                                                
            "Type": "AWS::CodeDeploy::DeploymentGroup"                                                                                                                                                        
        },                                                                                                                                                                                                    
        "test123": {                                                                                                                                                                                          
            "Type": "AWS::CodeDeploy::Application"                                                                                                                                                            
        }                                                                                                                                                                                                     
    }                                                                                                                                                                                                         
}