async function getCloudformationStackNameForAsg()

in cdk/event-forwarder/index.ts [56:90]


async function getCloudformationStackNameForAsg(
	event: AutoscalingEvent,
	stage: string,
): Promise<string> {
	const awsConfig: AwsClientConfig = {
		region: 'eu-west-1',
		...(stage === 'DEV' && {
			credentials: fromIni({ profile: 'developerPlayground' }),
		}),
	};

	const { AutoScalingGroupName } = event.detail;

	const client = new AutoScalingClient(awsConfig);
	const command = new DescribeAutoScalingGroupsCommand({
		AutoScalingGroupNames: [AutoScalingGroupName],
	});
	const { AutoScalingGroups = [] } = await client.send(command);

	if (AutoScalingGroups.length !== 1) {
		throw new Error('Unable to locate unique autoscaling group');
	}

	const [cfnStackName] = AutoScalingGroups.flatMap((_) => _.Tags ?? [])
		.filter((_) => _.Key === 'aws:cloudformation:stack-name')
		.map((_) => _.Value);

	if (!cfnStackName) {
		throw new Error(
			'Unable to locate unique tag: aws:cloudformation:stack-name',
		);
	}

	return cfnStackName;
}