Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multi-env container hosting (#7009) #7346

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ export abstract class ContainersStack extends cdk.Stack {
// Unused in this stack, but required by the root stack
new cdk.CfnParameter(this, 'env', { type: 'String' });

const paramDomain = new cdk.CfnParameter(this, 'domain', { type: 'String' });
const paramRestrictAccess = new cdk.CfnParameter(this, 'restrictAccess', {
type: 'String',
allowedValues: ['true', 'false'],
});

const paramZipPath = new cdk.CfnParameter(this, 'ParamZipPath', {
type: 'String',
// Required only for FULLY_MANAGED
Expand All @@ -142,6 +148,8 @@ export abstract class ContainersStack extends cdk.Stack {
const parameters: Map<string, cdk.CfnParameter> = new Map();

parameters.set('ParamZipPath', paramZipPath);
parameters.set('domain', paramDomain);
parameters.set('restrictAccess', paramRestrictAccess);

const authParams: {
UserPoolId?: cdk.CfnParameter;
Expand Down
18 changes: 17 additions & 1 deletion packages/amplify-cli/src/initialize-env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import _ from 'lodash';
import ora from 'ora';
import sequential from 'promise-sequential';
import { stateManager, $TSAny, $TSMeta, $TSContext } from 'amplify-cli-core';
import { stateManager, $TSAny, $TSMeta, $TSContext, $TSTeamProviderInfo } from 'amplify-cli-core';
import { getProviderPlugins } from './extensions/amplify-helpers/get-provider-plugins';
const spinner = ora('');
const CATEGORIES = 'categories';

export async function initializeEnv(context: $TSContext, currentAmplifyMeta?: $TSMeta) {
const currentEnv = context.exeInfo.localEnvInfo.envName;
Expand All @@ -26,6 +27,7 @@ export async function initializeEnv(context: $TSContext, currentAmplifyMeta?: $T

if (!context.exeInfo.restoreBackend) {
populateAmplifyMeta(projectPath, amplifyMeta);
populateCategoriesMeta(projectPath, amplifyMeta, teamProviderInfo[currentEnv], 'hosting', 'ElasticContainer');
}

const categoryInitializationTasks: (() => Promise<$TSAny>)[] = [];
Expand Down Expand Up @@ -112,3 +114,17 @@ function populateAmplifyMeta(projectPath: string, amplifyMeta: $TSMeta) {
Object.assign(amplifyMeta, backendConfig);
stateManager.setMeta(projectPath, amplifyMeta);
}

function populateCategoriesMeta(
projectPath: string,
amplifyMeta: $TSMeta,
teamProviderInfo: $TSTeamProviderInfo,
category: string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

categoryName

serviceName: string,
) {
if (amplifyMeta[category]?.[serviceName] &&
teamProviderInfo[CATEGORIES]?.[category]?.[serviceName]) {
Object.assign(amplifyMeta[category][serviceName], teamProviderInfo[CATEGORIES][category][serviceName]);
stateManager.setMeta(projectPath, amplifyMeta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ export async function generateHostingResources(
existingEcrRepositories,
});

const domainConfig = {
domain,
restrictAccess,
};

context.exeInfo.template = stack.toCloudFormation();

const resourceDirPath = path.join(projectBackendDirPath, constants.CategoryName, serviceName);
Expand All @@ -300,10 +305,13 @@ export async function generateHostingResources(
let jsonString = JSON.stringify(context.exeInfo.template, null, 4);
fs.writeFileSync(templateFilePath, jsonString, 'utf8');

await context.amplify.saveEnvResourceParameters(context, 'hosting', 'ElasticContainer', domainConfig);

if (addResource) {
return context.amplify.updateamplifyMetaAfterResourceAdd(constants.CategoryName, serviceName, resource);
} else {
await context.amplify.updateamplifyMetaAfterResourceUpdate(constants.CategoryName, serviceName, 'restrictAccess', restrictAccess);
await context.amplify.updateamplifyMetaAfterResourceUpdate(constants.CategoryName, serviceName, 'domain', domain);
await context.amplify.updateamplifyMetaAfterResourceUpdate(constants.CategoryName, serviceName, 'hostedZoneId', hostedZoneId);
await context.amplify.updateamplifyMetaAfterResourceUpdate(constants.CategoryName, serviceName, 'exposedContainer', exposedContainer);
await context.amplify.updateamplifyMetaAfterResourceUpdate(constants.CategoryName, serviceName, 'environmentMap', environmentMap);
Expand Down
38 changes: 38 additions & 0 deletions packages/amplify-e2e-core/src/categories/hosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ export function addDEVHosting(cwd: string): Promise<void> {
});
}

export function enableContainerHosting(cwd: string): Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['configure', 'project'], { cwd, stripColors: true })
.wait('Which setting do you want to configure?')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed offline, could you make it to use single select and not arrow movement?

.sendKeyDown(2)
.sendCarriageReturn()
.wait('Do you want to enable container-based deployments?')
.sendConfirmYes()
.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}

export function addDevContainerHosting(cwd: string): Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['add', 'hosting'], { cwd, stripColors: true })
.wait('Select the plugin module to execute')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

.sendKeyDown(2)
.sendCarriageReturn()
.wait('Provide your web app endpoint (e.g. app.example.com or www.example.com):')
.sendLine('www.test-amplify-app.com')
.wait('Do you want to automatically protect your web app using Amazon Cognito Hosted UI')
.sendConfirmNo()
.run((err: Error) => {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
}

export function addPRODHosting(cwd: string): Promise<void> {
return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['add', 'hosting'], { cwd, stripColors: true })
Expand Down
38 changes: 38 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/container-hosting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
addDevContainerHosting,
createNewProjectDir,
deleteProject,
deleteProjectDir,
enableContainerHosting,
getBackendAmplifyMeta,
initJSProjectWithProfile,
removeHosting
} from 'amplify-e2e-core';

import * as fs from 'fs-extra';
import * as path from 'path';


describe('amplify add hosting - container', () => {
let projRoot: string;

beforeAll(async () => {
projRoot = await createNewProjectDir('container-hosting');
await initJSProjectWithProfile(projRoot, {});
await enableContainerHosting(projRoot);
await addDevContainerHosting(projRoot);
});

afterAll(async () => {
await removeHosting(projRoot);
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});

it('add container hosting works', async () => {
expect(fs.existsSync(path.join(projRoot, 'amplify', 'backend', 'hosting', 'ElasticContainer'))).toBe(true);
const projectMeta = getBackendAmplifyMeta(projRoot);
expect(projectMeta.hosting).toBeDefined();
expect(projectMeta.hosting.ElasticContainer).toBeDefined();
Comment on lines +35 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything else we can check to ensure things are working correctly?

});
});