Untitled

 avatar
unknown
plain_text
4 years ago
3.4 kB
8
Indexable
const { CfnEnvironment } = require('@aws-cdk/aws-elasticbeanstalk')
const { CfnRecordSet } = require('@aws-cdk/aws-route53')
const { EbsDeviceVolumeType, SecurityGroup, Peer, Port } = require('@aws-cdk/aws-ec2')
const _ = require('lodash')

const createBeanstalkEnvironment = (
	scope,
	id,
	{
		applicationName,
		environmentName,
		templateName = 'edpuzzle-configuration-template',
		description,
		vpc,
		maxInstances,
		domain,
		hostedZone,
		region,
	}
) => {
	const environmentSecurityGroup = new SecurityGroup(
		scope,
		`edpuzzleEnvironment${environmentName}SecurityGroup`,
		{
			vpc,
			allowAllOutbound: true,
			description: `Security Group for ${environmentName}`,
			securityGroupName: `${environmentName}-security-group`,
		}
	)

	environmentSecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(80))

	const privateSubnetsString = _.join(
		_.map(vpc.privateSubnets, (privateSubnet) => privateSubnet.subnetId),
		','
	)
	const publicSubnetsString = _.join(
		_.map(vpc.publicSubnets, (publicSubnet) => publicSubnet.subnetId),
		','
	)

	// OPTIONS

	const options = [
		{
			namespace: 'aws:ec2:vpc',
			optionName: 'ELBSubnets',
			// Public vpc subnets
			value: publicSubnetsString,
		},
		{
			resourceName: 'AWSEBAutoScalingGroup',
			namespace: 'aws:ec2:vpc',
			optionName: 'Subnets',
			// Private vpc subnets
			value: privateSubnetsString,
		},
		{
			resourceName: 'AWSEBLoadBalancerSecurityGroup',
			namespace: 'aws:ec2:vpc',
			optionName: 'VPCId',
			value: vpc.vpcId,
		},
		{
			resourceName: 'AWSEBAutoScalingLaunchConfiguration',
			namespace: 'aws:autoscaling:launchconfiguration',
			optionName: 'SecurityGroups',
			value: `${environmentSecurityGroup.securityGroupId},${vpc.vpcDefaultSecurityGroup}`,
		},
	]

	if (maxInstances)
		options.push({
			resourceName: 'AWSEBAutoScalingGroup',
			namespace: 'aws:autoscaling:asg',
			optionName: 'MaxSize',
			value: maxInstances.toString(),
		})

	// END OPTIONS

	const environment = new CfnEnvironment(scope, id, {
		applicationName,
		environmentName,
		templateName,
		description,
		optionSettings: options,
		tags: [
			{
				key: 'application',
				value: 'edpuzzle',
			},
		],
	})

	if (domain) {
		// Not finding proper way to create the domain
		// const HOSTED_ZONE_ID_PER_REGION = {
		// 	'us-east-2': 'Z14LCN19Q5QHIC',
		// 	'us-east-1': 'Z117KPS5GTRQ2G',
		// 	'us-east-2': 'Z14LCN19Q5QHIC',
		// 	'us-east-1': 'Z117KPS5GTRQ2G',
		// 	'us-west-1': 'Z1LQECGX5PH1X',
		// 	'us-west-2': 'Z38NKT9BP95V3O',
		// 	'ca-central-1': 'ZJFCZL7SSZB5I',
		// 	'ap-south-1': 'Z18NTBI3Y7N9TZ',
		// 	'ap-northeast-2': 'Z3JE5OI70TWKCP',
		// 	'ap-northeast-3': 'ZNE5GEY1TIAGY',
		// 	'ap-southeast-1': 'Z16FZ9L249IFLT',
		// 	'ap-southeast-2': 'Z2PCDNR3VC2G1N',
		// 	'ap-northeast-1': 'Z1R25G3KIG2GBW',
		// 	'eu-central-1': 'Z1FRNW7UH4DEZJ',
		// 	'eu-west-1': 'Z2NYPWQ7DFZAZH',
		// 	'eu-west-2': 'Z1GKAAAUGATPF1',
		// 	'eu-west-3': 'Z5WN6GAYWG5OB',
		// 	'sa-east-1': 'Z10X7K2B4QSOFV',
		// }
		// const record = new CfnRecordSet(scope, `${environmentName}Record`, {
		// 	name: `${domain}.`,
		// 	type: 'A',
		// 	aliasTarget: {
		// 		// Hosted Zone of the elastic beanstalks records
		// 		hostedZoneId: HOSTED_ZONE_ID_PER_REGION[region],
		// 		dnsName: environment.attrEndpointUrl,
		// 		evaluateTargetHealth: false,
		// 	},
		// 	hostedZoneId: hostedZone.hostedZoneId,
		// })
	}

	return environment
}
module.exports = { createBeanstalkEnvironment }
Editor is loading...