Skip to content

Commit

Permalink
Optimize Amplify App Deployment with Improved Environment Handling an…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
Imadnajam authored Nov 4, 2024
1 parent 5fca268 commit c3a980a
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand All @@ -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', {
Expand Down

0 comments on commit c3a980a

Please sign in to comment.