使用CF模板更新AWS堆栈

时间:2020-06-09 10:46:39

标签: ruby amazon-web-services amazon-cloudformation

希望每个人都保持安全!

我在AWS Beanstalk上托管了Ruby on Rails应用程序。我正在使用CloudFormation模板来更新任何堆栈,例如Ruby版本,Linux平台升级等。

我正尝试升级,将 Linux 框升级到2.11.7,将 Ruby 升级到2.6.6,然后将 ElasticSearch 升级到7.4

我在CloudFormation YML模板中进行了这些更改,然后运行了aws cloudformation update-stack命令以应用这些更改。

虽然更改花费了时间,但我意外地从Web AWS控制台单击了 Rebuild Environment ,所有先前配置的设置(如 SQS Load Balancer) 等,已被新设置取代。

现在,每当我尝试执行update-stack命令时,它都会失败,并出现以下错误:

2020-06-09 15:25:44 UTC+0530
WARN
Environment health has transitioned from Info to Degraded. Command failed on all instances. 
Incorrect application version found on all instances. Expected version "code-pipeline-1591695975198-d485a710a5a5a16e7a099e4e16303ef53c188d54" (deployment 2377). Application update failed 40 seconds ago and took 79 seconds.

2020-06-09 15:25:03 UTC+0530    
INFO
The environment was reverted to the previous configuration setting.

2020-06-09 15:24:44 UTC+0530    
INFO
Environment health has transitioned from Ok to Info. Application update in progress on 1 instance. 0 out of 1 instance completed (running for 39 seconds).

2020-06-09 15:24:30 UTC+0530    
ERROR
During an aborted deployment, some instances may have deployed the new application version. 
To ensure all instances are running the same version, re-deploy the appropriate application version.

2020-06-09 15:24:30 UTC+0530    
ERROR
Failed to deploy application.

2020-06-09 15:24:30 UTC+0530    
ERROR
Unsuccessful command execution on instance id(s) 'i-4d527ff00cb443z11'. Aborting the operation.

2020-06-09 15:24:30 UTC+0530    
INFO
Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].

2020-06-09 15:24:30 UTC+0530    
ERROR
[Instance: i-4d527ff00cb443z11] Command failed on instance. Return code: 18 Output: (TRUNCATED)...g: the running version of Bundler (1.16.0) is older than the version that created the lockfile (1.17.3). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`. Your Ruby version is 2.6.6, but your Gemfile specified 2.6.5. Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

2020-06-09 15:24:19 UTC+0530    
INFO
Deploying new version to instance(s).

2020-06-09 15:23:45 UTC+0530    
INFO
Updating environment developWeb's configuration settings.

2020-06-09 15:23:36 UTC+0530    
INFO
Environment update is starting.

我可以确认已设置Ruby-2.6.6。我不确定它是从哪里获取旧版本的Ruby的?

有什么办法可以解决这个问题?还是强行应用模板更改?

对此将提供任何帮助。

[更新]:当我尝试从Rails控制台连接到ElasticSearch时,我得到:

Faraday::ConnectionFailed: Failed to open TCP connection to old-elasticsearch-host-name.es.amazonaws.com:80 (Hostname not known: old-elasticsearch-host-name.es.amazonaws.com)
from /opt/rubies/ruby-2.6.6/lib/ruby/2.6.0/net/http.rb:949:in `rescue in block in connect'
Caused by SocketError: Failed to open TCP connection to old-elasticsearch-host-name.es.amazonaws.com:80 (Hostname not known: old-elasticsearch-host-name.es.amazonaws.com)
from /opt/rubies/ruby-2.6.6/lib/ruby/2.6.0/net/http.rb:949:in `rescue in block in connect'
Caused by Resolv::ResolvError: no address for old-elasticsearch-host-name.es.amazonaws.com
from /opt/rubies/ruby-2.6.6/lib/ruby/2.6.0/resolv.rb:94:in `getaddress'

elasticsearch实例的新URL不同,但是它仍然从ELASTICSEARCH_HOST ENV变量中提取旧URL。

我的CF模板中的信息:

AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template
Conditions:
  ProductionCondition: !Equals [!Ref 'AWS::StackName', production]
  NotProductionCondition: !Not [!Equals [!Ref 'AWS::StackName', production]]
Parameters:
  ElasticSearchInstanceClass:
    Description: ElasticSearch instance class
    Type: String
    Default: t2.small.elasticsearch
