Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.role = props.role || new iam.Role(this, 'InstanceRole', {
roleName: PhysicalName.GENERATE_IF_NEEDED,
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
});
this.grantPrincipal = this.role;
const iamProfile = new iam.CfnInstanceProfile(this, 'InstanceProfile', {
roles: [ this.role.roleName ]
});
// use delayed evaluation
const imageConfig = props.machineImage.getImage(this);
this.userData = props.userData || imageConfig.userData || ec2.UserData.forOperatingSystem(imageConfig.osType);
const userDataToken = Lazy.stringValue({ produce: () => Fn.base64(this.userData.render()) });
const securityGroupsToken = Lazy.listValue({ produce: () => this.securityGroups.map(sg => sg.securityGroupId) });
const launchConfig = new CfnLaunchConfiguration(this, 'LaunchConfig', {
imageId: imageConfig.imageId,
keyName: props.keyName,
instanceType: props.instanceType.toString(),
securityGroups: securityGroupsToken,
iamInstanceProfile: iamProfile.ref,
userData: userDataToken,
associatePublicIpAddress: props.associatePublicIpAddress,
spotPrice: props.spotPrice,
blockDeviceMappings: (props.blockDevices !== undefined ? props.blockDevices.map(
({deviceName, volume, mappingEnabled}) => {
const {virtualName, ebsDevice: ebs} = volume;
if (ebs) {
const {iops, volumeType} = ebs;
alarmDescription: props.alarmDescription,
alarmName: this.physicalName,
// Evaluation
comparisonOperator,
threshold: props.threshold,
datapointsToAlarm: props.datapointsToAlarm,
evaluateLowSampleCountPercentile: props.evaluateLowSampleCountPercentile,
evaluationPeriods: props.evaluationPeriods,
treatMissingData: props.treatMissingData,
// Actions
actionsEnabled: props.actionsEnabled,
alarmActions: Lazy.listValue({ produce: () => this.alarmActionArns }),
insufficientDataActions: Lazy.listValue({ produce: (() => this.insufficientDataActionArns) }),
okActions: Lazy.listValue({ produce: () => this.okActionArns }),
// Metric
...dropUndef(config),
...dropUndef({
// Alarm overrides
period: props.period && props.period.toSeconds(),
statistic: renderIfSimpleStatistic(props.statistic),
extendedStatistic: renderIfExtendedStatistic(props.statistic),
})
});
this.alarmArn = this.getResourceArnAttribute(alarm.attrArn, {
service: 'cloudwatch',
resource: 'alarm',
resourceName: this.physicalName,
sep: ':',
Tag.add(this, NAME_TAG, props.instanceName || this.node.path);
this.role = props.role || new iam.Role(this, 'InstanceRole', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
});
this.grantPrincipal = this.role;
const iamProfile = new iam.CfnInstanceProfile(this, 'InstanceProfile', {
roles: [this.role.roleName]
});
// use delayed evaluation
const imageConfig = props.machineImage.getImage(this);
this.userData = props.userData || imageConfig.userData || UserData.forOperatingSystem(imageConfig.osType);
const userDataToken = Lazy.stringValue({ produce: () => Fn.base64(this.userData.render()) });
const securityGroupsToken = Lazy.listValue({ produce: () => this.securityGroups.map(sg => sg.securityGroupId) });
const { subnets } = props.vpc.selectSubnets(props.vpcSubnets);
let subnet;
if (props.availabilityZone) {
const selected = subnets.filter(sn => sn.availabilityZone === props.availabilityZone);
if (selected.length === 1) {
subnet = selected[0];
} else {
throw new Error('When specifying AZ there has to be exactly on subnet of the given type in this az');
}
} else {
subnet = subnets[0];
}
this.instance = new CfnInstance(this, 'Resource', {
imageId: imageConfig.imageId,
private renderVpcConfig(config: VpcConfig | undefined): {[key: string]: any} {
return (config) ? { VpcConfig: {
SecurityGroupIds: Lazy.listValue({ produce: () => (this.securityGroups.map(sg => (sg.securityGroupId))) }),
Subnets: this.subnets,
}} : {};
}
constructor(scope: Construct, id: string, props: ApplicationLoadBalancerProps) {
super(scope, id, props, {
type: "application",
securityGroups: Lazy.listValue({ produce: () => [this.securityGroup.securityGroupId] }),
ipAddressType: props.ipAddressType,
});
this.securityGroup = props.securityGroup || new ec2.SecurityGroup(this, 'SecurityGroup', {
vpc: props.vpc,
description: `Automatically created Security Group for ELB ${this.node.uniqueId}`,
allowAllOutbound: false
});
this.connections = new ec2.Connections({ securityGroups: [this.securityGroup] });
if (props.http2Enabled === false) { this.setAttribute('routing.http2.enabled', 'false'); }
if (props.idleTimeout !== undefined) { this.setAttribute('idle_timeout.timeout_seconds', props.idleTimeout.toSeconds().toString()); }
}
}
});
withResolved(desiredCapacity, maxCapacity, (desired, max) => {
if (max < desired) {
throw new Error(`Should have desiredCapacity (${desired}) <= maxCapacity (${max})`);
}
});
const { subnetIds, hasPublic } = props.vpc.selectSubnets(props.vpcSubnets);
const asgProps: CfnAutoScalingGroupProps = {
cooldown: props.cooldown !== undefined ? props.cooldown.toSeconds().toString() : undefined,
minSize: Tokenization.stringifyNumber(minCapacity),
maxSize: Tokenization.stringifyNumber(maxCapacity),
desiredCapacity: Tokenization.stringifyNumber(desiredCapacity),
launchConfigurationName: launchConfig.ref,
loadBalancerNames: Lazy.listValue({ produce: () => this.loadBalancerNames }, { omitEmpty: true }),
targetGroupArns: Lazy.listValue({ produce: () => this.targetGroupArns }, { omitEmpty: true }),
notificationConfigurations: !props.notificationsTopic ? undefined : [
{
topicArn: props.notificationsTopic.topicArn,
notificationTypes: [
"autoscaling:EC2_INSTANCE_LAUNCH",
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
"autoscaling:EC2_INSTANCE_TERMINATE",
"autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
],
}
],
vpcZoneIdentifier: subnetIds,
healthCheckType: props.healthCheck && props.healthCheck.type,
healthCheckGracePeriod: props.healthCheck && props.healthCheck.gracePeriod && props.healthCheck.gracePeriod.toSeconds(),
};
constructor(scope: Construct, id: string, props: VpcLinkProps = {}) {
super(scope, id, {
physicalName: props.vpcLinkName ||
Lazy.stringValue({ produce: () => this.node.uniqueId }),
});
const cfnResource = new CfnVpcLink(this, 'Resource', {
name: this.physicalName,
description: props.description,
targetArns: Lazy.listValue({ produce: () => this.renderTargets() })
});
this.vpcLinkId = cfnResource.ref;
if (props.targets) {
this.addTargets(...props.targets);
}
}
this.role.addToPolicy(statement);
}
const code = props.code.bind(this);
verifyCodeConfig(code, props.runtime);
const resource: CfnFunction = new CfnFunction(this, 'Resource', {
functionName: this.physicalName,
description: props.description,
code: {
s3Bucket: code.s3Location && code.s3Location.bucketName,
s3Key: code.s3Location && code.s3Location.objectKey,
s3ObjectVersion: code.s3Location && code.s3Location.objectVersion,
zipFile: code.inlineCode
},
layers: Lazy.listValue({ produce: () => this.layers.map(layer => layer.layerVersionArn) }, { omitEmpty: true }),
handler: props.handler,
timeout: props.timeout && props.timeout.toSeconds(),
runtime: props.runtime.name,
role: this.role.roleArn,
environment: Lazy.anyValue({ produce: () => this.renderEnvironment() }),
memorySize: props.memorySize,
vpcConfig: this.configureVpc(props),
deadLetterConfig: this.buildDeadLetterConfig(props),
tracingConfig: this.buildTracingConfig(props),
reservedConcurrentExecutions: props.reservedConcurrentExecutions
});
resource.node.addDependency(this.role);
this.functionName = this.getResourceNameAttribute(resource.ref);
this.functionArn = this.getResourceArnAttribute(resource.attrArn, {
});
withResolved(desiredCapacity, maxCapacity, (desired, max) => {
if (max < desired) {
throw new Error(`Should have desiredCapacity (${desired}) <= maxCapacity (${max})`);
}
});
const { subnetIds, hasPublic } = props.vpc.selectSubnets(props.vpcSubnets);
const asgProps: CfnAutoScalingGroupProps = {
cooldown: props.cooldown !== undefined ? props.cooldown.toSeconds().toString() : undefined,
minSize: Tokenization.stringifyNumber(minCapacity),
maxSize: Tokenization.stringifyNumber(maxCapacity),
desiredCapacity: Tokenization.stringifyNumber(desiredCapacity),
launchConfigurationName: launchConfig.ref,
loadBalancerNames: Lazy.listValue({ produce: () => this.loadBalancerNames }, { omitEmpty: true }),
targetGroupArns: Lazy.listValue({ produce: () => this.targetGroupArns }, { omitEmpty: true }),
notificationConfigurations: !props.notificationsTopic ? undefined : [
{
topicArn: props.notificationsTopic.topicArn,
notificationTypes: [
"autoscaling:EC2_INSTANCE_LAUNCH",
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
"autoscaling:EC2_INSTANCE_TERMINATE",
"autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
],
}
],
vpcZoneIdentifier: subnetIds,
healthCheckType: props.healthCheck && props.healthCheck.type,
healthCheckGracePeriod: props.healthCheck && props.healthCheck.gracePeriod && props.healthCheck.gracePeriod.toSeconds(),
};