使用 aws cdk 创建 elasticbeanstalk 实例

Using aws cdk to create elasticbeanstalk instance

我正在使用以下代码创建一个 elasticbeanstalk 实例,但是,当我尝试部署时,它抛出 No application with name pythonService is found

import cdk = require('@aws-cdk/core');
import ebs = require('@aws-cdk/aws-elasticbeanstalk');

export class DevOpsStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const webAppName: string = 'pythonService';

    const webApp = new ebs.CfnApplication(this, webAppName);

    const pythonServiceS3BucketName : string = 'my-bucket-name'
    const pythonServiceArchiveName : string = 'my-awesome-app.zip'
    const webAppVersion = new ebs.CfnApplicationVersion(this, 'pythonServiceVer', {
      applicationName: webAppName,
      sourceBundle: {
        s3Bucket: pythonServiceS3BucketName,
        s3Key: pythonServiceArchiveName
      }
    });

    const options: ebs.CfnEnvironment.OptionSettingProperty[] = [{
      namespace: 'aws:ec2:vpc',
      optionName: 'VPCId',
      value: 'a-vpc-id'
    }, {
      namespace: 'aws:ec2:vpc',
      optionName: 'Subnets',
      value: 'a-subnet-id'
    }];

    const webAppEnv = new ebs.CfnEnvironment(this, 'pythonServiceEnv', {
      applicationName: webAppName,
      solutionStackName: '64bit Amazon Linux 2018.03 v2.9.2 running Python 3.6',
      optionSettings: options
    });
  }
}

我相信这是因为它试图在创建 CfnApplication 之前部署 CfnApplicationVersion

DevOpsStack: creating CloudFormation changeset...
 0/5 | 8:33:48 PM | CREATE_IN_PROGRESS   | AWS::ElasticBeanstalk::Application        | pythonService
 0/5 | 8:33:48 PM | CREATE_IN_PROGRESS   | AWS::CDK::Metadata                        | CDKMetadata
 0/5 | 8:33:48 PM | CREATE_IN_PROGRESS   | AWS::ElasticBeanstalk::Environment        | pythonServiceEnv
 0/5 | 8:33:48 PM | CREATE_IN_PROGRESS   | AWS::ElasticBeanstalk::ApplicationVersion | pythonServiceVer
 0/5 | 8:33:48 PM | CREATE_IN_PROGRESS   | AWS::ElasticBeanstalk::Application        | pythonService Resource creation Initiated
 1/5 | 8:33:48 PM | CREATE_FAILED        | AWS::ElasticBeanstalk::ApplicationVersion | pythonServiceVer No Application named 'pythonService' found. (Service: AWSElasticBeanstalk; Status Code: 400; Error Code: InvalidParameterValue; Request ID: cd1a9f2b-a3fc-450a-9c34-fec13132f613)

此外,它生成的 cloudformation 模板在应用程序之前没有 !Ref

Resources:
  pythonService:
    Type: AWS::ElasticBeanstalk::Application
    Metadata:
      aws:cdk:path: DevOpsStack/pythonService
  pythonServiceVer:
    Type: AWS::ElasticBeanstalk::ApplicationVersion
    Properties:
      **ApplicationName: pythonService**
      SourceBundle:
        S3Bucket: ...
        S3Key: ...

您必须手动构建依赖树,因为 CfnApplication 和 CfnApplicationVersion 之间没有引用。所以将 CfnApplication 添加为依赖项以确保它在 CfnApplicationVersion:

之前部署

webAppVersion.addDependsOn(webApp);

我遇到了类似的问题,发现 addDependsOn 建议很有效。

但是,仅添加此修复程序将部署示例应用程序,因为您的环境未引用特定版本。除了 dependsOn 修复之外,我相信您还需要添加 </code>versionLabel<code>to yourwebAppEnv`:

    const webAppEnv = new ebs.CfnEnvironment(this, 'pythonServiceEnv', {
      applicationName: webAppName,
      solutionStackName: '64bit Amazon Linux 2018.03 v2.9.2 running Python 3.6',
      optionSettings: options,
      versionLabel webAppVersion.ref
    });