Resources:
  DeploymentBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub '${AWS::StackName}-deployment'
  DeploymentRepository:
    Type: AWS::CodeCommit::Repository
    Properties:
      RepositoryDescription: Deployment Repository
      RepositoryName: !Sub '${AWS::StackName}${BaseName}Deployment'
  ElasticsearchDomain:
    Type: AWS::Elasticsearch::Domain
    Properties:
      ElasticsearchVersion: 7.4
      ElasticsearchClusterConfig:
        DedicatedMasterEnabled: true
        InstanceCount: 2
        ZoneAwarenessEnabled: false
        InstanceType: !Ref ElasticSearchInstanceClass
        DedicatedMasterType: !Ref ElasticSearchInstanceClass
        DedicatedMasterCount: 3
      EBSOptions:
        EBSEnabled: true
        VolumeType: gp2
        VolumeSize: 30
      SnapshotOptions:
        AutomatedSnapshotStartHour: 7
      AccessPolicies:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
               AWS: '*'
            Action: 'es:*'
            Resource: '*'
      VPCOptions:
        SubnetIds:
          - subnet-f4ad24de
        SecurityGroupIds:
          - sg-*
      AdvancedOptions:
        rest.action.multi.allow_explicit_index: true
  RailsApplication:
    Type: AWS::ElasticBeanstalk::Application
    DependsOn: PostgresDatabase
    Properties:
      ApplicationName: !Sub '${AWS::StackName}'
      Description: !Sub 'Application (${AWS::StackName})'
  RailsApplicationVersion:
    Type: AWS::ElasticBeanstalk::ApplicationVersion
    DependsOn: RailsApplication
    Properties:
      ApplicationName: !Ref RailsApplication
      Description: !Sub 'Application Version (${AWS::StackName})'
      SourceBundle:
        S3Bucket: !Ref DeploymentBucket
        S3Key: code.zip
  RailsApplicationTemplate:
    Type: AWS::ElasticBeanstalk::ConfigurationTemplate
    DependsOn:
      - RailsApplicationVersion
      - ElasticsearchDomain
    Properties:
      ApplicationName: !Ref RailsApplication
      SolutionStackName: 64bit Amazon Linux 2018.03 v2.11.7 running Ruby 2.6 (Puma)
      Description: !Sub 'Configuration Template (${AWS::StackName})'
      OptionSettings:
      - Namespace: aws:elb:loadbalancer
        OptionName: LoadBalancerHTTPSPort
        Value: 443
      - Namespace: aws:elb:loadbalancer
        OptionName: SSLCertificateId
        Value: !ImportValue SSLCertificateArn
      - Namespace: aws:elb:policies
        OptionName: ConnectionDrainingEnabled
        Value: true
      - Namespace: aws:autoscaling:launchconfiguration
        OptionName: IamInstanceProfile
        Value: !ImportValue InstanceProfileIdentifier
      - Namespace: aws:elasticbeanstalk:application:environment
        OptionName: ELASTICSEARCH_HOST
        Value: !GetAtt ElasticsearchDomain.DomainEndpoint
      - Namespace: aws:elasticbeanstalk:application:environment
        OptionName: ELASTICSEARCH_PORT
        Value: 80
  RailsWebEnvironment:
    Type: AWS::ElasticBeanstalk::Environment
    DependsOn:
      - RailsApplicationTemplate
    Properties:
      EnvironmentName: !Sub '${AWS::StackName}Web'
      ApplicationName: !Ref RailsApplication
      Description: !Sub 'Web Environment (${AWS::StackName})'
      TemplateName: !Ref RailsApplicationTemplate
      VersionLabel: !Ref RailsApplicationVersion
      Tier:
        Name: WebServer
        Type: Standard
  RailsWorkerEnvironment:
    Type: AWS::ElasticBeanstalk::Environment
    DependsOn:
      - RailsApplicationTemplate
    Properties:
      EnvironmentName: !Sub '${AWS::StackName}Worker'
      ApplicationName: !Ref RailsApplication
      Description: !Sub 'Worker Environment (${AWS::StackName})'
      - Namespace: aws:elasticbeanstalk:application:environment
        OptionName: AWS_ENV
        Value: worker
      - Namespace: aws:elasticbeanstalk:application:environment
        OptionName: PROCESS_ACTIVE_ELASTIC_JOBS
        Value: true
      TemplateName: !Ref RailsApplicationTemplate
      VersionLabel: !Ref RailsApplicationVersion
      Tier:
        Name: Worker
        Type: SQS/HTTP
  ApplicationCodePipeline:
    Type: AWS::CodePipeline::Pipeline
    DependsOn:
      - RailsApplication
      - RailsWebEnvironment
      - RailsWorkerEnvironment
    Properties:
      RoleArn: !ImportValue ApplicationRoleArn
      Stages:
        -
          Name: Source
          Actions:
            -
              Name: SourceAction
              ActionTypeId:
                Category: Source
                Owner: AWS
                Version: 1
                Provider: CodeCommit
              OutputArtifacts:
                - Name: SourceOutput
              Configuration:
                BranchName: !Ref RepositoryBranch
                RepositoryName: !GetAtt DeploymentRepository.Name
              RunOrder: 1
        -
          Name: Release
          Actions:
            -
              Name: WebReleaseAction
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Version: 1
                Provider: ElasticBeanstalk
              InputArtifacts:
                - Name: SourceOutput
              OutputArtifacts: []
              Configuration:
                ApplicationName: !Ref RailsApplication
                EnvironmentName: !Ref RailsWebEnvironment
              RunOrder: 1
            -
              Name: WorkerReleaseAction
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Version: 1
                Provider: ElasticBeanstalk
              InputArtifacts:
                - Name: SourceOutput
              OutputArtifacts: []
              Configuration:
                ApplicationName: !Ref RailsApplication
                EnvironmentName: !Ref RailsWorkerEnvironment
              RunOrder: 1
      ArtifactStore:
        Type: S3
        Location: !Ref DeploymentBucket
  AppliationDomain:
    Type: AWS::Route53::RecordSet
    Condition: NotProductionCondition
    Properties:
      HostedZoneId: !ImportValue HostedZoneIdentifier
      Comment: Subdomain for this application
      Name: !Sub '${AWS::StackName}.host-name.com'
      Type: CNAME
      TTL: 60
      ResourceRecords:
        - !Sub '${AWS::StackName}.host-name.com.cdn.cloudflare.net'

1 个答案:

答案 0 :(得分:0)

这是一个配置问题。

每当我运行aws update-stack命令时,它都会转到s3并提取(源代码的)邮政编码,在该邮政编码的ruby版本的Gemfile中将其设置为2.6.5。

所以,我上传了源代码的新副本,然后执行了update-stack命令,它起作用了