AWS CloudFormation + Elastic Beanstalk 错误

Posted

技术标签:

【中文标题】AWS CloudFormation + Elastic Beanstalk 错误【英文标题】:AWS CloudFormation + Elastic Beanstalk error 【发布时间】:2021-11-26 21:02:16 【问题描述】:

我是 AWS CloudFormation 和 Elastic Beanstalk 的新手。我能够在 Elastic Beanstalk 上成功部署 Java(Spring Boot)应用程序,但是当我尝试通过 CloudFormation 进行部署时,虽然我通过 root 帐户运行它,但它无法正常工作。这是一个简单的 CloudFormation 脚本,主要取自官方页面here 和here:

Cloudformation 错误: 未能创建以下资源:[MyInstanceProfile,sampleEnvironment]。用户请求回滚。

CloudFormation Json 文件:


  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": 
    "sampleApplication": 
      "Type": "AWS::ElasticBeanstalk::Application",
      "Properties": 
        "Description": "AWS Elastic Beanstalk Sample Application"
      
    ,
    "sampleApplicationVersion": 
      "Type": "AWS::ElasticBeanstalk::ApplicationVersion",
      "Properties": 
        "ApplicationName":  "Ref": "sampleApplication" ,
        "Description": "AWS ElasticBeanstalk Sample Application Version",
        "SourceBundle": 
          "S3Bucket":  "Fn::Join": [ "-", [ "bucket-elasticbeanstalk",  "Ref": "AWS::Region"  ] ] ,
          "S3Key": "helloworld-0.0.1-SNAPSHOT.jar"
        
      
    ,
    "sampleConfigurationTemplate": 
      "Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
      "Properties": 
        "ApplicationName":  "Ref": "sampleApplication" ,
        "Description": "AWS ElasticBeanstalk Sample Configuration Template",
        "OptionSettings": [
          
            "Namespace": "aws:autoscaling:asg",
            "OptionName": "MinSize",
            "Value": "2"
          ,
          
            "Namespace": "aws:autoscaling:asg",
            "OptionName": "MaxSize",
            "Value": "6"
          ,
          
            "Namespace": "aws:elasticbeanstalk:environment",
            "OptionName": "EnvironmentType",
            "Value": "LoadBalanced"
          
        ],
        "SolutionStackName": "64bit Amazon Linux 2 v3.2.5 running Corretto 11"
      
    ,
    "sampleEnvironment": 
      "Type": "AWS::ElasticBeanstalk::Environment",
      "Properties": 
        "ApplicationName":  "Ref": "sampleApplication" ,
        "Description": "AWS ElasticBeanstalk Sample Environment",
        "TemplateName":  "Ref": "sampleConfigurationTemplate" ,
        "VersionLabel":  "Ref": "sampleApplicationVersion" 
      
    ,  
        "MyInstanceRole": 
            "Type": "AWS::IAM::Role",
            "Properties": 
                "AssumeRolePolicyDocument": 
                    "Version": "2012-10-17",
                    "Statement": [
                        
                            "Effect": "Allow",
                            "Principal": 
                                "Service": [
                                    "ec2.amazonaws.com"
                                ]
                            ,
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        
                    ]
                ,
                "Description": "Beanstalk EC2 role",
                "ManagedPolicyArns": [
                    "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier",
                    "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker",
                    "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier"
                ]
            
        ,
        "MyInstanceProfile": 
            "Type": "AWS::IAM::InstanceProfile",
            "Properties": 
                "Roles": [
                    
                        "Ref": "MyInstanceRole"
                    
                ]
            
        
  

完整的活动:

