From c3a980af1cdeba464fa5cd448abe3685144fe1e5 Mon Sep 17 00:00:00 2001 From: Imad <118115991+Imadnajam@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:25:04 +0100 Subject: [PATCH] Optimize Amplify App Deployment with Improved Environment Handling and Code Refactoring Environment Variable Management: Introduced a helper function, getEnvVar, to streamline environment variable handling, providing clear error messages when required values are missing. Branch Initialization: Simplified branch setup using .map() to dynamically create branches, reducing code repetition and improving scalability. Code Organization: Enhanced logical grouping of related sections, making the codebase easier to read and maintain. --- .../test/integ.app-custom-domain.ts | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts index 3a6338676a6de..34ed981ec92da 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts +++ b/packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts @@ -1,8 +1,8 @@ +import { App, Stack, StackProps } from 'aws-cdk-lib'; +import { Construct } from 'constructs'; import * as acm from 'aws-cdk-lib/aws-certificatemanager'; import * as codecommit from 'aws-cdk-lib/aws-codecommit'; import * as route53 from 'aws-cdk-lib/aws-route53'; -import { App, Stack, StackProps } from 'aws-cdk-lib'; -import { Construct } from 'constructs'; import * as amplify from '../lib'; import { IntegTest } from '@aws-cdk/integ-tests-alpha'; @@ -24,8 +24,7 @@ class TestStack extends Stack { sourceCodeProvider: new amplify.CodeCommitSourceCodeProvider({ repository }), }); - const prodBranch = app.addBranch('main'); - const devBranch = app.addBranch('dev'); + const [prodBranch, devBranch] = ['main', 'dev'].map(branchName => app.addBranch(branchName)); const hostedZone = route53.PublicHostedZone.fromHostedZoneAttributes(this, 'HostedZone', { hostedZoneId: props.hostedZoneId, @@ -38,34 +37,24 @@ class TestStack extends Stack { }); const domain = app.addDomain(props.domainName, { - subDomains: [ - { - branch: prodBranch, - prefix: 'prod', - }, - ], + subDomains: [{ branch: prodBranch, prefix: 'prod' }], customCertificate, }); domain.mapSubDomain(devBranch); } } -/** - * In order to test this you need to have a valid public hosted zone that you can use - * to request certificates for. -*/ -const hostedZoneId = process.env.CDK_INTEG_HOSTED_ZONE_ID ?? process.env.HOSTED_ZONE_ID; -if (!hostedZoneId) throw new Error('For this test you must provide your own HostedZoneId as an env var "HOSTED_ZONE_ID". See framework-integ/README.md for details.'); -const hostedZoneName = process.env.CDK_INTEG_HOSTED_ZONE_NAME ?? process.env.HOSTED_ZONE_NAME; -if (!hostedZoneName) throw new Error('For this test you must provide your own HostedZoneName as an env var "HOSTED_ZONE_NAME". See framework-integ/README.md for details.'); -const domainName = process.env.CDK_INTEG_HOSTED_ZONE_NAME ?? process.env.DOMAIN_NAME; -if (!domainName) throw new Error('For this test you must provide your own DomainName as an env var "DOMAIN_NAME". See framework-integ/README.md for details.'); +function getEnvVar(name: string, fallbackName?: string): string { + const value = process.env[name] || (fallbackName ? process.env[fallbackName] : undefined); + if (!value) throw new Error(`Environment variable ${name} is required.`); + return value; +} const app = new App(); const stack = new TestStack(app, 'cdk-amplify-app-custom-domain', { - hostedZoneId, - hostedZoneName, - domainName, + hostedZoneId: getEnvVar('CDK_INTEG_HOSTED_ZONE_ID', 'HOSTED_ZONE_ID'), + hostedZoneName: getEnvVar('CDK_INTEG_HOSTED_ZONE_NAME', 'HOSTED_ZONE_NAME'), + domainName: getEnvVar('CDK_INTEG_DOMAIN_NAME', 'DOMAIN_NAME'), }); new IntegTest(app, 'amplify-app-custom-domain-integ', {