From bcd38c64371d8aa4d86213c6d996921102dacfc6 Mon Sep 17 00:00:00 2001 From: Tanner Lewis Date: Thu, 7 Nov 2024 12:52:11 -0500 Subject: [PATCH] Use CFN input for VPC id and Stage (#1122) Take CFN input from the bootstrap stack, and fill in appropriate CDK context if available --------- Signed-off-by: Tanner Lewis --- .../initBootstrap.sh | 8 ++++++ .../lib/solutions-stack.ts | 25 ++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/deployment/migration-assistant-solution/initBootstrap.sh b/deployment/migration-assistant-solution/initBootstrap.sh index 5355bca93..0b5cf6993 100644 --- a/deployment/migration-assistant-solution/initBootstrap.sh +++ b/deployment/migration-assistant-solution/initBootstrap.sh @@ -50,6 +50,14 @@ else fi cd deployment/cdk/opensearch-service-migration || exit + +if [[ -n "$VPC_ID" ]]; then + sed -i "s||$VPC_ID|g" /opensearch-migrations/deployment/cdk/opensearch-service-migration/cdk.context.json +fi +if [[ -n "$STAGE" ]]; then + sed -i "s||$STAGE|g" /opensearch-migrations/deployment/cdk/opensearch-service-migration/cdk.context.json +fi + npm install -g aws-cdk 2>&1 npm install 2>&1 ./buildDockerImages.sh \ No newline at end of file diff --git a/deployment/migration-assistant-solution/lib/solutions-stack.ts b/deployment/migration-assistant-solution/lib/solutions-stack.ts index c727a562a..5ef3ed83d 100644 --- a/deployment/migration-assistant-solution/lib/solutions-stack.ts +++ b/deployment/migration-assistant-solution/lib/solutions-stack.ts @@ -89,6 +89,12 @@ function importVPC(stack: Stack, vpdIdParameter: CfnParameter, availabilityZones }); } +function generateExportString(exports: Record): string { + return Object.entries(exports) + .map(([key, value]) => `export ${key}=${value}`) + .join("; "); +} + export class SolutionsInfrastructureStack extends Stack { constructor(scope: Construct, id: string, props: SolutionsInfrastructureStackProps) { @@ -144,12 +150,6 @@ export class SolutionsInfrastructureStack extends Stack { }) const solutionsUserAgent = `AwsSolution/${props.solutionId}/${props.solutionVersion}` - const cfnInitConfig: InitElement[] = [ - InitCommand.shellCommand(`echo "export MIGRATIONS_APP_REGISTRY_ARN=${appRegistryAppARN}; export MIGRATIONS_USER_AGENT=${solutionsUserAgent}" > /etc/profile.d/solutionsEnv.sh`), - InitFile.fromFileInline("/opensearch-migrations/initBootstrap.sh", './initBootstrap.sh', { - mode: "000744" - }), - ] const bootstrapRole = new Role(this, 'BootstrapRole', { assumedBy: new ServicePrincipal('ec2.amazonaws.com'), @@ -188,6 +188,19 @@ export class SolutionsInfrastructureStack extends Stack { vpc = importVPC(this, vpcIdParameter, availabilityZonesParameter, privateSubnetIdsParameter); } + const exportString = generateExportString({ + "MIGRATIONS_APP_REGISTRY_ARN": appRegistryAppARN, + "MIGRATIONS_USER_AGENT": solutionsUserAgent, + "VPC_ID": vpc.vpcId, + "STAGE": stageParameter.valueAsString, + }) + const cfnInitConfig: InitElement[] = [ + InitCommand.shellCommand(`echo "${exportString}" > /etc/profile.d/solutionsEnv.sh`), + InitFile.fromFileInline("/opensearch-migrations/initBootstrap.sh", './initBootstrap.sh', { + mode: "000744" + }), + ] + new Instance(this, 'BootstrapEC2Instance', { vpc: vpc, vpcSubnets: {