2021-10-06 09:27:05 UTC+0530    main-cf-sb2-2   ROLLBACK_COMPLETE   -
2021-10-06 09:27:04 UTC+0530    sampleApplication   DELETE_COMPLETE -
2021-10-06 09:26:33 UTC+0530    MyInstanceRole  DELETE_COMPLETE -
2021-10-06 09:26:33 UTC+0530    sampleApplication   DELETE_IN_PROGRESS  -
2021-10-06 09:26:33 UTC+0530    sampleConfigurationTemplate DELETE_COMPLETE -
2021-10-06 09:26:33 UTC+0530    sampleApplicationVersion    DELETE_COMPLETE -
2021-10-06 09:26:32 UTC+0530    sampleConfigurationTemplate DELETE_IN_PROGRESS  -
2021-10-06 09:26:32 UTC+0530    sampleApplicationVersion    DELETE_IN_PROGRESS  -
2021-10-06 09:26:32 UTC+0530    MyInstanceRole  DELETE_IN_PROGRESS  -
2021-10-06 09:26:32 UTC+0530    MyInstanceProfile   DELETE_COMPLETE -
2021-10-06 09:26:32 UTC+0530    sampleEnvironment   DELETE_COMPLETE -
2021-10-06 09:26:31 UTC+0530    MyInstanceProfile   DELETE_IN_PROGRESS  -
2021-10-06 09:26:31 UTC+0530    sampleEnvironment   DELETE_IN_PROGRESS  -
2021-10-06 09:26:28 UTC+0530    main-cf-sb2-2   ROLLBACK_IN_PROGRESS    The following resource(s) failed to create: [MyInstanceProfile, sampleEnvironment]. Rollback requested by user.
2021-10-06 09:26:27 UTC+0530    MyInstanceProfile   CREATE_FAILED   Resource creation cancelled
2021-10-06 09:26:27 UTC+0530    sampleEnvironment   CREATE_FAILED   Environment failed to launch as it entered Terminated state
2021-10-06 09:26:05 UTC+0530    MyInstanceProfile   CREATE_IN_PROGRESS  Resource creation Initiated
2021-10-06 09:26:05 UTC+0530    MyInstanceProfile   CREATE_IN_PROGRESS  -
2021-10-06 09:26:03 UTC+0530    MyInstanceRole  CREATE_COMPLETE -
2021-10-06 09:25:56 UTC+0530    sampleEnvironment   CREATE_IN_PROGRESS  Resource creation Initiated
2021-10-06 09:25:55 UTC+0530    sampleEnvironment   CREATE_IN_PROGRESS  -
2021-10-06 09:25:53 UTC+0530    sampleConfigurationTemplate CREATE_COMPLETE -
2021-10-06 09:25:53 UTC+0530    sampleConfigurationTemplate CREATE_IN_PROGRESS  Resource creation Initiated
2021-10-06 09:25:53 UTC+0530    sampleApplicationVersion    CREATE_COMPLETE -
2021-10-06 09:25:53 UTC+0530    sampleApplicationVersion    CREATE_IN_PROGRESS  Resource creation Initiated
2021-10-06 09:25:52 UTC+0530    sampleApplicationVersion    CREATE_IN_PROGRESS  -
2021-10-06 09:25:52 UTC+0530    sampleConfigurationTemplate CREATE_IN_PROGRESS  -
2021-10-06 09:25:50 UTC+0530    sampleApplication   CREATE_COMPLETE -
2021-10-06 09:25:50 UTC+0530    sampleApplication   CREATE_IN_PROGRESS  Resource creation Initiated
2021-10-06 09:25:50 UTC+0530    MyInstanceRole  CREATE_IN_PROGRESS  Resource creation Initiated
2021-10-06 09:25:50 UTC+0530    MyInstanceRole  CREATE_IN_PROGRESS  -
2021-10-06 09:25:50 UTC+0530    sampleApplication   CREATE_IN_PROGRESS  -
2021-10-06 09:25:46 UTC+0530    main-cf-sb2-2   CREATE_IN_PROGRESS  User Initiated

【问题讨论】:

完整的错误是什么? 在哪里可以找到错误?我在 CloudFormation 的“事件”选项卡中看到的唯一问题:无法创建以下资源:[MyInstanceProfile,sampleEnvironment]。用户请求回滚。 这不是一个完整的错误。向下滚动事件日志,那里应该详细说明原因。 我已将问题更新为“所有事件”。 【参考方案1】:

