如何使用 AWS CDK TYPESCRIPT 引用现有 VPC 部署 Beanstalk 应用程序
How to refer exsisting VPC to deploy Beanstalk app using AWS CDK TYPESCRIPT
正在使用 AWS CDK TYPESCRIPT 部署 Beanstalk 应用程序。我能够使用 AWS CDK Typescript 设置 beanstalk 应用程序,但找不到在现有 VPC 中以高可用性设置它的方法。
这是我的 ebstack.ts
#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import elasticbeanstalk = require('@aws-cdk/aws-elasticbeanstalk');
export class ElbtestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
//objects for access parameters
const node = this.node;
const appName = 'DEVELOPMENT';
const platform = node.tryGetContext("platform");
const app = new elasticbeanstalk.CfnApplication(this, 'Application', {
applicationName: appName
});
const optionSettingProperties: elasticbeanstalk.CfnEnvironment.OptionSettingProperty[] = [
{
namespace: 'aws:autoscaling:launchconfiguration',
optionName: 'InstanceType',
value: 't3.small',
},
{
namespace: 'aws:autoscaling:launchconfiguration',
optionName: 'IamInstanceProfile',
// Here you could reference an instance profile by ARN (e.g. myIamInstanceProfile.attrArn)
// For the default setup, leave this as is (it is assumed this role exists)
//
value: 'aws-elasticbeanstalk-ec2-role',
},
{
namespace: 'aws:elasticbeanstalk:container:nodejs',
optionName: 'NodeVersion',
value: '10.16.3',
}
];
new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
environmentName: 'x-SERVICE',
applicationName: app.applicationName || appName,
solutionStackName: '64bit Amazon Linux 2018.03 v4.13.1 running Node.js',
optionSettings: optionSettingProperties,
});
}
}
我正在关注 AWS 提供的示例 https://github.com/aws-samples/aws-cdk-examples
如何设置子网以高可用性部署此 beantalk 应用程序?
有关命名空间和选项的完整列表:
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-specific.html
这只是一个例子。
import cdk = require('@aws-cdk/core');
import {IVpc, Peer, Port, SecurityGroup,} from "@aws-cdk/aws-ec2";
import {CfnApplication, CfnEnvironment} from '@aws-cdk/aws-elasticbeanstalk';
interface ApplicationStackProps extends cdk.StackProps {
vpc: IVpc;
}
export class ApplicationStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: ApplicationStackProps) {
super(scope, id, props);
const prj: string = this.node.tryGetContext("prj");
const stage: string = this.node.tryGetContext("stage");
const platform: string = this.node.tryGetContext("platform");
const albSecurityGroup = new SecurityGroup(this, 'albSecurityGroup', {
allowAllOutbound: true,
securityGroupName: 'alb-sg',
vpc: props.vpc,
});
albSecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(80));
albSecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(443));
const app = new CfnApplication(this, 'Application', {
applicationName: `${prj}-${stage}-application`
});
const optionSettings: CfnEnvironment.OptionSettingProperty[] = [
{
namespace: 'aws:ec2:vpc',
optionName: 'VPCId',
value: props.vpc.vpcId,
},
{
namespace: 'aws:ec2:vpc',
optionName: 'ELBSubnets',
value: props.vpc.publicSubnets.map(value => value.subnetId).join(','),
}
,
{
namespace: 'aws:ec2:vpc',
optionName: 'Subnets',
value: props.vpc.privateSubnets.map(value => value.subnetId).join(','),
}
];
const env = new CfnEnvironment(this, 'Environment', {
environmentName: 'Environment',
applicationName: app.applicationName || `${prj}-${stage}-application`,
platformArn: platform,
optionSettings: optionSettings,
});
}
}
正在使用 AWS CDK TYPESCRIPT 部署 Beanstalk 应用程序。我能够使用 AWS CDK Typescript 设置 beanstalk 应用程序,但找不到在现有 VPC 中以高可用性设置它的方法。 这是我的 ebstack.ts
#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import elasticbeanstalk = require('@aws-cdk/aws-elasticbeanstalk');
export class ElbtestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
//objects for access parameters
const node = this.node;
const appName = 'DEVELOPMENT';
const platform = node.tryGetContext("platform");
const app = new elasticbeanstalk.CfnApplication(this, 'Application', {
applicationName: appName
});
const optionSettingProperties: elasticbeanstalk.CfnEnvironment.OptionSettingProperty[] = [
{
namespace: 'aws:autoscaling:launchconfiguration',
optionName: 'InstanceType',
value: 't3.small',
},
{
namespace: 'aws:autoscaling:launchconfiguration',
optionName: 'IamInstanceProfile',
// Here you could reference an instance profile by ARN (e.g. myIamInstanceProfile.attrArn)
// For the default setup, leave this as is (it is assumed this role exists)
//
value: 'aws-elasticbeanstalk-ec2-role',
},
{
namespace: 'aws:elasticbeanstalk:container:nodejs',
optionName: 'NodeVersion',
value: '10.16.3',
}
];
new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
environmentName: 'x-SERVICE',
applicationName: app.applicationName || appName,
solutionStackName: '64bit Amazon Linux 2018.03 v4.13.1 running Node.js',
optionSettings: optionSettingProperties,
});
}
}
我正在关注 AWS 提供的示例 https://github.com/aws-samples/aws-cdk-examples 如何设置子网以高可用性部署此 beantalk 应用程序?
有关命名空间和选项的完整列表:
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-specific.html
这只是一个例子。
import cdk = require('@aws-cdk/core');
import {IVpc, Peer, Port, SecurityGroup,} from "@aws-cdk/aws-ec2";
import {CfnApplication, CfnEnvironment} from '@aws-cdk/aws-elasticbeanstalk';
interface ApplicationStackProps extends cdk.StackProps {
vpc: IVpc;
}
export class ApplicationStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: ApplicationStackProps) {
super(scope, id, props);
const prj: string = this.node.tryGetContext("prj");
const stage: string = this.node.tryGetContext("stage");
const platform: string = this.node.tryGetContext("platform");
const albSecurityGroup = new SecurityGroup(this, 'albSecurityGroup', {
allowAllOutbound: true,
securityGroupName: 'alb-sg',
vpc: props.vpc,
});
albSecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(80));
albSecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(443));
const app = new CfnApplication(this, 'Application', {
applicationName: `${prj}-${stage}-application`
});
const optionSettings: CfnEnvironment.OptionSettingProperty[] = [
{
namespace: 'aws:ec2:vpc',
optionName: 'VPCId',
value: props.vpc.vpcId,
},
{
namespace: 'aws:ec2:vpc',
optionName: 'ELBSubnets',
value: props.vpc.publicSubnets.map(value => value.subnetId).join(','),
}
,
{
namespace: 'aws:ec2:vpc',
optionName: 'Subnets',
value: props.vpc.privateSubnets.map(value => value.subnetId).join(','),
}
];
const env = new CfnEnvironment(this, 'Environment', {
environmentName: 'Environment',
applicationName: app.applicationName || `${prj}-${stage}-application`,
platformArn: platform,
optionSettings: optionSettings,
});
}
}