最后,我能够解决它。由于错误很模糊,我在官方页面上使用了另一个示例 CloudFormation 脚本,该脚本使用 Elastic Beanstalk 部署 php 应用程序。 这个脚本有一个区别:

 
    "Namespace": "aws:autoscaling:launchconfiguration",
    "OptionName": "IamInstanceProfile",
    "Value": 
        "Ref": "MyInstanceProfile"
    

希望它可以帮助其他人。 完整脚本:


    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": 
        "sampleApplication": 
            "Type": "AWS::ElasticBeanstalk::Application",
            "Properties": 
                "Description": "AWS Elastic Beanstalk Sample Application"
            
        ,
        "sampleApplicationVersion": 
            "Type": "AWS::ElasticBeanstalk::ApplicationVersion",
            "Properties": 
                "ApplicationName": 
                    "Ref": "sampleApplication"
                ,
                "Description": "AWS ElasticBeanstalk Sample Application Version",
                "SourceBundle": 
                    "S3Bucket": 
                        "Fn::Sub": "elasticbeanstalk-samples-$AWS::Region"
                    ,
                    "S3Key": "helloworld-0.0.1-SNAPSHOT.jar"
                
            
        ,
        "sampleConfigurationTemplate": 
            "Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
            "Properties": 
                "ApplicationName": 
                    "Ref": "sampleApplication"
                ,
                "Description": "AWS ElasticBeanstalk Sample Configuration Template",
                "OptionSettings": [
                    
                        "Namespace": "aws:autoscaling:asg",
                        "OptionName": "MinSize",
                        "Value": "2"
                    ,
                    
                        "Namespace": "aws:autoscaling:asg",
                        "OptionName": "MaxSize",
                        "Value": "6"
                    ,
                    
                        "Namespace": "aws:elasticbeanstalk:environment",
                        "OptionName": "EnvironmentType",
                        "Value": "LoadBalanced"
                    ,
                    
                        "Namespace": "aws:autoscaling:launchconfiguration",
                        "OptionName": "IamInstanceProfile",
                        "Value": 
                            "Ref": "MyInstanceProfile"
                        
                    
                ],
                "SolutionStackName": "64bit Amazon Linux 2 v3.2.5 running Corretto 11"
            
        ,
        "sampleEnvironment": 
            "Type": "AWS::ElasticBeanstalk::Environment",
            "Properties": 
                "ApplicationName": 
                    "Ref": "sampleApplication"
                ,
                "Description": "AWS ElasticBeanstalk Sample Environment",
                "TemplateName": 
                    "Ref": "sampleConfigurationTemplate"
                ,
                "VersionLabel": 
                    "Ref": "sampleApplicationVersion"
                
            
        ,
        "MyInstanceRole": 
            "Type": "AWS::IAM::Role",
            "Properties": 
                "AssumeRolePolicyDocument": 
                    "Version": "2012-10-17",
                    "Statement": [
                        
                            "Effect": "Allow",
                            "Principal": 
                                "Service": [
                                    "ec2.amazonaws.com"
                                ]
                            ,
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        
                    ]
                ,
                "Description": "Beanstalk EC2 role",
                "ManagedPolicyArns": [
                    "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier",
                    "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker",
                    "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier"
                ]
            
        ,
        "MyInstanceProfile": 
            "Type": "AWS::IAM::InstanceProfile",
            "Properties": 
                "Roles": [
                    
                        "Ref": "MyInstanceRole"
                    
                ]
            
        
    

【讨论】:

以上是关于AWS CloudFormation + Elastic Beanstalk 错误的主要内容,如果未能解决你的问题,请参考以下文章

将现有 AWS 资源整合到 CloudFormation 堆栈中

AWS Cloudformation的相关概念

CloudFormation - 将标签应用于其他 AWS 资源

AWS CloudFormation:Cognito LambdaTrigger CustomEmailSender - 属性“AWS CloudFormation 目前不支持。”和 CDK 的使用

AWS学习笔记--利用CloudFormation管理AWS资源

AWS — AWS CloudFormation